-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
tormag.py
185 lines (149 loc) · 5.43 KB
/
tormag.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
import requests
import os
from bs4 import BeautifulSoup
import hashlib
import urllib.parse
# bencode #####
import collections
def bencode(elem):
if type(elem) == str:
elem = str.encode(elem)
if type(elem) == bytes:
result = str.encode(str(len(elem)))+b":"+elem
elif type(elem) == int:
result = str.encode("i"+str(elem)+"e")
elif type(elem) == list:
result = b"l"
for item in elem:
result += bencode(item)
result += b"e"
elif type(elem) in [dict, collections.OrderedDict]:
result = b"d"
for key in elem:
result += bencode(key)+bencode(elem[key])
result += b"e"
return result
def bdecode(bytestr, recursiveCall=False):
startingChars = dict({
b"i" : int,
b":" : str,
b"l" : list,
b"d" : dict
})
digits = [b"0", b"1", b"2", b"3", b"4", b"5", b"6", b"7", b"8", b"9"]
started = ended = False
curtype = None
numstring = b"" # for str, int
result = None # for list, dict
key = None # for dict
while len(bytestr) > 0:
# reading and popping from the beginning
char = bytestr[:1]
if not started:
bytestr = bytestr[1:]
if char in digits:
numstring += char
elif char in startingChars:
started = True
curtype = startingChars[char]
if curtype == str:
size = int(bytes.decode(numstring))
# try to decode strings
try:
result = bytes.decode(bytestr[:size])
except UnicodeDecodeError:
result = bytestr[:size]
bytestr = bytestr[size:]
ended = True
break
elif curtype == list:
result = []
elif curtype == dict:
result = collections.OrderedDict()
else:
raise ValueError("Expected starting char, got ‘"+bytes.decode(char)+"’")
else: # if started
if not char == b"e":
if curtype == int:
bytestr = bytestr[1:]
numstring += char
elif curtype == list:
item, bytestr = bdecode(bytestr, recursiveCall=True)
result.append(item)
elif curtype == dict:
if key == None:
key, bytestr = bdecode(bytestr, recursiveCall=True)
else:
result[key], bytestr = bdecode(bytestr, recursiveCall=True)
key = None
else: # ending: char == b"e"
bytestr = bytestr[1:]
if curtype == int:
result = int(bytes.decode(numstring))
ended = True
break
if ended:
if recursiveCall:
return result, bytestr
else:
return result
else:
raise ValueError("String ended unexpectedly")
############
def decodeurl(url):
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:103.0) Gecko/20100101 Firefox/103.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Origin': 'https://www.url-encode-decode.com',
'Connection': 'keep-alive',
'Referer': 'https://www.url-encode-decode.com/',
'Upgrade-Insecure-Requests': '1',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-User': '?1',
}
data = { 'string': url, 'action': 'Decode'}
response = requests.post('https://www.url-encode-decode.com/', headers=headers, data=data)
soup = BeautifulSoup(response.text,"html.parser")
return soup.find("textarea",id="string2").string
def getTorFile(maglink):
id = maglink.split("magnet:?xt=urn:btih:")[1].split('&')[0]
try:
name = maglink.split('&dn=')[1].split("&tr=")[0]
name = decodeurl(name) + ".torrent"
name = "".join( x for x in name if (x.isalnum() or x in "._-@ "))
except:
name = id + ".torrent"
os.system(f"wget https://itorrents.org/torrent/{id}.torrent")
os.rename(f'{id}.torrent',name)
return name
def getMagnet(filename):
file = open(filename, "br")
byte_stream = file.read()
file.close()
torrentdic = bdecode(byte_stream)
result = []
if "info" not in torrentdic:
return ("No info dict in torrent file")
encodedInfo = bencode(torrentdic["info"])
sha1 = hashlib.sha1(encodedInfo).hexdigest()
result.append("xt=urn:btih:"+sha1)
if "name" in torrentdic["info"]:
quoted = urllib.parse.quote(torrentdic["info"]["name"], safe="")
result.append("dn="+quoted)
trackers = []
if "announce-list" in torrentdic:
for urllist in torrentdic["announce-list"]:
trackers += urllist
elif "announce" in torrentdic:
trackers.append(torrentdic["announce"])
seen_urls = []
for url in trackers:
if [url] not in seen_urls:
seen_urls.append([url])
quoted = urllib.parse.quote(url, safe="")
result.append("tr="+quoted)
torrentdic["announce-list"] = seen_urls
return "magnet:?" + "&".join(result)