]>
Commit | Line | Data |
---|---|---|
879aa787 MT |
1 | #!/usr/bin/python |
2 | ||
3 | TRACKER_URL ="http://tracker.ipfire.org:6969/stats?format=txt&mode=tpbs" | |
4 | TORRENT_BASE="/srv/pakfire/data/torrent" | |
5 | ||
6 | import os | |
7 | import sha | |
8 | import urllib2 | |
9 | ||
10 | from client.bencode import bencode, bdecode | |
11 | import web | |
12 | ||
13 | class TrackerInfo: | |
14 | def __init__(self, url): | |
15 | self.info = {} | |
16 | ||
17 | f = urllib2.urlopen(url) | |
18 | for line in f.readlines(): | |
19 | (hash, seeds, peers,) = line.split(":") | |
20 | self.info[hash] = (seeds, peers.rstrip("\n"),) | |
21 | f.close() | |
22 | ||
23 | def __call__(self): | |
24 | print self.info | |
25 | ||
26 | def get(self, hash): | |
27 | try: | |
28 | return self.info[hash] | |
29 | except KeyError: | |
30 | return 0, 0 | |
31 | ||
32 | ||
33 | class TorrentObject: | |
34 | def __init__(self, file): | |
35 | self.name = os.path.basename(file) | |
36 | f = open(file, "rb") | |
37 | self.info = bdecode(f.read()) | |
38 | f.close() | |
39 | ||
40 | def __call__(self): | |
41 | print "File : %s" % self.get_file() | |
42 | print "Hash : %s" % self.get_hash() | |
43 | ||
44 | def get_hash(self): | |
45 | return sha.sha(bencode(self.info["info"])).hexdigest().upper() | |
46 | ||
47 | def get_file(self): | |
48 | return self.name | |
49 | ||
50 | ||
51 | torrent_files = [] | |
52 | for file in os.listdir(TORRENT_BASE): | |
53 | if not file.endswith(".torrent"): | |
54 | continue | |
55 | file = os.path.join(TORRENT_BASE, file) | |
56 | torrent_files.insert(0, TorrentObject(file)) | |
57 | ||
58 | ||
59 | tracker = TrackerInfo(TRACKER_URL) | |
60 | ||
61 | class TorrentBox(web.Box): | |
62 | def __init__(self, file): | |
63 | web.Box.__init__(self, file.name, file.get_hash()) | |
64 | self.w(""" | |
65 | <p> | |
66 | <strong>Seeders:</strong> %s<br /> | |
67 | <strong>Leechers:</strong> %s | |
68 | </p>""" % tracker.get(file.get_hash())) | |
69 | self.w(""" | |
70 | <p style="text-align: right;"> | |
71 | <a href="http://download.ipfire.org/torrent/%s">Download</a> | |
72 | </p>""" % (file.name,)) | |
73 | ||
74 | ||
75 | class Content(web.Content): | |
76 | def __init__(self, name): | |
77 | web.Content.__init__(self, name) | |
78 | ||
79 | def content(self): | |
80 | self.w("<h3>IPFire Torrent Tracker</h3>") | |
81 | for t in torrent_files: | |
82 | b = TorrentBox(t) | |
83 | self.w(b()) | |
84 | ||
85 | Sidebar = web.Sidebar |