-
Notifications
You must be signed in to change notification settings - Fork 17
/
animeX.py
277 lines (225 loc) · 9.86 KB
/
animeX.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
import os
import sys
import time
import urllib3
import requests
from bs4 import BeautifulSoup
def banner():
# App banner
banner_ascii = """
/$$$$$$ /$$ /$$ /$$
/$$__ $$ |__/ | $$ / $$
| $$ \ $$ /$$$$$$$ /$$ /$$$$$$/$$$$ /$$$$$$ | $$/ $$/
| $$$$$$$$| $$__ $$| $$| $$_ $$_ $$ /$$__ $$ \ $$$$/
| $$__ $$| $$ \ $$| $$| $$ \ $$ \ $$| $$$$$$$$ >$$ $$
| $$ | $$| $$ | $$| $$| $$ | $$ | $$| $$_____/ /$$/\ $$
| $$ | $$| $$ | $$| $$| $$ | $$ | $$| $$$$$$$| $$ \ $$
|__/ |__/|__/ |__/|__/|__/ |__/ |__/ \_______/|__/ |__/
"""
return banner_ascii
class BadLinkException(Exception):
def __init__(self, ok):
self.ok = ok
def name_parser(name):
new_name = ("]".join(name.split("]")[1:2]) + "]").strip()
if new_name in ["[RapidBot]", "[]"]:
new_name = os.path.basename(name)
return new_name
def get_search_result(search_item):
# search for a given anime using WP Rest API since cloudflare recaptcha can be a hassle
search_url = "https://www.animeout.xyz/wp-json/wp/v2/posts"
# set to firefox client
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
# search parameter
params = {
"search": search_item
}
# array of contexts searched from api
search_result = []
r = requests.get(search_url, params=params, headers=headers).json()
# loop through (& functions) each post found in json
for post in r:
post_title = post['title']['rendered']
# condition for a more relevant result. as per WP API response can be ambiguous
if search_item.split(' ', 1)[0].lower() in post_title.lower():
print(post_title)
search_result.append({
'name': post_title,
'raw-content': post['content']['rendered']
})
return search_result
def get_anime_episodes(anime_content):
# parse the anime content to html
anime_result = BeautifulSoup(anime_content, "html.parser")
episodes = []
for i in anime_result.findAll("a"):
try:
if i["href"][-3:] in ["mkv", "mp4]"]:
episodes.append(i["href"])
except:
pass
return episodes
def get_download_url(anime_url):
# get the video download URL
r = requests.get(anime_url)
pre_download_page = BeautifulSoup(r.text, "html.parser")
pre_download_url = pre_download_page.find("a", {"class": "btn"})["href"]
r = requests.get(pre_download_url)
download_page = BeautifulSoup(r.text, "html.parser")
# using a try catch because .text returned empty on some OS
try:
download_url = download_page.find(
"script", {"src": None}).text.split('"')[1]
except:
download_url = download_page.find(
"script", {"src": None}).contents[0].split('"')[1]
return download_url
def download_episode(anime_name, download_url, i=1):
# using urllib3 rather wget as wget seems quite redundant for mkv file download
http = urllib3.PoolManager()
# prevent eyesoring error printout
urllib3.disable_warnings()
# download anime and store in the folder the same name
# don't download files that exist and clear tmp files after download
filename = os.path.basename(download_url)
download_path = os.path.join(anime_name, filename)
if not os.path.exists(download_path):
# Due to the existence of multiple streams of download
# we prepare a download url with i as subdomain index variant
_url = download_url.replace(" ", "%20")
_url = "https://pub" + str(i) + ".animeout.com" + \
_url[_url.find('/series'):]
print("\nTrying " + _url + " ...")
try:
# send a download request with current url
r = http.request('GET', _url, preload_content=False)
if r.status == 404:
raise BadLinkException('bad link')
print('Gotten Verified Download link!')
print("Downloading", name_parser(filename))
# download if response of download url request is ok
with open(download_path, 'wb') as out:
while True:
data = r.read()
if not data:
break
out.write(data)
r.release_conn()
clear_tmp(anime_name)
except BadLinkException as e:
print(e)
n = i + 1
download_episode(anime_name, download_url, n)
def make_directory(anime_name):
# create folder to store anime
if not os.path.exists(anime_name):
os.mkdir(anime_name)
def clear_tmp(directory):
# clear tmp files
for i in os.listdir(directory):
if i[-3:] == "tmp":
os.remove(os.path.join(directory, i))
def check_update():
# check if there's a higher version of the app
commit_count = 48
repo_commit_count = len(requests.get(
"https://api.github.com/repos/LordGhostX/animeX-v2/commits?per_page=100").json())
if commit_count != repo_commit_count:
print("\nYou are using an outdated version of animeX. Please update from "
"https://github.com/LordGhostX/animeX-v2\n")
else:
print("\nYou're ready to go :)\n")
def get_user_choice(cap):
LIST_OF_ALLOWED_DIGITS = [d for d in "0123456789"]
choice = input("\nWhich one? Enter the number of your choice ::: ")
# ensure choice is not empty
if len(choice) == 0:
return get_user_choice(cap)
# separate each digit of the choice string
digits = [c for c in choice]
# look for any element that isn't a digit
for digit in digits:
if digit not in LIST_OF_ALLOWED_DIGITS:
print("Your input is invalid! pick another number")
return get_user_choice(cap)
if int(choice) > cap or int(choice) == 0:
print("Your input is invalid! pick another number")
return get_user_choice(cap)
return abs(int(
choice))
if __name__ == "__main__":
print(banner())
print("\nAll anime are gotten from www.animeout.xyz/")
check_update()
if len(sys.argv) == 2:
anime_name = sys.argv[1]
else:
anime_name = input("\nWhat anime do you wanna download today ::: ")
search_result = get_search_result(anime_name)
if len(search_result) == 0:
print(
"We couldn't find the anime you searched for, check the spelling and try again")
exit()
print("\nSearch results for", anime_name)
for i, j in enumerate(search_result):
print(i + 1, " - " + j["name"])
choice = get_user_choice( len(search_result) )
anime = search_result[choice - 1]
anime["name"] = "".join(
[i if i.isalnum() or i in [")", "(", " "] else "-" for i in anime["name"]])
# using the raw anime content rather than url since it contains all that is needed
episodes = get_anime_episodes(anime["raw-content"])
getall = input(
"\nanimeX found {} episodes for the requested anime\nDo you want to get all episodes? ::: (Y/N) ".format(len(episodes))).lower()
make_directory(anime["name"])
print("\nPress CTRL + C to cancel your download at any time")
splice_download = False
if getall in ['n', 'no']:
try:
options = int(input(
"\nWhat kind of action would you like to perform: \n1) Get latest episode \n2) See other download Options\nChoose an option ::: "))
except ValueError:
print("Invalid Entry, enter in '1' or '2'")
options = int(input(
"\nWhat kind of action would you like to perform: \n1) Get latest episode \n2) See other download Options\nChoose an option ::: "))
if options == 1:
print(
"Aye aye captain, downloading latest episode, hit CTRL + C to cancel anytime")
latest = episodes[-1]
download_url = get_download_url(latest)
download_episode(anime["name"], download_url)
elif options == 2:
for i, j in enumerate(episodes, 1):
print(i, name_parser(j))
episode_no = input(
"\nYou can choose an episode from the list above or specify a range of anime to download in the format start:end e.g 10:20 or a list seperated by comma e.g 1,5,7,10\nChoose episode number::: ")
if len(episode_no.split(":")) == 1:
if len(episode_no.split(",")) == 1:
download_url = get_download_url(
episodes[int(episode_no.split(":")[0]) - 1])
start = time.perf_counter()
download_episode(anime["name"], download_url)
end = time.perf_counter()
print(f'\ncompleted download in {int(end-start)} seconds')
else:
episode_list = [int(i) - 1 for i in episode_no.split(",")]
episodes = [j for i, j in enumerate(
episodes) if i in episode_list]
splice_download = True
else:
start_ep, end_ep = [int(i) for i in episode_no.split(":")[:2]]
episodes = episodes[start_ep - 1:end_ep]
splice_download = True
else:
print("Invalid Entry, enter in '1' or '2'")
options = int(input(
"\nWhat kind of action would you like to perform:\n1) Get latest episode\n2) See other Options\nChoose an option ::: "))
if getall in ['yes', 'y'] or splice_download:
start = time.perf_counter()
for i in episodes:
download_url = get_download_url(i)
download_episode(anime["name"], download_url)
end = time.perf_counter()
print(f'\ncompleted download in {int(end-start)} seconds')
print("\nFinished downloading all episodes of", anime["name"])