-
Notifications
You must be signed in to change notification settings - Fork 0
/
magnet_maker.py
38 lines (34 loc) · 1.08 KB
/
magnet_maker.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
import sys, mmap, gc, urllib.parse
m = 'magnet:?'
lm = len(m)
def build_magnet(res):
if "dn" in res.keys():
magnet = m+urllib.parse.urlencode({"xt": res["xt"], "dn": ''.join(res["dn"])})
else:
magnet = m+urllib.parse.urlencode({"xt": res["xt"]})
magnet = magnet.replace("urn%3Abtih%3A", "urn:btih:")
if 'tr' in res.keys():
for tr in res['tr']:
magnet += "&"+urllib.parse.urlencode({"tr":tr})
return magnet
trs = [x for x in open('trz').read().split('\n') if x]
with open(sys.argv[1], 'r+') as f:
data = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ)
i = 0
while True:
i += 1
res = {}
line = data.readline()
try:
line = line.decode('utf-8')
if line.strip() == '':
break
ih, dn = line.split(' ', maxsplit=1)
res["xt"] = "urn:btih:"+ih.lower()
res["dn"] = dn
res['tr'] = trs
sys.stdout.write(build_magnet(res)+'\n')
if (i % 1000) == 0:
gc.collect()
except:
pass