-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchanime.py
executable file
·594 lines (365 loc) · 17.3 KB
/
fetchanime.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
#! /usr/bin/python3
import sys,time,requests,argparse,os,sys
from json import loads
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.firefox import GeckoDriverManager
from webdriver_manager.core.utils import ChromeType
start = time.perf_counter()
def spinning_cursor():
while True:
for cursor in '|/-\\':
yield cursor
def spinner(secs):
spinner = spinning_cursor()
for _ in range(secs):
sys.stdout.write(next(spinner))
sys.stdout.flush()
time.sleep(0.1)
sys.stdout.write('\b')
start = time.perf_counter()
def n():
print()
n()
n()
# ============================ The requests argument handler function===============================
def box_text(func):
def wrapper():
box_width = 70
padding = (box_width - len(func())) // 2
print('*' * box_width)
print('*' + ' ' * padding + func() + ' ' * (box_width - len(func()) - padding) + '*')
print('*' * box_width)
return wrapper
@box_text
def print_result():
return " ANIME FETCH API "
print_result()
def browsarg():
global driver
if barg == "chrome":
serv = Service(executable_path=ChromeDriverManager().install())
option = webdriver.ChromeOptions()
option.headless = False
option.add_experimental_option("detach", False)
driver = webdriver.Chrome(service = serv,options=option)
elif barg == "ffgui":
options = Options()
options.headless = False
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install(),options=options)
elif barg == "firefox":
options = Options()
options.headless = True
driver = webdriver.Firefox(executable_path=GeckoDriverManager().install(),options=options)
elif barg == "brave":
serv = Service(ChromeDriverManager().install())
options = Options()
options.headless = False
driver = webdriver.Chrome(service=Service(ChromeDriverManager(chrome_type=ChromeType.BRAVE).install()))
else:
pass
# =======================================================================================================
def searcharg(arg):
print()
print(f'You searched for {arg}')
n()
print('Searching....')
n()
# url pattern requested when anime is searched
animepahe_search_pattern = f'https://animepahe.com/api?m=search&q={arg}'
global search_response_dict
search_response = requests.get(animepahe_search_pattern).text
# converting response json data to python dictionary for operation
search_response_dict = loads(search_response)
# all animepahe has a session url and the url will be https://animepahe.com/anime/[then the session id]
resultlen = len(search_response_dict['data'])
print(f'{resultlen} results were found and are as follows --> ')
n()
for el in range(len(search_response_dict['data'])):
name = search_response_dict['data'][el]['title']
episodenum = search_response_dict['data'][el]['episodes']
status = search_response_dict['data'][el]['status']
session = search_response_dict['data'][el]['session']
print('------'*10)
print(f'Name of Anime : {name}')
n()
print(f'Number of episodes contained : {episodenum}')
n()
print(f'Current status of the anime : {status}')
n()
print(f'session id of the anime : {session}')
n()
print(f'Index of anime : {el}')
print('------'*10)
n()
n()
n()
n()
def search_hidden(arg):
print()
print(f'You searched for {arg}')
n()
print('Searching....')
n()
# url pattern requested when anime is searched
animepahe_search_pattern = f'https://animepahe.com/api?m=search&q={arg}'
global search_response_dict
# converting response json data to python dictionary for operation
search_response_dict = loads(requests.get(animepahe_search_pattern).text)
# all animepahe has a session url and the url will be https://animepahe.com/anime/[then the session id]
# =========================================== handling the single download utility ========================
def indexarg(arg):
n()
global jsonpage_dict,episto,episode_page_format,epno,session_id,animepicked
animepicked = search_response_dict['data'][arg]['title']
epno = search_response_dict['data'][arg]['episodes']
print(f'Anime Name : {animepicked}')
n()
#session id of the whole session with the anime
session_id = search_response_dict['data'][arg]['session']
# anime episode page url format and url
episode_page_format = f'https://animepahe.com/anime/{session_id}'
# now the anime_json_data url format
anime_url_format = f'https://animepahe.com/api?m=release&id={session_id}&sort=episode_asc&page=1'
jsonpage_dict = loads(requests.get(anime_url_format).text)
episto = jsonpage_dict['total']
print(f'No of Episodes : {episto}')
n()
@box_text
def aboutarg():
#extract the anime info from a div with class anime-synopsis
soup = BeautifulSoup((requests.get(episode_page_format).text),'lxml')
abt = soup.select('.anime-synopsis')
return abt[0].text.strip()
def singledownarg(arg):
# using return value of the search function to get the page
# using the json data from the page url to get page where the episodes to watch are
print()
print(f'Number of episode contained is : {epno}')
#session string of the stream episode
episode_session = jsonpage_dict['data'][arg-1]['session']
#stream page url format
stream_page_url = f'https://animepahe.com/play/{session_id}/{episode_session}'
n()
stream_page_soup = BeautifulSoup(requests.get(f'{stream_page_url}').content,'lxml')
dload = stream_page_soup.find_all('a',class_='dropdown-item',target="_blank")
from re import search
# for link in dload:
# stlink = str(link)
# match = re.search(r'720p', stlink)
# if match:
# for link in stlink:
# soup = BeautifulSoup(link, 'html.parser')
# href = soup.a['href']
# print(href)
# i am sure u are not wise enough to know what is going on
#but the code above was shortened to the code below
#using walrus operator and list comprehension
#and it return a list of chars which when combined will return the link
linkpahe = [(href:=BeautifulSoup(stlink, 'html.parser').a['href']) for link in dload if (match:=search(r'720p', stlink:=str(link)))]
#the linkpahe variable carries a list of the characters of the link
#so the pahewin variable will return the link webpage content
pahe_win = requests.get(f'{[ch for ch in linkpahe][0]}').content
#getting the link to the kwik download page
kwik = (pahe_soup:=BeautifulSoup(pahe_win,'lxml')).find('a', class_='redirect')['href']
# ---------------------------Using selenium to render and clickk the download button------------------------
# going to the download page
driver.get(kwik)
WebDriverWait(driver, 15).until(EC.presence_of_element_located(
(By.XPATH, "//form[@method = 'POST']/button[contains(@class, 'button')]")))
element = driver.find_element(
By.XPATH, "//form[@method = 'POST']/button[contains(@class, 'button')]")
# the first click will click on the ad on the screeen
try:
ad=driver.find_element(By.XPATH, '/html/body/div[2]/a')
ad.click()
element.click()
# the second click will download the content
except:
element.click()
n()
print(f'Downloading Episode {arg} of {animepicked} NOW!!!!!!!')
def multi_download_verbose(arg):
print("To make efficient use of the multi download function,\nit is advised you have a very fast and stable internet connection")
n()
episodes_list = [int(x) for x in arg.split(',')]
for elem in episodes_list:
episode_session = jsonpage_dict['data'][elem]['session']
#stream page url format
stream_page_url = f'https://animepahe.com/play/{session_id}/{episode_session}'
n()
stream_page_soup = BeautifulSoup(requests.get(f'{stream_page_url}').content,'lxml')
dload = stream_page_soup.find_all('a',class_='dropdown-item',target="_blank")
from re import search
# for link in dload:
# stlink = str(link)
# match = re.search(r'720p', stlink)
# if match:
# for link in stlink:
# soup = BeautifulSoup(link, 'html.parser')
# href = soup.a['href']
# print(href)
# i am sure u are not wise enough to know what is going on
#but the code above was shortened to the code below
#using walrus operator and list comprehension
#and it return a list of chars which when combined will return the link
linkpahe = [(BeautifulSoup(stlink, 'html.parser').a['href']) for link in dload if (match:=search(r'720p', stlink:=str(link)))]
#the linkpahe variable carries a list of the characters of the link
#so the pahewin variable will return the link webpage content
pahe_win = requests.get(f'{[ch for ch in linkpahe][0]}').content
#getting the link to the kwik download page
kwik = (BeautifulSoup(pahe_win,'lxml')).find('a', class_='redirect')['href']
#--------------------------------------making a__init__ new tab to efficiently multithread the program---------------------
driver.execute_script("window.open('');")
driver.switch_to.window(driver.window_handles[-1])
# ---------------------------Using selenium to render and clickk the download button------------------------
# going to the download page
driver.get(kwik)
WebDriverWait(driver, 15).until(EC.presence_of_element_located(
(By.XPATH, "//form[@method = 'POST']/button[contains(@class, 'button')]")))
element = driver.find_element(
By.XPATH, "//form[@method = 'POST']/button[contains(@class, 'button')]")
try:
# the first click will click on the ad on the screeen
ad=driver.find_element(By.XPATH,"/html/body/div[2]/a")
ad.click()
# the second click will download the content
n()
except:
pass
n()
#to click the download btton
element.click()
#return to original window
driver.switch_to.window(driver.window_handles[0])
# already downloading
print(f'Downloading Episode {elem} of {animepicked} NOW!!!!!!!')
print(f'Already Aired episode : {episto}')
n()
# This function unlike the one above downloads (if there is stable and fast connection) anime concurrently
def multi_download_optimized(arg: str):
print("\n\nTo make efficient use of the multi download function,\nit is advised you have a very fast and stable internet connection\n")
print(f'Already aired Episodes : {episto}')
pahe_win_pages= [BeautifulSoup(requests.get(f"https://animepahe.com/play/{session_id}/{jsonpage_dict['data'][int(x)-1]['session']}").content,'lxml')for x in arg.split(',') ]
dload = [page.find_all('a',class_='dropdown-item',target="_blank") for page in pahe_win_pages]
from re import search
# for url in dload:
# for char in url:
# if (match := search(r'720p',str(char))):
# soup = BeautifulSoup(str(char),'lxml')
# href=soup.a['href']
# print(href)
n()
linkpahe = [BeautifulSoup(str(url),'lxml').a['href'] for link in dload for url in link if (search(r'720p', str(url)))]
pahewin_link = [(BeautifulSoup(requests.get(link).content,'lxml').find('a',class_ = "redirect"))['href'] for link in linkpahe ]
# print(pahewin_link)
for i,url in enumerate(pahewin_link):
# ---------------------------Using selenium to render and click the download button------------------------
# going to the download page
driver.get(url)
WebDriverWait(driver, 15).until(EC.presence_of_element_located(
(By.XPATH, "//form[@method = 'POST']/button[contains(@class, 'button')]")))
element = driver.find_element(
By.XPATH, "//form[@method = 'POST']/button[contains(@class, 'button')]")
try:
# the first click will click on the ad on the screeen
ad=driver.find_element(By.XPATH,"/html/body/div[2]/a")
ad.click()
# the second click will download the content
n()
except:
pass
n()
#to click the download btton
element.click()
# already downloading
elem = (arg.split(','))[i]
print(f'Downloading Episode {elem} of {animepicked} !!')
# ----------------------------------------------End of All the Argument Handling----------------------------------------------------------
finish = time.perf_counter()
n()
with open("speed_report.txt","a") as st:
st.write(f'finish time on {time.asctime()}: {(round(finish-start,2))-5}')
print(f'Finish time is {round(finish-start,2)}\n')
if __name__ == "__main__":
# ===================================== Handling Arguments and other involved function============================================
parser = argparse.ArgumentParser()
# Adding all the required arguments
parser.add_argument(
'-b', '--browser', help='To select the global desired browser either chrome or firefox. Also "ff" is for gui')
parser.add_argument(
'-s', '--search',type=str,
help='Specify the search keyword or Anime name e.g jujutsu kaisen.it returns a match of available anime related to the search word')
parser.add_argument(
'-sh', '--search_hidden', help='Less Verbose search function,should be used only the anime and index is known')
parser.add_argument('-i', '--index', type=int,
help='Specify the index of the desired anime from the search results')
parser.add_argument('-sd', '--single_download', type=int,
help='Used to download a single episode of an anime')
parser.add_argument('-md', '--multi_download_optimized', type=str,
help='Used to download multiple episodes of an anime in a faster way,a string of ints separated by commas[FASTER]')
parser.add_argument('-mdv', '--multi_download_verbose', type=str,
help='Used to download multiple episodes of an anime and show verbose,a string of ints separated by commas[SLOWER]')
parser.add_argument('-a', '--about',
help='Outputs an overview information on the anime',action='store_true')
args = parser.parse_args()
print(args.__dict__)
barg = args.browser
sarg = args.search
sharg = args.search_hidden
iarg = args.index
sdarg = args.single_download
mdarg = args.multi_download_optimized
mdvarg = args.multi_download_verbose
abtarg = args.about
autoarg = args.download_driver
# Browser Argument
if barg != None:
browsarg()
else:
pass
# Search Argument
if sarg != None:
searcharg(sarg)
elif type(sarg) == float:
print("The search argument does not accept a float value")
else:
pass
if bool(sharg) == True:
search_hidden(sharg)
elif type(sarg) == float:
print("The search argument does not accept a float value")
else:
pass
# Index Argument
if iarg != None:
indexarg(iarg)
else:
pass
#About Argument
if abtarg:
aboutarg()
else:
pass
# Single Download Argument
if sdarg != None:
singledownarg(sdarg)
else:
pass
# Multi Download Arguments(mdc,mdv,mdo or md)
if mdvarg != None:
multi_download_verbose(mdvarg)
else:
pass
# MD
if bool(mdarg) == True:
multi_download_optimized(mdarg)
else:
pass