-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetcomics.py
1111 lines (1010 loc) · 40.5 KB
/
getcomics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/python 3
"""
Scrapes http://www.readcomics.net and creates .cbz files from
downloaded image files.
The sites Robots.txt file
User-agent: *
Disallow:
Disallow: /cgi-bin/
Creates instance of GetComic class(the only class in the module :P),
and calls the setup method which gets the home directory where all files will
be stored and invokes the home method, which is the main text menu of the script
where all other functions are called from.
*************************
# Some Things To Note #
*************************
1)The sites html is pretty simple to scrape, however there were some issues with
consistency, found that some of the chapter listings were out of order or numbered wrong completely.
Ive attempted to remedy this a bit.
2)Some sleep periods have been placed between the downloading of images
as to be nice to their servers.They mey need to be adjusted.
3)Some chapters are just non existant(ugh) when the error is raised it simply notifys the user and
continues(they really need to clean up the site a bit :P)
*************************
# To Do list #
*************************
1)General code cleanup and random attempts at optimizing whats already here.
2)Fix the freaking search function its no bueno and does not worko
3)Write better Doc strings
"""
from bs4 import BeautifulSoup as bs
import calendar
import datetime
from distutils.dir_util import copy_tree as dis_copy
from getpass import getpass as maskinput
import glob
import json
import os
import requests as re
import re as regx
import shutil
import sys
from tempfile import TemporaryFile
import textwrap
import time
import tkinter as tk
from tkinter import filedialog
from tqdm import tqdm
import webbrowser
import zipfile
class GetComic():
def __init__(self):
"""Scrapes readcomics.net and Creates '.cbz' files from
downloaded images"""
self.last_dir = None
self.last_chapter_name = None
self.script_dir = os.path.dirname(os.path.abspath(__file__))
self.home_dir = self.setup()
self.comicList = 'http://www.readcomics.io/comic-list'
self.pull_load = False
self.leave = False
self.clean_up = True
self.pull_list = {}
self.book_lib = {}
self.todays_date = datetime.date.today()
self.day = calendar.day_name[self.todays_date.weekday()]
def setup(self):
"""
Gets directory path where the cbz files are to be saved and
serializes it as a json file.If the file already exists,
loads its contents into home_dir attribute
Returns
-------
output(string):
path to directory
"""
if 'config.json' in os.listdir(os.path.dirname(os.path.abspath(__file__))):
with open('config.json',) as infile:
data = json.load(infile)
self.last_dir = data['last'][0]
self.last_chapter_name = data['last'][1]
return data['home']
else:
print("Home Configuration file ('config.json') not Found.\n")
time.sleep(1)
print('Please Select A home Directory where all Files will Be saved.')
time.sleep(2)
while True:
root = tk.Tk()
root.withdraw()
path = filedialog.askdirectory()
try:
if path == '':
pass
elif os.path.exists(path):
self.home_dir = path
print('\nHome Directory:',path,'saved.')
time.sleep(2)
self.update_config()
print("\033c")
return path
except PermissionError:
print('Access Denied! Application does not have permission\
to access desired location!')
os.chdir(self.home_dir)
time.sleep(3)
print("\033c")
break
except FileNotFoundError:
print('No such directory.:',path)
os.chdir(self.home_dir)
time.sleep(3)
print("\033c")
def get_list(self,src):
"""
Retrieves chapter href links, book title,and the books description.
Sends a request to readcomics.net comic display page
ex. http://www.readcomics.net/comic/batman Parses code for
the above items
Parameters
----------
src(string):
Html link to comic's page
Returns
-------
output(tuple):
returns a tuple containing the books title,
a modified title(that has all illegal filename/directory chars
removed,a list of hrefs(the chapters),and the books description
"""
page = re.get(src)
page.raise_for_status()
soup = bs(page.text,"lxml")
soup_book_name = soup.find('strong').getText()
corrected_book_name = self.directory_name_check(soup_book_name)
description = soup.find('p').getText()
chapters = soup.find_all('a',{'class':'ch-name'},href = True)
ch_list = [os.path.join(ch['href'],'full') for ch in chapters]
chap_dates = [i.text for i in soup.find_all('span')][12:-2]
book_info = soup.body.find('div',attrs= {'class': 'manga-details'})
book_i_list = [i.text.strip() for i in book_info.find_all('td')]
return corrected_book_name,ch_list,description,book_i_list,chap_dates
def convert_chapter_name(self,title,url):
"""
$Not in use anymore not going to be deleted incase that
changes
converts a number string to its english word equivalent
i.e: '1' ==> 'One'.
Parameters
----------
title(string):
String representing the book title
url(string):
html link to book chapter
Returns:
--------
Output(string):
a concatnated string representing the title and chapter number.
"""
ending = self.get_chap_num(url)
#print(ending)
if ending == '00':
new = num2words(0)
elif '-' in ending:
temp = ending.split('-')
temp[0] = num2words(int(temp[0]))
temp[-1] = num2words(int(temp[-1]))
new = ''.join([temp[0],'-',temp[-1]])
else:
new = num2words(int(ending))
return ''.join([title,' ','Chapter',' ',ending])
def directory_name_check(self,dir_name):
"""
Removes illegal characters from the string
that the directory will be named with.
(/, \\, <, |, >, *) All become an empty single space substrings(' ').
(?,:) become empty substrings with no spacing ('').
(") double quotes become (')the single quotes
loops until all illegal characters are removed
Parameters
----------
dir_name(string):
String representing book title that may contain illegal characters.
Returns
-------
output(string):
a legal directory name
"""
mapping = [ ('/', ' '), ('\\', ' '), ('"',"'"), (':', ''), ('<', ' '),\
('>', ' '), ('|'," "), ('*', ' '), ('?', ''), ]
for k, v in mapping:
dir_name = dir_name.replace(k, v)
return dir_name
def download_chapter(self,book_dir_title,link):
"""
Downloads indivdual chapters from selected series.
Creates a directory using the book_dir_title argument and
checks to see if a file of the same name exists as to avoid
making any unnecessary requests.
if a matching file name is not found,
method parses code and retrieves all links inside [IMG] tags
from page's source code
Displays a simple progress bar from the tqdm library as it
iterates over the list of links,Entering a second loop
inside the 'with' context manager and downloads the file bit by bit
before closing it.
Parameters
----------
book_dir_title(string):
Name of series
link(string):
html link to books chapter
Returns
-------
output:None
"""
try:
pages = []
if not os.path.isdir(os.path.join(self.home_dir,book_dir_title)):
os.mkdir(book_dir_title)
ch_name = ''.join((book_dir_title,' ','Chapter',' ',self.get_chap_num(link)))
if os.path.isfile(os.path.join(self.home_dir,book_dir_title,ch_name+'.cbz')):
print()
print('Chapter Already Downloaded.')
time.sleep(1)
print()
print("\033c")
return
os.chdir(book_dir_title)
print(book_dir_title)
print('Downloading: {}.'.format(ch_name))
print('Press Ctrl + C to cancel.')
self.last_dir = os.getcwd()
self.last_chapter_name = ch_name
self.update_config()
chapter_page = re.get(link)
chapter_page.raise_for_status()
ch_soup = bs(chapter_page.text,'lxml')
ch_list = [i['src'] for i in ch_soup.find_all('img')]
del ch_list[0]
for index,item in enumerate(tqdm(ch_list),start = 1):
if index <= 9:
name = ''.join([ch_name,' ','0',str(index),'.jpg'])
else:
name = ''.join([ch_name,' ',str(index),'.jpg'])
pages.append(name)
res = re.get(item)
res.raise_for_status()
with open(name, mode = 'wb') as out:
for chunk in res.iter_content(10000):
out.write(chunk)
time.sleep(.4)
self.createCbz(ch_name,os.getcwd(),pages)
os.chdir(self.home_dir)
print('Done!')
time.sleep(2)
print("\033c")
except KeyboardInterrupt:
print("\033c")
print('Operation Terminated by User')
print('\rCleaning Up...')
path = os.path.join(self.home_dir,book_dir_title)
if os.getcwd() != path:
os.chdir(path)
try:
if len(pages) != 0:
for pics in pages:
os.remove(pics)
os.chdir(self.home_dir)
os.rmdir(path)
finally:
print('Returning...')
#os.chdir(self.home_dir)
time.sleep(2)
print("\033c")
self.leave = True
return
except re.exceptions.HTTPError:
print('There was a problem retrieving files.')
print('...')
time.sleep(1)
print('Cleaning Up.')
print('Continuing...')
time.sleep(1.2)
os.chdir(self.home_dir)
print("\033c")
def choose_chapter_list(self,name,links,dates):
"""
Displays a list of issues for a given series
and allows user to make a selection from the list.
Parameters
----------
name(string):
Name of book
links(list):
list of chapter links for given book
dates:
Returns
-------
output(int):
interger representing the Issue Number
used to get link by index from list.
"""
print("\033c")
links.sort(key = self.natural_key)
while True:
print('####################')
print('# Chapter Select #')
print('####################\n')
print("Press: 'q' or type: 'back' to return\n")
for index,item in enumerate(links,start = 1):
#subtracts 1 inorder to keep the index correct since we started at one in the line above
print('{0}){1}{2:>10}'.format(index,name,dates[index-1]))
choice = input('>>>')
if choice in ('back','b','q'):
print("\033c")
return
elif choice.isdigit():
#Again,subtracts 1 inorder to keep the index correct
issue_number = int(choice)-1
if issue_number in range(len(links)):
print("\033c")
return issue_number
else:
print('Invalid Entry')
time.sleep(1)
print("\033c")
def natural_key(self,string_):
"""
See http://www.codinghorror.com/blog/archives/001018.html
Used Reg Ex to create a key for sorting list in 'Natural' Order
Parameters
----------
string_(string):
string for sorting
Returns
-------
output(list):
returns key for sorting
"""
return [int(s) if s.isdigit() else s for s in regx.split(r'(\d+)', string_)]
def get_chap_num(self,link):
''' '''
if link.endswith('/full'):
temp = list(link)
del temp[-5:]
link = ''.join(temp)
cut_num = link.rfind('r') + 2
return link[cut_num:]
def book_display(self,src):
"""
Basic Text menu, to display information and download options
for a given series.
Parameters
----------
src(string):
url for book to feed get_list function
Returns
-------
Output:None
Allows for the download of indivdual as well as multiple chapters
if the optional argument 'add_option is set to True,
displays another option 'F' which allows the user to add a series to the
pull list
"""
print("\033c")
title,chapters,description,book_details,chap_dates = self.get_list(src)
chapters.sort(key = self.natural_key)
title_length = len(title) + 2
ch_list_length = len(chapters)
start_num = chapters[0].rfind('-')+ 1
latest_num = chapters[-1].rfind('-') + 1
dir_exists = False
if title not in self.pull_list.keys():
add_option = True
else:
add_option = False
while True:
print()
print('='* title_length)
print(''.join(('#',title,'#')))
print('=' * title_length)
print('Issues:',ch_list_length)
print('First:{} #{}'.format(title,self.get_chap_num(chapters[0])))
print('Latest:{} #{}'.format(title,self.get_chap_num(chapters[-1])))
print()
print('Menu')
print('A)Description.')
print('B)Download First Ch.')
print('C)Download Latest Ch.')
print('D)Download All.')
print('E)Choose Chapter.')
if add_option:
print('F)Add to Pull List.')
else:
print('F)Remove from Pull List.')
if os.path.isdir(title):
dir_exists = True
print('G)Delete Folder.')
print('O)Open Folder')
else:
dir_exists = False
print('Q)Back')
choice = ''.join(input('>>> ').split()).lower()
if choice == 'q':
print("\033c")
break
elif choice == 'a':
print('\033c')
print('###############')
print('# Description #')
print('###############')
print('\n'.join(textwrap.wrap(description)).strip())
print()
for heading,entry in zip(book_details[::2],book_details[1::2]):
print(heading,entry)
maskinput('\nPress Enter To Continue.')
print('\033c')
elif choice == 'b':
self.download_chapter(title,chapters[0])
elif choice == 'c':
self.download_chapter(title,chapters[-1])
elif choice == 'd':
for index,chap in enumerate(chapters,start = 1):
if self.leave == True:
self.leave = False
break
print('Chapter {} of {}'.format(index,len(chapters)))
self.download_chapter(title,chap)
time.sleep(.6)
elif choice == 'e':
issue_number = self.choose_chapter_list(title,chapters,chap_dates)
if type(issue_number) == int:
self.download_chapter(title,chapters[issue_number])
elif choice == 'f':
if add_option:
self.pull_list[title] = src
self.update_pull()
add_option = False
print(title,'added to Pull List')
time.sleep(2)
print('\033c')
elif title in self.pull_list.keys():
add_option = True
del self.pull_list[title]
self.update_pull()
print(title,'removed from Pull List')
time.sleep(2)
print('\033c')
elif choice == 'g' and dir_exists:
print('\033c')
while True:
print('################')
print('#{0} Warning! {0} #'.format(u"\u26A0"))
print('################')
print('Continuing will erase the contents of:')
print(os.path.join(self.home_dir,title))
decision = ''.join(input('Continue(Y/N)? >>> ').split()).lower()
if decision in ('y','yes','ya'):
print('Deleting...')
tqdm(shutil.rmtree(os.path.join(self.home_dir,title)))
print('Folder Deleted')
maskinput('Press Enter to Continue.')
break
elif decision in ('n','nah','no'):
break
else:
print('Ivalid entry!')
print('\033c')
print('\033c')
elif choice == 'o' and dir_exists:
check = os.system('xdg-open "%s"' % title)
print('\033c')
if check != 0:
print('An error occurred! The containting folder could not be opened!')
maskinput('Press Enter to Continue.')
print('\033c')
else:
print()
print('Invalid Entry!')
time.sleep(1)
print('\033c')
def createCbz(self,name,src,dst = '.',page_list = []):
"""
Creates zip archive, writes pages, and changes
ext to '.cbz'
changes to a given directory, uses glob to get a list
of all files with the 'jpg' ext.
does a sort on the glob list and using the 'with'
context manager in combination with zipfile,
it iterates over the list, adding it to the zip then calls
os.remove() to delete the file from the directory.
a new name for the zip is made by splitting the file name from the zip
extension and joining the file name to its new '.cbz' extension
Parmeters:
---------
name(string):
Name
"""
os.chdir(src)
zip_name = ''.join((name,'.zip'))
if page_list:
pages = page_list
else:
pages = glob.glob('*jpg')
pages.sort(key = self.natural_key)
#Compresses zipfile but little slower to open
with zipfile.ZipFile(zip_name,mode = 'w',compression = zipfile.ZIP_DEFLATED) as page:
for i in pages:
page.write(i)
os.remove(i)
new_name = ''.join((os.path.splitext(zip_name)[0],'.cbz'))
os.rename(zip_name,new_name)
def load_pull(self):
"""
Loads in pull_list.config.
Creates pull list.json file if one does not already exist in
the directorythen creates an empty dictionary assining it
to the pull_list attribute.
otherwise opens file and saves pull_list.config contents to
the pull_list class attribute
Return
------
output(None):
"""
file_path = os.path.join(self.script_dir,'pull list.json')
if not os.path.isfile(file_path)or os.path.getsize(file_path) == 0 :
with open(file_path,'w') as out:
json.dump({},out)
self.pull_list = {}
return
with open(file_path) as infile:
self.pull_list = json.load(infile)
def update_pull(self):
"""Creates pull list.json if it doesn't already exist in the directory.
opens 'pull list.json' and saves it the 'data' variable updates it with the current
dictionary in the pull_list attribute and opens the json file one final time
to save the dictionary to file"""
file_path = os.path.join(self.script_dir,'pull list.json')
if not os.path.isfile(file_path)or os.path.getsize(file_path) == 0 :
with open(file_path,'w') as out:
json.dump(self.pull_list,out)
else:
with open(file_path) as infile:
data = json.load(infile)
data.update(self.pull_list)
with open(file_path,'w') as out:
json.dump(self.pull_list,out)
def update_config(self):
"""
"""
file_path = os.path.join(self.script_dir,'config.json')
config = {'script':self.script_dir,'home':self.home_dir,'last':[self.last_dir,self.last_chapter_name]}
with open(file_path,'w') as out:
json.dump(config,out)
def download_pull_list(self):
"""Displays contents dictionary keys from pull list attribute.
if user input in range of the of the length of the list of
keys, retireves the corresponding link, and then calls self.book_display
to show the overview and download options"""
print("\033c")
self.load_pull()
if not self.pull_list:
print()
print('Pull List empty!')
print()
time.sleep(1)
print("\033c")
return
while True:
book_list = [i for i in self.pull_list.keys()]
book_list.sort()
numbered_list = [''.join((str(i),')',book_list[i])) for i in range(len(book_list))]
print('#############')
print('# Pull List #')
print('#############')
print("Press: 'q' or type: 'back' to return\n")
for comic in numbered_list:
print(comic)
choice = ''.join(input('>>> ').split()).lower()
if choice in ('back','b','q'):
print("\033c")
return
elif choice.isdigit() and int(choice) in range(len(book_list)):
book_link = self.pull_list.get(book_list[int(choice)])
self.book_display(book_link)
else:
print('Invalid Entry')
time.sleep(1)
print("\033c")
def edit_pull_list(self):
"""Allows user to add or delete entries to the pull list json file"""
self.load_pull()
print("\033c")
while True:
print('###################')
print('# Pull List Edit #')
print('###################')
print("Press: 'q' or type: 'back' to return\n")
print('A) Add to Pull List')
print('B) Delete Pull List entry')
print('Q) Back')
choice = input('>>> ').lower()
if choice == 'q':
print("\033c")
break
elif choice == 'a':
print("\033c")
self.library_search()
elif choice == 'b':
if not self.pull_list:
print("\033c")
print('Pull list empty!')
time.sleep(2)
print("\033c")
else:
print("\033c")
break
while True:
comic_list = [i for i in self.pull_list.keys()]
comic_list.sort(key = self.natural_key)
numbered_list = [''.join((str(i),')',comic_list[i])) for i in range(len(comic_list))]
if not self.pull_list:
print('Pull list empty!')
time.sleep(2)
print("\033c")
break
print('#####################')
print('# Pull Entry Delete #')
print('#####################')
for i in numbered_list:
print(i)
print('\nWhat would you like to delete?')
del_choice = ''.join(input('>>> ').split()).lower()
if del_choice in ('back','b','q'):
print("\033c")
break
elif del_choice.isdigit():
del_choice = int(del_choice)
if del_choice in range(len(comic_list)):
print('Deleting',comic_list[del_choice])
time.sleep(2)
del self.pull_list[comic_list[del_choice]]
self.update_pull()
print("\033c")
else:
print('Invalid Entry')
time.sleep(1)
print("\033c")
else:
print('Invalid Entry')
time.sleep(1)
print("\033c")
def overwrite_check(self,files,dst):
"""Verifys if files to be moved, that exist in another directory, are ok to be
overwritten"""
print("\033c")
while True:
print('These files/directories already exist at:',dst,'\nand will cannot be transferred')
for i in files:
print(i)
print('Overwrite(Y/N)?')
choice = ''.join(input('>>> ').split()).lower()
if choice in ('yes','y'):
return True
elif choice in ('no','n'):
return False
else:
print('Invalid Entry!')
time.sleep(1)
print("\033c")
def library_load(self):
"""Attempts to establish connection to webpage and gathers all
hrefs from side source code.
Uses requets to connect to readcomics.net, then parses
the html(with Beautiful Soup) and extracts all the href links using
a list comprhension.
Finally uses a for loop to iterate over the list and places the comics title(key)
and corresponding href links(value) into a dictionary"""
try:
page = re.get(self.comicList)
soup= bs(page.text,'lxml')
bad_links = (r'http://www.readcomics.io/',r'http://www.readcomics.io/advanced-search',\
r'http://www.readcomics.io/popular-comic',r'http://www.readcomics.io/comic-list',\
r'http://www.readcomics.io/comic-updates')
links = [i['href'] for i in soup.select('ul > li > a') if i['href'] not in bad_links]
for i in links:
temp_list = list(i)
del temp_list[:31] #comic name
temp_join = ''.join(temp_list)
title = temp_join.replace('-',' ')
self.book_lib[title] = i
except req.HTTPError:
print("\033c")
print('Failed to load library.')
print('Please check your connection and restart Application.')
print('Terminating')
sys.exit(1)
def library_search(self):
"""Simple text menu that waits for user input on their preferred method
of searching calls keyword_search() with/without optional 'abc'
argument"""
print("\033c")
while True:
print('#############')
print('# Library #')
print('#############\n')
print('A)Search by keyword')
print('B)Search by Letter')
print('C)Search All')
print('Q)Back')
choice = ''.join(input('>>> ').split()).lower()
if choice == 'q':
print("\033c")
break
elif choice == 'a':
self.keyword_search()
elif choice == 'b':
self.keyword_search(abc = True)
elif choice == 'c':
pass
else:
print('Invalid Entry')
time.sleep(1)
print('\033c')
def keyword_search(self,abc = False):
""""Takes a user string and searches through the library for a match
Gets input from user,and then uses a list comprehension
to loop over all the keys in the self.book_lib attribute and
adds them to the list if part of the user generated string is
containted within the key string.
If no matches are found prints 'No results found!', clears screen
and enters a new loop where it prompts the user to search again.
If the 'abc' argument is sent to True:
allows user to display all comics whose first character matches the users
entry"""
print("\033c")
if abc == True:
word = ' Abc '
else:
word = 'Keyword'
while True:
print('#####################')
print('# Search({}) #'.format(word))
print('#####################')
print("Type: '00' to return \n")
selection = ''.join(input('>>> ').split()).lower()
if selection in ('00'):
print("\033c")
return
if abc == True:
comics = [i.lower() for i in self.book_lib if i[0] == selection[0]]
else:
comics = [i.lower() for i in self.book_lib if selection in i]
if len(comics) == 0:
print('No Results Found!')
time.sleep(2)
print("\033c")
while True:
print('Search Again?')
again = input('>>> ').lower()
if again in ('y','yes'):
break
elif again in ('n','no'):
return
else:
break
print("\033c")
comics.sort(key = self.natural_key)
numbered_list = [''.join((str(i),')',comics[i])) for i in range(len(comics))]
while True:
print('####################')
print('# Search Results #')
print('####################\n')
print('Showing Matches for: {}'.format(selection))
print('{} result(s) found\n'.format(len(comics)))
for result in numbered_list:
print(result.title())
pick = ''.join(input('>>> ').split()).lower()
if pick in ('back','b','q'):
print("\033c")
return
elif pick.isdigit():
if int(pick) in range(len(comics)):
book_link = self.book_lib[comics[int(pick)]]
self.load_pull()
if book_link not in self.pull_list.values():
self.book_display(book_link)
else:
self.book_display(book_link)
else:
print('Invalid Entry!')
time.sleep(1)
print('\033c')
def options(self):
"""Displays a list of operations for user to invoke.
allows user to erase their collection,
clear the pull list json file, as well as
change the home directory."""
print("\033c")
while True:
print('#############')
print('# Options #')
print('#############\n')
print('A)Delete Pull List')
print('B)Delete Collection')
print('C)Change Home Directory')
print('D)Clean Up Configuration')
print('Q)Back')
choice = ''.join(input('>>> ').split()).lower()
if choice not in ('a','b','c','q'):
print('Invalid Entry!')
time.sleep(1)
print('\033c')
elif choice == 'q':
print("\033c")
break
elif choice == 'a':
print("\033c")
while True:
print('Are You sure you want to delete Your Pull List?')
print('All information will be lost')
del_choice = ''.join(input('Delete pull list?(y/n)>>> ').split()).lower()
if del_choice in ('y','yes','ya'):
self.pull_list = {}
self.update_pull()
print('\nPull List Cleared..')
time.sleep(2)
break
elif del_choice in ('n','no','q'):
break
else:
print('Invalid Entry!')
time.sleep(1)
print("\033c")
print("\033c")
elif choice == 'b':
print("\033c")
while True:
print('Are You sure you want to delete Your collection located at:\n',\
os.getcwd())
print('All Files in folder will be Deleted.')
del_choice2 = ''.join(input('Delete Collection(y/n)>>> ').split()).lower()
if del_choice2 == 'y':
contents = os.listdir()
print('\nDeleting Files')
for i in tqdm(contents):
if os.path.isdir(i):
shutil.rmtree(i)
#else:
#os.remove(i) #Deletes files not in folders
print('Files Removed')
time.sleep(2)
break
elif del_choice2 == 'n':
break
else:
print('Invalid Entry!')
time.sleep(1)
print("\033c")
print("\033c")
elif choice == 'c':
self.change_dir()
def change_dir(self):
""" """
print("\033c")
while True:
print('Current Directory:',os.getcwd())
print('Would You like to change directories?')
dir_choice = ''.join(input('>>> ').split()).lower()
if dir_choice not in ('n','no','q','quit','yes','y'):
print('Invalid Entry!')
time.sleep(1)
print("\033c")
elif dir_choice in ('n','no','q','quit'):
print("\033c")
return
elif dir_choice in ('yes','y'):
break
while True:
root = tk.Tk()
root.withdraw()
path = filedialog.askdirectory()
if path == '':
print("\033c")
return
try:
temp = TemporaryFile(dir = path)
temp.close()
except PermissionError:
print('Access Denied! Application does not have permission\
to access desired location!')
os.chdir(self.home_dir)
time.sleep(3)
print("\033c")
continue
except FileNotFoundError:
print('No such directory.:',path)
os.chdir(self.home_dir)
time.sleep(3)
print("\033c")
continue
except FileExistsError:
print('directory already exists:.',path)
os.chdir(self.home_dir)
time.sleep(3)
print("\033c")
continue