-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmagnetfinder.py
258 lines (210 loc) · 9.36 KB
/
magnetfinder.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
import bs4 as bs
import re
import random
import requests
import string
import os
import operator
import lxml
from subprocess import call
from prettytable import PrettyTable
from pathlib import Path
from configparser import ConfigParser
import platform
import getopt, sys
config = ConfigParser()
config.read('config.ini')
class Torrent_Link():
def name(self, name):
self.name = name
def magnetlink(self, link):
self.magnet = link
def size(self, size):
self.size = size
def seeders(self, seeders):
self.seeders = seeders
def choose_torrent_website():
accepted_links = ['nyaa', 'piratebay']
while(True):
choice = input('Torrent Site (nyaa, piratebay, or \'all\'): ')
choice = choice.lower()
if(choice in accepted_links):
return choice
if(choice == 'all'):
return choice
print('Choose one of the available websites (or \'all\'):', *accepted_links)
def display_torrents_found(amount):
x = PrettyTable()
x.field_names = ["#", "Name", "Size", "Seeders"]
x.align['Name'] = 'l'
x.align['Size'] = 'l'
x.align['Seeders'] = 'l'
for num, t in enumerate(top_torrents[(amount-10):amount], amount-9):
x.add_row([num, t.name, t.size, t.seeders])
t.magnet = t.magnet.split('&tr=', 1)[0]
print(x)
selected = input('Torrent num(s)? (n for next 10): ')
return selected
def grab_nyaa():
link = 'https://nyaa.si/?f=0&c=0_0&q='
sortbyseeders = '&s=seeders&o=desc'
search_query = re.sub(r"\s+", "+", original_query)
link = ''.join([link, search_query, sortbyseeders])
data = requests.get(link).text
soup = bs.BeautifulSoup(data, "lxml")
for torrent in soup.find_all('tr'):
currentTorrent = Torrent_Link()
for link in torrent.find_all('a'):
if(link.get('title') != None):
if(all((word.lower() in link.get('title').lower() for word in original_query.split()))):
currentTorrent.name(link.get('title'))
top_torrents.append(currentTorrent)
if('magnet' in link['href'].lower()):
currentTorrent.magnetlink(link['href'])
for info in torrent.find_all('td', {'class': 'text-center'})[:4]:
if(not '-' in info.text):
if('B' in info.text):
currentTorrent.size(info.text)
else:
currentTorrent.seeders = info.text
return top_torrents
def grab_piratebay():
link = 'https://www.tpb.party/search/'
sortbyseeders = ''
search_query = re.sub(r"\s+", "%20", original_query) +'/1/99/0'
link = ''.join([link, search_query, sortbyseeders])
data = requests.get(link).text
soup = bs.BeautifulSoup(data, "lxml")
for row in soup.find_all('tr'):
currentTorrent = Torrent_Link()
for torrent in row.find_all('div', {'class': 'detName'}):
currentTorrent.name((torrent.find('a', {'class': 'detLink'})['title'])[11:])
top_torrents.append(currentTorrent)
for mag in row.find_all('a', href=True):
if('magnet' in mag['href'].lower()):
currentTorrent.magnetlink((mag['href']))
if(row.find('font', {'class': 'detDesc'}) is not None):
desc = row.find('font', {'class': 'detDesc'}).text
try:
size = re.search('Size(.+?),', desc)
currentTorrent.size(size.group(1))
except:
currentTorrent.size('#')
if(row.find('td', {'align': 'right'}) is not None):
currentTorrent.seeders = row.find('td', {'align': 'right'}).text
else:
currentTorrent.seeders = '#'
return top_torrents
def handle_anime_directories():
if(type_of_media.lower() == 'a' or type_of_media.lower() == 'anime'):
series_status = input('On-Going or Finished Series? (O / F): ')
if(series_status.lower() == 'o'):
show_list = open('ongoing_directories.txt', 'a+')
new_show = input('New Folder? (Y/N): ')
if new_show.lower() == 'y':
new_show_name = input('Enter Name of New Folder: ')
show_list.write(new_show_name +'\n')
show_directory_name = new_show_name
show_list.close()
if(platform.system().lower() == 'windows'):
directory = Path(config['directories']['anime'] + show_directory_name)
else:
directory = Path(config['directories']['anime'] + re.sub(r"\s+", "\\ ", show_directory_name))
else:
with open('ongoing_directories.txt', 'r') as f:
all_ongoing_shows = f.read().splitlines()
for num, show in enumerate(all_ongoing_shows):
print(f'({num+1}) {show}')
show_choice = input('Choose which show to add too (1-#): ')
show_directory_name = all_ongoing_shows[int(show_choice)-1]
if(platform.system().lower() == 'windows'):
directory = Path(config['directories']['anime'] + show_directory_name)
else:
directory = Path(config['directories']['anime'] + re.sub(r"\s+", "\\ ", show_directory_name))
else:
directory = Path(config['directories']['anime'])
elif(type_of_media.lower() == 'm' or type_of_media.lower() == 'movie'):
directory = Path(config['directories']['movies'])
return directory
def autodownload():
if(vpnarg and vpnarg.lower() == 'true'):
try:
call(['sudo', 'nordvpn', 'connect'])
except:
print("failed to run NordVPN")
elif(config['vpn']['status'].lower() == 'true'):
try:
call(['sudo', 'nordvpn', 'connect'])
except:
print("failed to run NordVPN")
for num in selected.split():
try:
if(config['torrent-client']['client'].lower() == 'aria2c'):
call(['aria2c', '-d', f'{Path.home().joinpath(directory)}', '--seed-time=0', f'{top_torrents[int(num) - 1].magnet}'])
elif(config['torrent-client']['client'].lower() == 'deluge'):
call(['sudo', 'deluge-console', 'add', '-p', f'{directory}', top_torrents[int(num) - 1].magnet])
elif(config['torrent-client']['client'].lower() == 'transmission'):
print(f'{Path.home().joinpath(directory)}')
call(['transmission-remote', '-w', f'{Path.home().joinpath(directory)}', '-a', top_torrents[int(num) - 1].magnet])
except:
print(f"Error auto-downloading torrent from selected torrent client {config['torrent-client']['client']}")
print(top_torrents[int(num)-1].magnet)
if __name__ == '__main__':
choice = choose_torrent_website()
type_of_media = input('Anime Series, Movie or TV Show? (A / M / T): ')
original_query = input('Search Query: ')
top_torrents = []
directory = None
full_cmd_args = sys.argv
arg_list = full_cmd_args[1:]
short_options = "v:"
long_options = "vpn="
vpnarg = None
try:
arguments, values = getopt.getopt(arg_list, short_options, long_options)
except getopt.error as err:
print(str(err))
sys.exit(2)
for current_argument, current_value in arguments:
if current_argument in ("-v", "--vpn"):
vpnarg = current_value
if(choice == 'nyaa'):
#oranizing folders and saves folder locations if its an ongoing series
directory = handle_anime_directories()
#finds the top 50 magnet links from nyaa
grab_nyaa()
if(choice =='piratebay'):
if(type_of_media.lower() == 't' or type_of_media.lower() == 'tv show'):
directory = Path(config['directories']['tvshows'])
elif(type_of_media.lower() == 'm' or type_of_media.lower() == 'movie'):
directory = Path(config['directories']['movies'])
elif(type_of_media.lower() == 'a' or type_of_media.lower() == 'anime'):
directory = handle_anime_directories()
#finds the top 50 magnet links from piratebay
grab_piratebay()
if(choice == 'all'):
if(type_of_media.lower() == 't' or type_of_media.lower() == 'tv show'):
directory = Path(config['directories']['tvshows'])
elif(type_of_media.lower() == 'm' or type_of_media.lower() == 'movie'):
directory = Path(config['directories']['movies'])
elif(type_of_media.lower() == 'a' or type_of_media.lower() == 'anime'):
directory = handle_anime_directories()
grab_nyaa()
grab_piratebay()
#manually sorts torrents if they are from multiple websites
top_torrents.sort(key=lambda x: int(x.seeders), reverse=True)
selected = 'n'
amount = 10
#displays torrents in a prettytable and gets user selection
while(selected.lower() == 'n'):
selected = display_torrents_found(amount)
amount = amount + 10
if(amount > len(top_torrents)+9):
amount = 10
print('All torrents displayed, please select one or multiple (Ex: 1 5 15)')
#if user configured autodownload to be true, will call torrent client to download selected torrents
if(config['autodownload']['status'].lower() == 'true'):
autodownload()
else:
for num in selected.split():
print(top_torrents[int(num)-1].magnet)