-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRunGoGo.py
243 lines (232 loc) · 9.98 KB
/
RunGoGo.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
from processor import (
UrlFixer,
UrlSearch,
pagination_link,
Get_ID,
pretty_size,
validatename,
HlsObject,
pick_quality,
list_quality
)
from Lib import Goscraper, Prettify, EpisodeScraper, ConsumetAPI, GogoCDN
import version
from Varstorage import Configuration, Constants
from pathlib import Path
import time, os
from terminology import in_green
config = Configuration().load()
config.self_check()
#GlobalVar
chosen_quality_manual = None
def user_input(text, valid: list, msg="Enter valid variables"):
"Get a list of valid integers, add any on list to allow any input other than blank or whitespace"
user = None
while True:
user = input(text).strip()
try:
user = int(user)
except ValueError:
if "int" in valid:
print(msg)
continue
if user and "any" in valid:
return user
if user in valid:
return user
if isinstance(user, int) and "int" in valid:
return user
print(msg)
def Episode_UI(url, anime_title):
"Collect episodes and UI for bulk download"
goepisode = EpisodeScraper(url)
available_episodes = goepisode.get_episodes()
preprint = Prettify()
preprint.define_alignment(tabs=1)
preprint.add_tab()
preprint.add_line(f"Found {available_episodes} episodes!")
preprint.add_tab("Bulk downloader")
preprint()
starting_ep = user_input(text="Start from episode:", valid=[i+1 for i in range(available_episodes)], msg="Enter valid number")
ending_ep = user_input(text="End at episode:", valid=[i+1 for i in range(available_episodes)], msg="Enter valid number")
for i in range(starting_ep, ending_ep+1):
preprint = Prettify()
preprint.define_alignment(tabs=1)
preprint.add_tab()
preprint.add_line(f"Downloading Episode {i} / {ending_ep}")
preprint.add_tab()
preprint()
episode_link = goepisode.get_episode_link(i)
exit_code = Download_UI(episode_link, anime_title=anime_title, episode_number=i)
if exit_code == 2:
return exit_code
def Download_UI(url, anime_title, episode_number):
"Get download links and show download UI"
Path(Constants.download_folder).mkdir(parents=True, exist_ok=True) #Create Downloads folder
#===
preprint = Prettify()
preprint.define_alignment(tabs=1)
preprint.add_tab()
preprint.add_line("Getting m3u8 file...")
preprint.add_tab(char="-")
preprint()
video_id = Get_ID(url)
if config.video_source == config.valid_video_source[1]:
consumet = ConsumetAPI(base_url=config.get_consumet_api, video_id=video_id,source=config.get_consumet_video_server)
video_data = consumet.get_m3u8_files()
headers = consumet.get_referrer()
elif config.video_source == config.valid_video_source[0]:
gogocdn = GogoCDN(url)
video_data = gogocdn.get_streaming_data()
video_data = video_data.get_sources()
headers = gogocdn.get_referrer()
global chosen_quality_manual
if config.video_quality_mode == "manual" and not chosen_quality_manual:
qualities = list_quality(video_data)
prettyqual = Prettify()
prettyqual.define_alignment(tabs=1)
prettyqual.add_line("Manual Mode enabled")
prettyqual.add_tab(data="Available Quality",char="-")
for quality in qualities:
prettyqual.add_line(quality)
prettyqual.add_tab(char="-")
prettyqual()
quality = user_input("Select Quality:", valid=qualities, msg="Incorrect input")
chosen_quality_manual = quality
video = pick_quality(video_data, preferred_quality=quality, force=True)
elif chosen_quality_manual:
video = pick_quality(video_data, preferred_quality=chosen_quality_manual, force=True)
elif config.video_quality_mode == "auto":
video = pick_quality(video_data, preferred_quality=config.video_quality_preference)
print(f"Source: {config.video_source}")
if config.video_quality_mode != "manual": print(f"Preferred Quality: {config.video_quality_preference}")
print(f"Quality Selected: {video['quality']}")
if not video:
print("We are not able to find streamable media for this title")
return 1
#Download the file
file_name = validatename(f"{anime_title}_{video['quality']}_{episode_number}")
hls = HlsObject(m3u8_url=video['url'], headers=headers,file_name=file_name, download_location=os.path.join(Constants.download_folder, validatename(anime_title)))
hls.download() #Initiate download
print(f"Downloading: {anime_title}")
error_msg = ""
try:
while True:
hls.update_progress()
segment_done = hls.progress['progress']
segment_available = hls.segment_count
segment_errored = hls.progress['errored']
data_downloaded = hls.progress['file_size']
try:
percent_done = round(segment_done / segment_available * 100, 2)
except ZeroDivisionError:
percent_done = 0
if segment_errored:
error_msg = f"/Err:{segment_errored}"
print(in_green(f"=== Progress: [{segment_done}/{segment_available}]{error_msg} ** {percent_done}% - {pretty_size(data_downloaded)} ==="), end="\r")
if segment_done == segment_available:
print("\n Download successful!")
hls.arrange_files()
hls.cache_clear()
break
time.sleep(1)
except KeyboardInterrupt:
hls.close()
return 2
def Genre_UI():
"Show available genres on site"
gogo_page = Goscraper(config.get_host)
genre_list = gogo_page.get_genres()
preprint = Prettify()
preprint.define_alignment(tabs=1)
preprint.add_tab(data="Found Genres", lines=33)
valid = []
for num, genre_each in enumerate(genre_list):
preprint.add_sort(key=num+1, value=genre_each['genre-name'], separator=".")
valid.append(num+1)
preprint.add_tab(char='-',lines=33)
preprint()
selection = user_input("Select:", valid=valid)
new_url = UrlFixer(config.get_host, genre_list[selection-1]['flair'])
print("\tLoading (restart if it took >10s)")
return Home_UI(host=new_url)
def Home_UI(host):
'Display main results'
gogo_page = Goscraper(host)
if not gogo_page.get_result_count():
print("\tThere are no results found")
return 1
result_title = gogo_page.get_titles()
pagination = gogo_page.get_pagination()
preprint = Prettify()
preprint.define_alignment(tabs=1)
preprint.add_tab("Results",lines=33)
valid = []
for num, res_tile in enumerate(result_title):
preprint.add_sort(key=num+1, value=res_tile["title_name"], separator=".)")
if res_tile.get('episode'):
preprint.add_line(f"\t Episode: {res_tile['episode']}")
preprint.add_tab(char="-",lines=33)
valid.append(num+1)
[valid.append(each) for each in Constants.pagination_commands]
preprint.add_line(f"There are {gogo_page.get_result_count()} results found")
if pagination['page_total'] > 1:
preprint.add_line("To switch a page: << or >>")
preprint.add_line(f"Page: {pagination['page_on']}/{pagination['page_total']}")
preprint()
while True: #use loops to prevent halting of application for when pagination returns None
selection = user_input("Select:", valid=valid)
if selection in Constants.pagination_commands:
if selection == Constants.pagination_commands[0] or selection == Constants.pagination_commands[3]:
#Forwards
result = pagination_link(host, pagination['page_on'], pagination['page_total'], 'fwd')
elif selection == Constants.pagination_commands[1] or selection == Constants.pagination_commands[2]:
#Backwards
result = pagination_link(host, pagination['page_on'], pagination['page_total'], 'prv')
if result:
return Home_UI(result)
else:
new_url = UrlFixer(config.get_host, result_title[selection-1]['flair']) #Use base url for this
if res_tile.get('episode'):
return Download_UI(new_url, result_title[selection-1]['title_name'], result_title[selection-1]['episode'])#This needs to go straight to Downloader or Video Scraper
return Episode_UI(new_url, result_title[selection-1]['title_name'])
#Next step is get download link and or skip to episode download
def ResultZone(mode, value=None):
if mode == "Home":
return Home_UI(config.get_host)
elif mode == "Genre":
Genre_UI()
elif mode == "Search":
return Home_UI(UrlSearch(value))
def main():
preprint = Prettify()
preprint.define_alignment(tabs=1)
preprint.add_tab(lines=33)
preprint.define_alignment(tabs=1, spaces=5)
preprint.add_line(f"GoGoDownloader R2 v{version.__version__}")
preprint.define_alignment(tabs=1)
preprint.add_tab(lines=33)
preprint.add_sort(key="1",value="Search at Homepage", separator=".)")
preprint.add_sort(key="2",value="Search by Genres", separator=".)")
preprint.add_line("Or type in the title, to search")
preprint.add_tab(lines=33)
preprint()
selection = user_input("\tEnter Number/Search title:", [1,2,"any"])
print("\tLoading (restart if it took >10s)")
if selection == 1:
return ResultZone("Home")
elif selection == 2:
return ResultZone("Genre")
else:
return ResultZone("Search", value=selection)
def update_checker():
prettify = Prettify()
prettify.add_tab("Update Notice",lines=50, char='-')
version.init()
notification = version.show_update(prettify=prettify)
prettify.add_tab(lines=50, char='-')
if (notification):
prettify()
if __name__ == "__main__":
update_checker()
main()