-
Notifications
You must be signed in to change notification settings - Fork 0
/
turboG.py
301 lines (242 loc) · 8.32 KB
/
turboG.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
"""
Author: Prince Addai Desmond @bubbs.sh
Date: 2024-12-31
Description: turboG or turboGranny is a command-line Python utility that scrapes
torrent websites,retrieves .torrent files, and automatically launches
a torrent client to begin downloading movies.
It is designed use on Linux and Termux environments, offering a simple
and fast way to download movies directly from the terminal. :)
"""
#! /usr/bin/env python3
from bs4 import BeautifulSoup
import os
import platform
import subprocess
import sys
import time
import requests
tmp_dir = "./.tmp/files"
# Colors variables
bold = "\033[1m"
bold_white = "\033[1;97m"
gray = "\033[1;37m"
red = "\033[1;91m"
blue = "\033[1;94m"
yellow = "\033[1;93m"
reset = "\033[0m"
def main():
try:
art()
args = sys.argv[1:]
query = '+'.join(args)
movie, title = scrape_page(query)
if movie == "none":
print("Couldn't load movie's data")
sys.exit(2)
name, url = scrape_movie(movie)
if url == "none":
print("Couldn't load movie's sources")
sys.exit(3)
clean(tmp_dir)
os.makedirs(tmp_dir, exist_ok=True)
os.chmod(tmp_dir, 0o755)
try:
name = name.split("/")
name = "-".join(name)
except:
pass
filename = f"{name}.torrent"
path = os.path.join(tmp_dir, filename)
try:
subprocess.run(["curl", "-o", path, url])
time.sleep(1)
download_path = get_download()
time.sleep(1)
try:
unhide(download_path, title)
subprocess.run(["aria2c", "-d", download_path, path, "--allow-overwrite=true"], check=True)
hide(download_path, title)
clean(tmp_dir)
print("\n\nOpen Downloads folder. Have fun!♥")
exit()
except subprocess.CalledProcessError as e:
clean(tmp_dir)
hide(download_path, title)
print(f"An error occurred while downloading with: {e}")
except FileNotFoundError:
clean(tmp_dir)
hide(download_path, title)
print(f"Error: aria2 is not installed.\n Please run: {bold}python3 setup.py{reset} to install missing dependencies")
except subprocess.CalledProcessError as e:
clean(tmp_dir)
hide(download_path, title)
exit()
except Exception as e:
clean(tmp_dir)
hide(download_path, title)
exit()
except requests.exceptions.ConnectionError:
clean(tmp_dir)
hide(download_path, title)
print(f"{red}Connect to the internet 🥲{red}")
sys.exit(1)
except KeyboardInterrupt:
hide(download_path, title)
clean(tmp_dir)
print("\nBai Baii ♥")
sys.exit(0)
except Exception:
clean(tmp_dir)
hide(download_path, title)
exit(1)
# Deletes donwloaded torrents
def clean(tmp_dir):
if os.path.exists(tmp_dir):
files = os.listdir(tmp_dir)
if files:
for file in files:
file_path = os.path.join(tmp_dir, file)
try:
if os.path.isfile(file_path):
os.remove(file_path)
except Exception as e:
pass
# Scrapes the initial page
def scrape_page(query):
url = f"https://yts.mx/browse-movies/{query}"
response = requests.get(url)
soup = BeautifulSoup(response.text, "lxml")
titles = []
links = []
# Prints out each movie(title to titles list and link to links list)
for movie in soup.find_all('div', class_ = 'browse-movie-wrap col-xs-10 col-sm-4 col-md-5 col-lg-4') :
title = movie.find('div', class_ = 'browse-movie-bottom').text.splitlines()
link = movie.find('a', class_ = 'browse-movie-link') ['href']
titles.append(f"{title[1]} - {title[2]}")
links.append(link)
# Prints out each title in titles and link in links
count = 1
for title in titles:
print(f"[{count:02}] {bold}{title}{reset}")
print()
count += 1
print(f"{red}[{count:02}] Exit{reset}")
print()
print()
while True:
number = int(input(f"{blue}Select a movie[01 - {count-1:02}]: {reset}"))
if not(number > count) and (number > 0):
break
if number == count:
print("\nBai Baii ♥")
sys.exit(0)
for i in range(len(links)):
if number == i+1:
return links[i], titles[i]
return "none"
# Scrapes the movie page
def scrape_movie(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, "lxml")
data = soup.find('p', class_ = 'hidden-md hidden-lg')
titles = []
torrents = []
a_tags = data.find_all('a')
for a in a_tags:
title = str(a['title'])
torrent = str(a['href'])
if not("subtitles" in title.lower() or "subtitles" in torrent.lower()):
titles.append(title)
torrents.append(torrent)
if len(titles) == len(torrents):
count = 1
else:
print("Sumn's wrong, fs")
print(f"\n{yellow}Available Sources:{reset}\n")
for title in titles:
if "Torrent" in title:
title = title.replace("Torrent", "")
print(f"[{count:02}] {bold}{title}{reset}")
print()
count += 1
print(f"{red}[{count:02}] Go back{reset}")
print()
print()
number = input(f"{blue}Select a resolution to download[01 - {count-1:02}]: {reset}")
while True:
while not number.isnumeric():
number = input(f"Select a resolution to download[1 - {count-1}]: ")
number = int(number)
if not(number > count) and (number > 0):
break
if number == count:
print("\n🔙 {}BACK{}\n".format(red,reset))
main()
for i in range(len(torrents)):
if number == i+1:
return titles[i] ,torrents[i]
return "none" , "none"
# Gets download path
def get_download():
linux = os.path.expanduser("~/Downloads")
termux = os.path.expanduser("~/storage/downloads")
if os.getenv("TERMUX_VERSION") and os.path.exists(os.path.expanduser("~/storage")):
return termux
elif os.path.exists(os.path.expanduser(("~/Downloads"))):
return linux
# Just read the code. Its really conspicuous
def detect_env():
if os.path.exists("/data/data/com.termux/files/usr/bin/pkg"):
return "termux"
elif platform.system().lower() == "linux":
return "linux"
else:
return "unknown"
# Hides the log files in download path
def hide(path, title):
title = title.split('-')
ext = "aria2"
if os.path.exists(path):
files = os.listdir(path)
for file in files:
if title[0] in file and ext in file and not str(file).startswith('.'):
file_path = os.path.join(path,file)
hidden_path = os.path.join(path, f".{file}")
try:
os.rename(file_path, hidden_path)
except:
pass
# Opposite of hide
def unhide(path, title):
title = title.split('-')
ext = "aria2"
if os.path.exists(path):
files = os.listdir(path)
for file in files:
if title[0] in file and ext in file and str(file).startswith('.'):
hidden_path = os.path.join(path,file)
file_path = os.path.join(path, file[1:])
try:
os.rename(hidden_path, file_path)
except:
pass
# Skip :)
def art():
try:
desc = "Fast and Simple Movie Downloader"
version = "25.01.01"
author = "https://github.com/bubbs-sh/"
art = r'''
(\_/) {3}turboGranny{4} v{0}
( •_•) {1}
/ >♥< \ ~ {2} ~
'''.format(version, desc, author, bold, reset)
subprocess.run(f"echo \"{art}\" | lolcat", shell=True)
except FileNotFoundError:
print(f"Missing dependencies, please install them.\nExecute: {bold}python3 install.py{reset}")
except KeyboardInterrupt:
pass
except Exception:
pass
if __name__ == "__main__":
main()