-
Notifications
You must be signed in to change notification settings - Fork 12
/
KissDownloader.py
863 lines (775 loc) · 35 KB
/
KissDownloader.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
#!/usr/bin/env python
import pip
import sys
import platform
import os
import time
import glob
import csv
import re
import socket
from pathlib import Path
import threading
from threading import Thread, Lock
import queue as Queue
from urllib.parse import urlparse
import urllib.request
import shutil
from utils import *
# install requires modules
try:
pip.main(['install', '--upgrade', 'requests'])
import requests
except:
pip.main(['install', 'requests'])
import requests
try:
pip.main(['install', '--upgrade', 'youtube_dl'])
import youtube_dl
except:
pip.main(['install', 'youtube_dl'])
import youtube_dl
try:
pip.main(['install', '--upgrade', 'pySmartDL'])
import pySmartDL
except ImportError:
pip.main(['install', 'pySmartDL'])
import pySmartDL
try:
pip.main(['install', '--upgrade', 'BeautifulSoup4'])
from bs4 import BeautifulSoup
except ImportError:
pip.main(['install', 'BeautifulSoup4'])
from bs4 import BeautifulSoup
try:
pip.main(['install', '--upgrade', 'selenium'])
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
except ImportError:
pip.main(['install', 'selenium'])
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
if getattr(sys, 'frozen', False):
utils.slog("Release: " + platform.system() + " " + platform.release())
dir_path = os.path.dirname(sys.executable)
chromedriver = utils.resource_path("chromedriver.exe")
chromedriver_path = Path(chromedriver)
if not chromedriver_path.is_file():
utils.log("=E Chromedriver not found")
sys.exit()
ublock_origin = utils.resource_path("ublock_origin.crx")
ublock_origin_path = Path(ublock_origin)
if not ublock_origin_path.is_file():
utils.log("=E ublock_origin.crx not found")
sys.exit()
elif __file__:
utils.slog("Release: Python")
dir_path = os.path.dirname(__file__)
if not dir_path:
dir_path = os.path.dirname(os.path.realpath(__file__)) # fallback
if not dir_path:
utils.log("=E Unable to resolve dir_path")
sys.exit()
if platform.system() == "Windows":
chromedriver = dir_path + "/chromedriver.exe"
chromedriver_path = Path(chromedriver)
if not chromedriver_path.is_file():
utils.log("=E Chromedriver not found http://chromedriver.storage.googleapis.com/index.html?path=2.9/")
sys.exit()
else:
chromedriver = ""
ublock_origin = dir_path + "/ublock_origin.crx"
utils.log("== Starting up ==")
utils.slog("OS: " + platform.system() + " " + platform.release())
utils.slog("Directory: " + str(dir_path))
# read kissdownloader.ini
utils.config = utils.read_settings()
if not utils.config['DEFAULT']['username'] or not utils.config['DEFAULT']['userpassword']:
utils.log("Configuration file located in: "+utils.get_config_path()+"\kissdownloader.ini")
utils.log("=E Please provide username and password in the configuration file.")
sys.exit()
else:
username = utils.config['DEFAULT']['username']
userpassword = utils.config['DEFAULT']['userpassword']
destination_folder = utils.config['DEFAULT']['destination_folder']
download_threads = utils.config['OTHER']['download_threads']
complete_dir = utils.config['OTHER']['complete_dir']
demo_data = utils.config['OTHER']['demo_data']
if not os.path.exists(dir_path+'/resolved.csv'):
open(dir_path+'/resolved.csv', 'a').close()
queue=Queue.Queue()
count=0
download_list=({})
download_prog = False
class KissDownloader(threading.Thread):
def __init__(self, params, queue):
if(params):
self.rootPage=""
self.file_extension=""
self.download(params)
else:
threading.Thread.__init__(self)
self.queue=queue
def run(self):
global count
global download_list
while True:
host=self.queue.get()
#print('url',host)
nestlist=[x for x in episode_list if host in x[0]]
if(nestlist):
count=count + 1
if not os.path.isfile(nestlist[0][2] + nestlist[0][1]):
episode=str(nestlist[0][3])
#print("Download", episode)
#urlretrieve(str(host).replace(" ","%20"), str(nestlist[0][2] + "temp/" + nestlist[0][1]))
#path=nestlist[0][2] + "temp/" + nestlist[0][1]
#location=obj.get_dest()
if "9xbuddy" in nestlist[0][0]:
url = str(host).replace(" ","%20")
file_name = nestlist[0][2] + nestlist[0][1]
console_output = "Downloading " + nestlist[0][1]
print(console_output)
#add to download list
try:
download_list[nestlist[0][3]] = console_output # update
except KeyError:
download_list[nestlist[0][3]].append(console_output) # initial
except Exception as e:
utils.slog(e)
utils.log("=E Download logic error")
response = requests.get(url, stream=True)
with open(file_name, 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
del response
#remove from download list
try:
del download_list[nestlist[0][3]] # remove
except Exception as e:
utils.slog(e)
utils.log("=E Unable to remove episode" + str(nestlist[0][3]))
print("Finished Downloading " + nestlist[0][1])
else:
try:
obj=pySmartDL.SmartDL(str(host).replace(" ","%20"), nestlist[0][2] + "temp/" + nestlist[0][1], progress_bar=False, fix_urls=True)
if nestlist[0][4]:
obj.headers = nestlist[0][4]
obj.start(blocking=False)
while True:
if obj.isFinished():
break
progress = obj.get_progress() * 100
if obj.get_eta() > 0 and (obj.get_progress() * 100) < 100:
console_output=str(nestlist[0][1] + "\t " + str(float("{0:.2f}".format((float(obj.get_progress())*100)))) + "% done at " + pySmartDL.utils.sizeof_human(obj.get_speed(human=False)) + "/s, ETA: "+ obj.get_eta(human=True))
try:
download_list[nestlist[0][3]]=console_output # update
except KeyError:
download_list[nestlist[0][3]].append(console_output) # initial
except Exception as e:
utils.slog(e)
utils.log("=E Download logic error")
time.sleep(1)
if progress == 100 and obj.get_eta() == 0:
time.sleep(1)
if obj.isFinished():
try:
del download_list[nestlist[0][3]] # remove
except Exception as e:
utils.slog(e)
utils.log("=E Unable to remove episode" + str(nestlist[0][3]))
try:
if os.path.isfile(nestlist[0][2] + "temp/" + nestlist[0][1]):
os.rename(nestlist[0][2] + "temp/" + nestlist[0][1], nestlist[0][2] + nestlist[0][1]) # move on download complete
utils.log("Completed " + str(episode))
else:
utils.log("=E Failed " + str(episode))
except Exception as e:
utils.slog(e)
utils.log("=E Failed moving " + str(nestlist[0][2] + "temp/" + nestlist[0][1]) + " to " + str(nestlist[0][2] + nestlist[0][1]))
except socket.timeout as e:
utils.log("=E Episode "+str(episode)+" timeout")
count=count - 1
self.queue.task_done()
def download_message():
global download_prog
global download_list
global count
if(download_prog == 0): # one instance
download_prog=1
while count > 0:
sep = 0
for item in download_list:
if download_list[item]:
if sep == 0:
sep = 1
print("------")
print(download_list[item]) # output download progress
else:
utils.log('Download starting...')
time.sleep(4)
download_prog=0
def login(self, user, pw, site):
status=""
while (status == 503 and status != ""):
req=requests.head(str(site))
status=req.status_code
utils.log("=E Status code: " + str(req.status_code))
return status
time.sleep(2)
utils.log("Login... (5 second cloudflare check)")
try:
self.driver.get(str(site) + "/Login")
time.sleep(4)
self.driver.implicitly_wait(30)
self.driver.execute_script("window.stop()")
time.sleep(1)
try: # fill login form and send
username=self.driver.find_element_by_id("username")
password=self.driver.find_element_by_id("password")
username.send_keys(user)
password.send_keys(pw)
password.send_keys(Keys.RETURN)
time.sleep(2)
except Exception as e:
utils.slog(e)
utils.log("=E Login page not loaded (critical)")
return False
except Exception as e:
utils.slog(e)
utils.log("=E Login failed")
time.sleep(4)
return False
# print(self.driver.current_url)
if str(self.driver.current_url).lower() == site + "/login" or str(self.driver.current_url).lower() == site + "/login": # confirm login, return bool
return False
else:
return True
def get_episode_regex(self, keyword, episode, keyword2, string):
episode = episode.replace('-5', '.5')
string = string.replace('-5', '.5')
#print(keyword,episode,keyword2,string)
if '.5' in episode and '.5' in string:
#print('decimal')
regex = re.compile(keyword+'([0-9]*).5'+keyword2)
elif '.5' not in episode and 'a?id=' in string: # incomplete
print('a?', string, episode, string)
regex = re.compile(keyword+'([0-9]*)a'+keyword2)
elif '.5' in episode and 'b?id=' in string:
regex = re.compile(keyword+'([0-9]*)b'+keyword2)
else:
regex = re.compile(keyword+'([0-9]*)'+keyword2)
try:
if float(episode) == float(regex.findall(string)[0]):
utils.log('Found ['+str(keyword)+str(episode)+str(keyword2)+'] in ['+str(string)+']')
return regex.findall(string)[0]
#else:
# print('Not2',episode,regex.findall(string)[0])
#else:
# print('Not',float(episode),float(regex.findall(string)[0]))
except IndexError as e:
return None
except ValueError as e:
return None
except Exception as e:
utils.slog(e)
sys.exit()
return None
def get_episode_page(self, episode, site): # parse video url, get episode page from list
# TODO support for 0 episode
# TODO test support for -5 episode http://kissanime.ru/Anime/Durarara episode 12.5
soup=BeautifulSoup(self.rootPage, 'html.parser')
init_episode=float(episode)
episode=str(init_episode).replace(".0","")
episode=str(episode).replace(".5","-5")
for link in soup.findAll('a'): # uncensored
currentlink=link.get('href')
if currentlink is None:
pass
elif self.get_episode_regex('uncensored-episode-', episode, '', currentlink.lower()) \
or self.get_episode_regex('uncen-episode-', episode, '', currentlink.lower()) \
or self.get_episode_regex('episode-', episode, '-uncensored?', currentlink.lower()) \
or self.get_episode_regex('episode-', episode, '-uncen?', currentlink.lower()) \
or self.get_episode_regex('', episode, '', currentlink.lower()):
return [site + "" + currentlink.lower(), True]
for link in soup.findAll('a'): # censored
currentlink=link.get('href')
if currentlink is None:
pass
elif self.get_episode_regex('episode-', episode, '', currentlink.lower()) \
or self.get_episode_regex('ova-', episode, '?', currentlink.lower()) \
or self.get_episode_regex('', episode, '?id=', currentlink.lower()):
return [site + "" + currentlink.lower(), False]
for link in soup.findAll('a'): # experimental
currentlink=link.get('href')
if(currentlink is None):
pass
else:
if("episode-" in link.get('href').lower()):
currentlinkx=currentlink.lower()
episodex=0
if ("/anime/" in currentlinkx):
currentlinkx=currentlinkx.replace("/anime/", "")
animetitle=currentlinkx.split("/", 1)
for item in animetitle: # get last item
episodexx=item
if animetitle[0] + "-" in episodexx:
episodex=episodexx.replace(
animetitle[0] + "-", "")
# print("found [" + episodex + "]")
episodex=episodex.split("-")[0]
try:
if float(episodex) == float(episode) and float(episode) != 0:
return [site + "" + currentlink.lower(), False]
else:
pass
except ValueError:
pass
except Exception as e:
utils.slog(e)
utils.log("=E Exception reading episode in experimental")
sys.exit()
return ["", False]
def get_video_src(self, episode_page, resolution): # parse url, return video url
self.captch_check_plus_server_preference(episode_page, "&s=kissanime")
currentpage=self.driver.page_source
soup=BeautifulSoup(currentpage, 'html.parser')
# print(currentpage)
time.sleep(2) # delay for page render
#kissanime server (google)
try:
resolution=int(resolution)
except Exception as e:
utils.slog(e)
utils.log("=E Resolution " + str(resolution))
sys.exit()
if(resolution >= 1080 or resolution == 0):
teneighty=pattern=re.compile(r'x1080.mp4')
for link in soup.findAll('a', text=teneighty):
return [link.get('href'), ".mp4", "1080p", ""]
if(resolution >= 720 or resolution == 0):
seventwenty=pattern=re.compile(r'x720.mp4')
for link in soup.findAll('a', text=seventwenty):
return [link.get('href'), ".mp4", "720p", ""]
if(resolution >= 480 or resolution == 0):
foureighty=pattern=re.compile(r'x480.mp4')
for link in soup.findAll('a', text=foureighty):
return [link.get('href'), ".mp4", "480p", ""]
if(resolution >= 360 or resolution == 0):
threesixty=pattern=re.compile(r'x360.mp4')
for link in soup.findAll('a', text=threesixty):
return [link.get('href'), ".mp4", "360p", ""]
if(resolution >= 0): # fallback
finalcheck=pattern=re.compile(r'.mp4')
for link in soup.findAll('a', text=finalcheck):
resolutionr=str(link).rsplit('.mp4')[0][-3:]
if(int(resolutionr) and int(resolutionr) >= 360 and int(resolutionr) <= 1080):
return [link.get('href'), ".mp4", resolutionr + "p", ""]
# openload
self.captch_check_plus_server_preference(episode_page, "&s=openload")
currentpage = self.driver.page_source
soup = BeautifulSoup(currentpage, 'html.parser')
time.sleep(2) # delay for page render
for link in soup.find_all('a', string="CLICK HERE TO DOWNLOAD"):
openload_link = link.get('href')
try:
video_src = self.resolve_url_9xbuddy(openload_link)
video_url = video_src[0]
video_format = video_src[1]
quality = ""
#headers
header_for_download = {'Referer': self.driver.current_url}
return [video_url, "." + video_format, quality, header_for_download]
except:
pass
#beta server
try:
self.captch_check_plus_server_preference(episode_page, "&s=beta")
currentpage = self.driver.page_source
soup = BeautifulSoup(currentpage, 'html.parser')
time.sleep(2) # delay for page render
link = soup.find('div', {'id': 'divContentVideo'}).find('video').get("src")
quality = soup.find('select', {'id': 'slcQualix'}).find('option', {'selected': True}).text
header_for_download = {'Referer':self.driver.current_url}
print("beta", link, quality)
video_url = link
video_format = ".mp4"
return [video_url, video_format, quality, header_for_download]
except:
pass
# rapidvideo
try:
self.captch_check_plus_server_preference(episode_page, "&s=rapidvideo")
currentpage = self.driver.page_source
soup = BeautifulSoup(currentpage, 'html.parser')
time.sleep(2) # delay for page render
# link = soup.find('div', {'id': 'divContentVideo'}).find('iframe').get("src")
link = soup.find('div', {'id': 'divContentVideo'}).find('iframe').get("src")
header_for_download = {'Referer': self.driver.current_url}
video_src = self.resolve_url_9xbuddy(link)
quality = ""
video_url = video_src[0]
video_format = video_src[1]
return [video_url, video_format, quality, header_for_download]
except Exception as e:
print(e)
return ["false", "", "", ""]
def resolve_url_9xbuddy(self, url):
self.driver.get("https://9xbud.com/" + url)
print("Page loading please wait")
time.sleep(15) # wait time for page to load
currentpage = self.driver.page_source
openload_soup = BeautifulSoup(currentpage, 'html.parser')
video_url = openload_soup.find('a', text='Download Now').get('href')
# video format
result = openload_soup.find_all('li', {'class': 'link-list'})
soup2 = BeautifulSoup(str(result[1]), 'html.parser')
video_format = soup2.find('li', {'class': 'link-format'}).text
return [video_url, video_format]
def captch_check_plus_server_preference(self, episode_page, server_preference):
x = True
while x:
try:
page = self.driver.get(episode_page + server_preference)
url = self.driver.current_url
if "Special/AreYouHuman" in str(url):
utils.log("Captcha " + str(self.driver.current_url))
# webbrowser.open(self.driver.current_url)
while ("Special/AreYouHuman" in str(self.driver.current_url)):
time.sleep(1)
# episode=episode - 1
else:
x = False
except Exception as e:
utils.slog(e)
utils.log("Timeout [" + str(episode_page) + "] Retrying...")
time.sleep(5)
def frange(self, start, stop, step):
try:
i=start
while i < stop:
yield i
i += step
except Exception as e:
utils.slog(e)
utils.log("=E Frange")
def zpad(self, val, n):
try:
bits=val.split('.')
return "%s.%s" % (bits[0].zfill(n), bits[1])
except Exception as e:
utils.slog(e)
utils.log("=E Zpad")
def download(self, p):
global count
global prefix
global downloading
global episode_list
global complete_dir
global download_threads
file_list=[]
episode_list=[]
ecount=0
downloading=0
if(int(p[5]) > 0):
epcount=int(p[5]) - 1 # temp folder
else:
epcount=0
if(downloading == 0):
downloading=1
for i in range(int(download_threads)): # start downloader, wait for files
params=""
t=KissDownloader(params, queue)
t.setDaemon(True)
t.start()
for infile in glob.glob(p[7] + "/*.mp4"):
infile=infile.replace(p[7], "")
infile=re.sub(r'.*_-_', '', infile)
infile=infile[:3]
if(infile.find('-')):
infile=str(infile).replace("-","")
if(infile[-1:] == "."):
infile=infile.replace(".","")
file_list.append(float(infile))
file_list.append(float(infile)+1)
elif(int(infile)):
file_list.append(int(infile))
if file_list:
if(int(max(file_list))):
if(len(file_list) < int(max(file_list)) and p[5] == 0):
utils.log("Downloaded episode " + str(max(file_list)) + ", filecount " + str(len(file_list)))
utils.log("Recheck from 0")
epcount=p[5]
else:
epcount=int(max(file_list)) + 1
if(p[4] and int(p[9]) == 0):
maxretrieve=int(p[4]) + 1
elif(int(p[9]) > 0):
maxretrieve=int(p[9]) + 1
if(int(ecount) < maxretrieve):
extension=webdriver.ChromeOptions()
extension.add_extension(ublock_origin)
extension.add_argument('--dns-prefetch-disable')
#prefs = {"profile.managed_default_content_settings.images":2}
#extension.add_experimental_option("prefs",prefs)
if platform.system() == "Mac" or platform.system() == "Linux":
self.driver=webdriver.Chrome(chrome_options=extension)
else:
self.driver=webdriver.Chrome(chromedriver, chrome_options=extension)
self.driver.set_page_load_timeout(50)
l = True
while l:
l=self.login(p[0], p[1], p[8])
if not l:
utils.log("=E Login failed... try again")
else:
l = False
k=True
while k:
try:
self.driver.get(p[3])
time.sleep(3)
self.rootPage=self.driver.page_source
k=False
except Exception as e:
utils.slog(e)
utils.log("=E Loading page")
time.sleep(2)
if (int(ecount) < (int(p[4]) + 1)):
utils.log("Retrieve from " + str(epcount)) # + " of " + str(p[4])
for e in self.frange(float(epcount), int(maxretrieve), 0.5):
if(int(ecount) < int(download_threads) * 3 and int(ecount) < int(maxretrieve)):
time.sleep(8)
page=self.get_episode_page(e, p[8])
if page[0] == "":
print('Unable to find episode ' + str(e) + '...')
pass
else:
video=self.get_video_src(page[0], p[10])
#video = [URL, file_extension, video_res, url_header(for beta server)
KA=""
try:
if prefix:
prefix2=p[6] + prefix
KA="_KA"
except:
prefix2=""
if (video[0] != 'false'):
if e % 1 == 0:
e=self.zpad(str(e), 3).replace(".0", "")
try: # hyphen seporator
varxx=page[0].split('?id=', 1)[0]
number7=sum(c.isdigit() for c in varxx[-7:])
if(number7 == 6 and "-" in varxx[-7:]):
e=str(varxx[-7:]).zfill(2)
number5=sum(c.isdigit() for c in varxx[-5:])
if(number5 == 4 and "-" in varxx[-5:]):
e=str(varxx[-5:]).zfill(2)
except Exception as e:
utils.slog(e)
utils.log("=E Hyphen")
if page[1]: # uncensored
filename=prefix2 + p[2] + "_-_" + str(e) + "_" + video[2] + "_BD" + KA + video[1]
else:
filename=prefix2 + p[2] + "_-_" + str(e) + "_" + video[2] + KA + video[1]
episode_list.append((video[0], filename, p[7], e, video[3]))
ecount += 1
print("Resolved [" + str(filename) + "]")
utils.log("Resolved [" + str(filename) + "]")
queue.put(video[0]) # append video url to queue
else:
utils.log("=E Retrieve failed [" + str(e) + "]")
else:
utils.log("Queue limit reached (" + str(int(download_threads) * 3) + ")")
break
else:
utils.log("Retrieved episode limit (" + str(int(maxretrieve) - 1) + ")")
self.driver.close()
# DownloadGUI Initiate
try:
last_count=9001
while(count > 0):
time.sleep(5) # allow downloads to start
t=threading.Thread(target=KissDownloader.download_message())
t.daemon=True
t.start()
if(int(count) != int(last_count) and int(count) < int(download_threads)):
if(int(last_count) > 9000):
last_count=count
else:
utils.log("> " + str(count) + " remain")
last_count=count
except KeyboardInterrupt:
utils.log("=E Keyboard interrupt") # TODO proper thread exit logic
sys.exit()
except Exception as e:
utils.slog(e)
utils.log("=E Exception downloading")
if(episode_list):
try:
os.remove(dir_path + "/resolved.csv.trash")
except FileNotFoundError as e:
pass
os.rename(dir_path + "/resolved.csv", dir_path + "/resolved.csv.trash")
KissDownloader.init()
else:
utils.log("Download finished!")
finaldestination=p[7]
utils.log(finaldestination)
if(complete_dir): # move *.mp4 to complete_dir
file_count=[]
for infile in glob.glob(p[7] + "/*.mp4"):
file_count.append(infile)
utils.log(str(len(file_count)) + "/" + str(p[4]))
if(len(file_count) >= int(p[4])-1):
for files in os.listdir(finaldestination):
if files.endswith('.mp4'):
os.rename(os.path.join(finaldestination, files), os.path.join(complete_dir, files))
try:
os.rmdir(finaldestination)
except Exception as e:
utils.slog(e)
utils.log("Folder delete failed")
else:
if(len(file_count) <= 1):
utils.log("=E Download failed!")
os.rmdir(finaldestination)
else:
utils.log("=E Invalid filecount!")
os.remove(dir_path + "/resolved.csv")
os.rename(dir_path + "/resolved2.csv", dir_path + "/resolved.csv")
KissDownloader.init()
def read_config():
global destination_folder
resolvednew = Path(dir_path + "/resolved2.csv")
if resolvednew.is_file():
os.remove(dir_path + "/resolved2.csv")
resolved = Path(dir_path + "/resolved.csv")
if not resolved.is_file():
utils.log('=E No series queued to download!')
sys.exit()
reader=csv.reader(open(dir_path + "/resolved.csv", "r"), delimiter=",")
newfile=open(dir_path + "/resolved2.csv", "a")
writer=csv.writer(newfile)
br=0
for row in reader:
try:
if row:
if(br == 0):
br = 1
if row[0]:
title=row[0]
else:
utils.log("=E Reading title")
sys.exit()
if row[1]:
url=row[1]
else:
utils.log("=E Reading url")
sys.exit()
if row[2]:
episode_count=row[2]
else:
utils.log("=E Reading episode_count")
sys.exit()
mal=row[3]
if(int(row[4]) > 0):
episode_min=int(row[4])+1
utils.log("Minimum episode set to " + str(int(episode_min)-1))
else:
episode_min=int(0)
if(int(row[5])):
episode_max=row[5]
utils.log("Maximum episode set to " + str(episode_max))
else:
episode_max=int(0)
if(int(row[6]) >= 0 and int(row[6]) <= 1080):
if(int(row[6]) >= 360 and int(row[6]) != 1080):
utils.log("Resolution limited to " + str(row[6]) + "p")
resolution=row[6]
else:
resolution=0
#break
else:
#print(row)
writer.writerows([row])
except IndexError as e:
utils.slog(e)
utils.log("=E Reading index")
except Exception as e:
utils.slog(e)
utils.log("=E Reading resolved.csv")
# writer.writerows([newrow])
newfile.close()
try:
if not title:
utils.log("Complete!")
sys.exit()
except:
utils.log("Complete!")
sys.exit()
try:
for k, v in {'&&': '', '&': '_and_', "'s": 's'}.items(): # replace
title=title.replace(k, v)
title=re.sub(r'[^a-zA-Z0-9\[\]]', '_', title) # alphanumeric only
title=re.sub('_+', '_', title) # replace multiple _
title=title.rstrip('_') # remove last underscore
except Exception as e:
utils.slog(e)
utils.log("=E Renaming title")
sys.exit()
utils.log('Initiate... [' + str(title) + ']')
if not destination_folder:
destination_folder=str(dir_path)
if not destination_folder.endswith('/'):
destination=str(destination_folder) + "/" + title + "/"
if not os.path.exists(destination_folder + "/" + title):
os.mkdir(destination_folder + "/" + title)
if not os.path.exists(destination_folder + "/" + title + "/temp"):
os.mkdir(destination_folder + "/" + title + "/temp")
website='{uri.scheme}://{uri.netloc}/'.format(uri=urlparse(url))
if website.endswith('/'):
website=website[:-1]
try:
return website, username, userpassword, title, url, mal, episode_min, episode_count, destination_folder, episode_max, resolution
except UnboundLocalError as e:
utils.slog(e)
utils.log("=E Reading resolved.csv in " + dir_path)
sys.exit()
except Exception as e:
utils.slog(e)
utils.log("=E Exception reading resolved.csv")
sys.exit()
def run_download(self):
if self[8] == "":
if not os.path.exists(dir_path + "/downloads"):
os.mkdir(dir_path + "/downloads")
destination=dir_path + "/downloads"
else:
destination_folderx=self[8].replace("\\", "/")
destination=str(destination_folderx) + str(self[3]) + "/"
if not destination_folderx.endswith('/'):
destination=str(destination_folderx) + "/" + str(self[3]) + "/"
params=[self[1], self[2], self[3], self[4], self[5], self[6], self[7], destination, self[0], self[9], self[10]]
# print(params)
KissDownloader(params, queue)
def init():
try: # check network connection
socket.create_connection(("www.google.com", 80))
except OSError:
utils.log("=E Network connection failed")
sys.exit()
# 0 website, 1 username,2 userpassword, 3 title, 4 url, 5 mal, 6 episode_min, 7 episode_count, 8 destination, 9 episode_max, 10 resolution
website, username, userpassword, title, url, mal, episode_min, episode_count, destination_folder, episode_max, resolution=KissDownloader.read_config()
KissDownloader.run_download([website, username, userpassword, title, url, mal, episode_min, episode_count, destination_folder, episode_max, resolution])
episodes_list=[]
for tup in episodes_list:
url=tup[0]
filename=tup[1]
destination=tup[2]
if __name__ == "__main__":
KissDownloader
KissDownloader.init()