-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker.py
More file actions
executable file
·87 lines (70 loc) · 2.3 KB
/
Copy pathtracker.py
File metadata and controls
executable file
·87 lines (70 loc) · 2.3 KB
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
import urllib2
import urlparse
import btlib.bcode
import btlib.infohash
class Tracker():
def __init__(self, **kargs):
if 'url' in kargs:
self.set_url(kargs['url'])
def set_url(self, url):
announce_path=scrape_path=None
o = urlparse.urlparse(url)
# udp trackers are not supported in this version
if o.scheme != 'http':
raise
if o.path:
if o.path.find('announce') != -1:
announce_path=o.path
scrape_path=o.path.replace("announce", "scrape")
else:
announce_path=o.path + int(not o.path.endswith('/')) * '/' + "announce"
scrape_path=o.path + int(not o.path.endswith('/')) * '/' + "scrape"
else:
announce_path = "/announce"
scrape_path = "/scrape"
self.announce_url = urlparse.urlunparse((o.scheme, o.netloc, announce_path, None, None, None))
self.scrape_url = urlparse.urlunparse((o.scheme, o.netloc, scrape_path, None, None, None))
# print self.announce_url, self.scrape_url
# scrape a tracker for a list of info_hashes, bdecode the output and return normalized directory
def scrape(self, hash_list=[]):
url = self.scrape_url
# if hash_list is given cycle trough hashes and construct url
if hash_list:
url += '?'
for info_hash in hash_list:
url += 'info_hash=' + urllib2.quote(btlib.infohash.hex2bin(info_hash)) + '&'
# url += 'info_hash=' + info_hash + '&'
url = url.rstrip('&')
# print "Scrapping ", url
res=[]
# call the tracker and read the response in a dictionary
try:
ua = urllib2.urlopen(url)
try:
res = btlib.bcode.bdecode(ua.read())['files']
except:
ua.close()
return
ua.close()
except:
return
# done. return the dictionary
return(res)
# announce info_hash to a tracker and collect peers, bdecode the output and return normalized directory
def announce(self, info_hash, cmd, me):
url = self.announce_url + '?info_hash=' + urllib2.quote(btlib.infohash.hex2bin(info_hash)) + "&port=65000&uploaded=0&downloaded=0&left=0&compact=1&event=started&numwant=100&peer_id=00000000000000000000" + "&event=" + cmd
if me: url += "&me=1"
res=[]
# call the tracker and read the response in a dictionary
try:
ua = urllib2.urlopen(url)
try:
res = btlib.bcode.bdecode(ua.read())
except:
ua.close()
return
ua.close()
except:
return
# done. return the dictionary
return(res)