-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathauto_pahe.py
executable file
·669 lines (457 loc) · 19.5 KB
/
auto_pahe.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
#! /usr/bin/python3
import time,argparse,os,sys
from pathlib import Path
import sys
import logging
from json import loads,load,dump,dumps
from bs4 import BeautifulSoup
from kwikdown import kwik_download
from manager import process_record,load_database,print_all_records,search_record
from execution_tracker import log_execution_time, reset_run_count, get_execution_stats
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as chrome_service
from selenium.webdriver.chrome.service import Service as ff_service
import concurrent.futures as concur
########################################### GLOBAL VARIABLES ######################################
# Download path
system_name = sys.platform.lower()
if system_name == "win32":
DOWNLOADS = Path.home() / "Downloads"
elif system_name == "darwin": # macOS
DOWNLOADS = Path.home() / "Downloads"
elif system_name == "linux":
DOWNLOADS = Path.home() / "Downloads"
else:
raise Exception("An Error Occurred, Unsupported Operating System")
exit()
########################################### LOGGING ################################################
logging.basicConfig(
level=logging.INFO, # Set the minimum log level
format='\n%(asctime)s - %(levelname)s - %(message)s', # Log message format
handlers=[
logging.StreamHandler(), # Output to console
logging.FileHandler('autopahe.log') # Output to a file
]
)
#######################################################################################################################
#Record list
records = []
############################################ BROWSER HANDLING ##########################################################
def browser(choice="firefox"):
chrome_guess = ["chrome", "Chrome", "google chrome", "google"]
ff_guess = ["ff", "firefox", "ffgui", "ffox", "fire"]
if choice.lower() in chrome_guess:
chserv = chrome_service("/snap/bin/geckodriver")
driver = webdriver.Chrome(service=chserv)
logging.info("Using Chrome browser")
elif choice.lower() in ff_guess:
ffserv = ff_service("/snap/bin/geckodriver")
options = webdriver.FirefoxOptions()
options.add_argument("--headless")
driver = webdriver.Firefox(service=ffserv, options=options)
logging.info("Using Firefox browser in headless mode\n")
else:
logging.error("Unsupported browser choice")
return 0
return driver
###############################################################################################
def data_report(data:dict,filepath = "autopahe_data.json",):
if data:
# Read existing JSON data from file
if os.path.exists(filepath):
with open(filepath, 'r') as json_file:
existing_data = load(json_file)
else:
open(filepath,"x")
existing_data={}
new_data = {**existing_data,**data}
with open(filepath,"w") as st:
dump(new_data,st,indent=4)
def driver_output(url:str,driver = False,content = False,json = False, wait_time = 10):
if driver == True :
driver = browser()
try:
driver.get(url)
except:
print("Selenium crashed while getting this page, please check ur internet connection")
driver.quit()
exit()
driver.refresh()
# Wait for the page to reload
driver.implicitly_wait(wait_time) # Adjust the timeout as needed
if content:
# Get page source after reloading
page_source = driver.page_source
driver.quit()
return page_source
elif json == True:
# Get the json response again after reloading
json_data = driver.execute_script("return document.body.textContent;")
driver.quit()
return json_data
else:
logging.error("Invalid arguments for driver_output")
print("Use the content( arg to get page_content or the json arg to get json response ")
exit()
current_system_os = str(sys.platform) #get current os
class Banners():
def header():
os.system('cls' if current_system_os.lower() == 'windows' else 'clear' )
print('''
db 8 8 88888 .d88b. 888b. db 8 8 8888
dPYb 8 8 8 8P Y8 ____ 8 .8 dPYb 8www8 8www
dPwwYb 8b..d8 8 8b d8 8wwP' dPwwYb 8 8 8
dP Yb `Y88P' 8 `Y88P' 8 dP Yb 8 8 8888
''')
def search(anime = None):
print(f'''
----------------------------------------
Searching for {anime}..
----------------------------------------
''')
def downloading(anime = None,eps = None):
print(f'''
----------------------------------------
Downloading episode {eps} of {anime}..
----------------------------------------
''')
def select(anime,eps = None,year = None,
atype = None,img = None,status = None):
print(f'''
----------------------------------------
Selected {anime}..
----------------------------------------
- Episodes Available : {eps}
- Year released : {year}
- Type of anime (TV or Movie) : {atype}
- Cover image : {img}
- Status : {status}
''')
def anime_info(anim = None , abt = None):
print(f'''
----------------------------------------
ABOUT {anim}
----------------------------------------
{abt}
''')
def i_info(summary = None):
print(f'''
----------------------------------------
Summary on Anime
----------------------------------------
{summary}
''')
# Banner Header
Banners.header()
def n():
print()
n()
# ============================ The requests argument handler function===============================
# TEXT wrap decorator
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
# =======================================================================================================
def lookup(arg):
global search_response_dict
# Search banner
Banners.search(arg)
n()
# url pattern requested when anime is searched
animepahe_search_pattern = f'https://animepahe.ru/api?m=search&q={arg}'
try:
search_response = driver_output(animepahe_search_pattern,driver=True,json=True)
except:
search_response = driver_output(animepahe_search_pattern,driver=True,json=True, wait_time = 30)
# print(search_response)
#return if no anime found
if not search_response:
Banners.header()
logging.info("No matching anime found. Retry!")
return
# 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'])
n()
print(f'{resultlen} results were found ---> ')
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']
year = search_response_dict['data'][el]['year']
print(f'''
[{el}] : {name}
---------------------------------------------------------
Number of episodes contained : {episodenum}
Current status of the anime : {status}
Year the anime aired : {year}
''')
n()
return search_response_dict
# =========================================== handling the single download utility ============================
def index(arg):
n()
global jsonpage_dict,session_id,animepicked,episode_page_format
animepicked = search_response_dict['data'][arg]['title']
#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'
try:
jsonpage_dict = loads(driver_output(anime_url_format,driver=True,json=True))
except:
jsonpage_dict = loads(driver_output(anime_url_format,driver=True,json=True, wait_time = 30))
episto = jsonpage_dict['total']
year = search_response_dict['data'][arg]['year']
type = search_response_dict['data'][arg]['type']
image = search_response_dict['data'][arg]['poster']
stat = search_response_dict['data'][arg]['status']
Banners.select(animepicked,eps=episto,year=year,
atype = type,img = image,status=stat)
final_data = {**search_response_dict,**jsonpage_dict}
data_report(data=final_data)
n()
def about():
#extract the anime info from a div with class anime-synopsis
ep_page = driver_output(episode_page_format,driver=True,content=True)
soup = BeautifulSoup(ep_page,'lxml')
abt = soup.select('.anime-synopsis')
return abt[0].text.strip()
def download(arg = 1):
# 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
arg = int(arg)
#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}'
# print(stream_page_url)
# get steampage
driver = browser()
driver.get(stream_page_url)
time.sleep(15)
stream_page_soup = BeautifulSoup(driver.page_source,'lxml')
dload = stream_page_soup.find_all('a',class_='dropdown-item',target="_blank")
# print (dload)
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 not (search(r'(360p|1080p|eng)', 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
# print(linkpahe)
# print(stream_page_soup)
driver.get(f"{linkpahe[0]}")
# driver.implicitly_wait(10)
time.sleep(10)
kwik_page = driver.page_source
driver.quit()
#getting the link to the kwik download page
kwik_cx=BeautifulSoup(kwik_page,'lxml')
# print(kwik_cx)
#getting kwik.cx f download link
kwik = kwik_cx.find('a', class_='redirect')['href']
# print(f"Download link => {kwik}")
Banners.downloading(animepicked,arg)
kwik_download(url=kwik, dpath=DOWNLOADS, ep=arg, animename = animepicked)
def multi_download(eps):
eps = str(eps)
# given arg specifies '-' for range
# New format for list comprehension
episodes = [
num
for segment in eps.split(",")
for num in (
range(int(segment.split("-")[0]), int(segment.split("-")[1]) + 1)
if "-" in segment
else [int(segment)]
)]
Banners.downloading(animepicked,eps)
with concur.ThreadPoolExecutor() as executor:
executor.map(download, episodes)
# ----------------------------------------------End of All the Argument Handling----------------------------------------------------------
#
def interactive_main():
# Selecting browser of choice
choice = str(input("Enter your favorite browser [e.g chrome] >> "))
# Search prompt for search function
lookup_anime = str(input("\nSearch an anime [e.g 'one piece'] >> "))
# searching anime with the lookup function
lookup(lookup_anime)
# selection prompt for the anime search
select_index = int(input("Select anime index [default : 0] >> "))
# handling the selected anime metadata
index(select_index)
# summary info on the selected anime
info = about()
Banners.i_info(info)
# Handling episode to download prompt
download_type = str(input("""
Enter the type of download facility you want:
1. s or single_download for single episode download
2. md or multi_download for multi episode download
3. v or md_verbose for a verbose variant of the md function [SLOW]
4. i for more in-depth info on the options above
>> """))
ep_to_download = (input("Enter episode(s) to download >> "))
switch = {
"1": download,
"2": multi_download,
"4": info,
's': download,
'md': multi_download,
'i': info,
'single_download': download,
'multi_download': multi_download,
'info': info
}
# initiating the action for the choice
selected_function = switch.get(download_type)
if selected_function:
selected_function(ep_to_download)
else:
print("Invalid input. Please select a valid option.")
def command_main(args):
global barg
barg = args.browser
sarg = args.search
iarg = args.index
sdarg = args.single_download
mdarg = args.multi_download
abtarg = args.about
rarg = args.record
dtarg = args.execution_data # New argument for retrieving stats by date
# Reset the run count
reset_run_count()
# Init browser
if barg:
browser(barg)
# Search function
if sarg:
records.append(sarg)
lookup(sarg)
# Index function
if iarg is not None:
records.append(search_response_dict['data'][iarg])
process_record(records)
index(iarg)
# About function
if abtarg:
info = about()
records.append(info)
process_record(records, update=True)
Banners.anime_info(animepicked, info)
# Single Download function
if sdarg:
records.append(sdarg)
process_record(records, update=True)
download(sdarg)
# Multi Download function
if mdarg:
records.append(mdarg)
process_record(records, update=True)
multi_download(mdarg)
# Record argument
if rarg:
if rarg == "view":
print_all_records()
elif rarg.isdigit():
position = int(rarg)
database = load_database()
if str(position) in database:
print(dumps(database[str(position)], indent=4))
else:
logging.info(f"No record found at position {position}")
else:
results = search_record(rarg)
if results:
print(dumps(results, indent=4))
else:
print("No matching records found.")
# Date argument to retrieve execution stats
if dtarg:
stats = get_execution_stats(dtarg)
# print(len(stats))
# print(stats.keys())
if len(stats) == 1:
for stat in stats:
# Convert total time from seconds to minutes
total_time_minutes = stats[stat]['total_time_mins']
total_time_hours = stats[stat]['total_time_hours']
average_time_mins = stats[stat]['average_time_mins']
average_time_hours = stats[stat]['average_time_hours']
print(f"\nExecution stat for '{dtarg}' -->>\n")
print(f"\n1.) Total Runs: {stats[stat]['run_count']}")
print(f"\n2.) Total Execution Time (Minutes): {total_time_minutes:.2f} minutes") # Print in minutes
print(f"\n3.) Total Execution Time (Minutes): {total_time_hours:.2f} hours") # Print in hours
print(f"\n4.) Average Execution Time (Minutes): {average_time_mins:.2f} minutes") # Print in minutes
print(f"\n5.) Average Execution Time (Hours): {average_time_hours:.2f} hours") # Print in hours
elif len(stats) > 1:
for stat in stats:
# Convert total time from seconds to minutes
total_time_minutes = stats[stat]['total_time_mins']
total_time_hours = stats[stat]['total_time_hours']
average_time_mins = stats[stat]['average_time_mins']
average_time_hours = stats[stat]['average_time_hours']
print(f"\n\nExecution stats for '{stat}' -->>")
print(f"\n1.) Total Runs: {stats[stat]['run_count']}")
print(f"\n2.) Total Execution Time (Minutes): {total_time_minutes:.2f} minutes") # Print in minutes
print(f"\n3.) Total Execution Time (Minutes): {total_time_hours:.2f} hours") # Print in hours
print(f"\n4.) Average Execution Time (Minutes): {average_time_mins:.2f} minutes") # Print in minutes
print(f"\n5.) Average Execution Time (Hours): {average_time_hours:.2f} hours") # Print in hours
print("=============================================================================")
else:
print(f"\n\nNo execution data found for '{dtarg}'.")
def main():
# Reset the run count
reset_run_count()
start_time = time.perf_counter()
parser = argparse.ArgumentParser()
parser.add_argument('-b', '--browser', help='Select the desired browser (chrome or firefox). Default is chrome.')
parser.add_argument('-s', '--search', type=str, help='Search for an anime by name.')
parser.add_argument('-i', '--index', type=int, help='Specify the index of the desired anime from the search results.')
parser.add_argument('-d', '--single_download', type=int, help='Download a single episode of an anime.')
parser.add_argument('-md', '--multi_download', help='Download multiple episodes of an anime.')
parser.add_argument('-a', '--about', help='Output an overview of the anime', action='store_true')
parser.add_argument('-r', '--record', help='Interact with the records/database (view, [index], [keyword]).')
# Adding help message for exec_data
parser.add_argument(
'-dt', '--execution_data',
help=(
"Retrieve execution data for a specific date or date range. "
"Format : YYYY-MM-DD (year-month-day)"
"Examples: ["
"'today', 'yesterday', 'last 3 days', 'last week', "
"'this week', '2 weeks ago', 'last month', '1 month ago'.]"
)
)
args = parser.parse_args()
if any(vars(args).values()):
command_main(args)
else:
interactive_main()
# Log execution time
log_execution_time(start_time)
if __name__ == '__main__':
main()
else:
Banners.header()