Bläddra i källkod

torrent tracker

master
Robin Thoni 9 år sedan
förälder
incheckning
896e3910c0
1 ändrade filer med 51 tillägg och 0 borttagningar
  1. 51
    0
      torrenttracker.py

+ 51
- 0
torrenttracker.py Visa fil

@@ -0,0 +1,51 @@
1
+#! /usr/bin/env python
2
+
3
+import requests
4
+import time
5
+import hashlib
6
+import bencode
7
+
8
+class TorrentTracker:
9
+    torrent = None
10
+    session = None
11
+    peer_id = ""
12
+    port = 0
13
+    user_agent = ""
14
+    interval = 300
15
+    def __init__(self, torrent, port=6882, user_agent="rtorrent/0.8.42/0.12.42"):
16
+        self.torrent = torrent
17
+        self.session = requests.session()
18
+        self.peer_id = hashlib.sha1(str(time.time())).digest()
19
+        self.port = port
20
+        self.user_agent = user_agent
21
+
22
+    def getInterval(self):
23
+        return self.interval
24
+
25
+    def request(self, uploaded, downloaded, left, event=""):
26
+        params = {"info_hash": self.torrent.getHash().digest(),
27
+                "peer_id": self.peer_id,
28
+                "port": self.port,
29
+                "uploaded": uploaded,
30
+                "downloaded": downloaded,
31
+                "left": left,
32
+                "event": event}
33
+        headers = {"User-Agent": self.user_agent}
34
+        r = self.session.get(self.torrent.getTracker(), headers=headers, params=params)
35
+        data = bencode.bdecode(r.content)
36
+        if 'interval' in data:
37
+            self.interval = int(data['interval'])
38
+        return data;
39
+
40
+    def start(self):
41
+        return self.request(0, 0, 0, "started")
42
+
43
+    def stop(self):
44
+        return self.request(0, 0, 0, "stopped")
45
+
46
+    def complete(self):
47
+        return self.request(0, 0, 0, "completed")
48
+
49
+    def interval(self, uploaded, downloaded, left):
50
+        return self.request(uploaded, downloaded, left)
51
+

Laddar…
Avbryt
Spara