]> git.ipfire.org Git - people/shoehn/ipfire.org.git/blob - www/pages/torrent/__init__.py
Change Color of Menuitem CeBIT.
[people/shoehn/ipfire.org.git] / www / pages / torrent / __init__.py
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 import web.elements
13
14 class TrackerInfo:
15 def __init__(self, url):
16 self.info = {}
17
18 f = urllib2.urlopen(url)
19 for line in f.readlines():
20 (hash, seeds, peers,) = line.split(":")
21 self.info[hash] = (seeds, peers.rstrip("\n"),)
22 f.close()
23
24 def __call__(self):
25 print self.info
26
27 def get(self, hash):
28 try:
29 return self.info[hash]
30 except KeyError:
31 return 0, 0
32
33
34 class TorrentObject:
35 def __init__(self, file):
36 self.name = os.path.basename(file)
37 f = open(file, "rb")
38 self.info = bdecode(f.read())
39 f.close()
40
41 def __call__(self):
42 print "File : %s" % self.get_file()
43 print "Hash : %s" % self.get_hash()
44
45 def get_hash(self):
46 return sha.sha(bencode(self.info["info"])).hexdigest().upper()
47
48 def get_file(self):
49 return self.name
50
51
52 torrent_files = []
53 for file in os.listdir(TORRENT_BASE):
54 if not file.endswith(".torrent"):
55 continue
56 file = os.path.join(TORRENT_BASE, file)
57 torrent_files.insert(0, TorrentObject(file))
58
59
60 tracker = TrackerInfo(TRACKER_URL)
61
62 class TorrentBox(web.elements.Box):
63 def __init__(self, file):
64 web.elements.Box.__init__(self, file.name, file.get_hash())
65 self.w("""
66 <p>
67 <strong>Seeders:</strong> %s<br />
68 <strong>Leechers:</strong> %s
69 </p>""" % tracker.get(file.get_hash()))
70 self.w("""
71 <p style="text-align: right;">
72 <a href="http://download.ipfire.org/torrent/%s">Download</a>
73 </p>""" % (file.name,))
74
75
76 class Content(web.Content):
77 def __init__(self):
78 web.Content.__init__(self)
79
80 def content(self):
81 self.w("<h3>IPFire Torrent Tracker</h3>")
82 for t in torrent_files:
83 b = TorrentBox(t)
84 self.w(b())
85
86 page = web.Page()
87 page.content = Content()
88 page.sidebar = web.elements.Sidebar()