Парсинг магнет ссылок на python, parsing magnet URI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import urllib2
def parse_magnet(magnet):
try:
text = magnet
if text.startswith('magnet:?'):
xl = None
dn = None
xt = None
string = text.split('?')[1]
for i in string.split('&'):
if i.startswith('xt='):
xt = i.split('=')[1]
elif i.startswith('xl='):
xl = i.split('=')[1]
elif i.startswith('dn='):
dn = i.split('=')[1]
if xt != None and xl != None and dn != None:
dn = urllib2.unquote(dn).decode('utf-8').encode('windows-1251')
TTH = xt.split(':')[-1]
return (dn, xl, TTH)
except:
pass
return (None, None, None)