Ver código fonte

torrent manager

master
Robin Thoni 9 anos atrás
pai
commit
c255b4efff
5 arquivos alterados com 69 adições e 41 exclusões
  1. 17
    6
      fake-tracker.py
  2. 7
    1
      torrentfile.py
  3. 42
    0
      torrentmanager.py
  4. 3
    3
      torrenttracker.py
  5. 0
    31
      trackermanager.py

+ 17
- 6
fake-tracker.py Ver arquivo

@@ -1,8 +1,10 @@
1 1
 #! /usr/bin/env python
2 2
 
3 3
 import sys
4
+import time
4 5
 import torrentfile
5 6
 import torrenttracker
7
+import torrentmanager
6 8
 import config
7 9
 
8 10
 if len(sys.argv) >= 2:
@@ -13,12 +15,21 @@ else:
13 15
 maxRatio = config.getMaxRatio()
14 16
 maxRate = config.getMaxRatio()
15 17
 
16
-
17
-
18
+trackers = []
18 19
 for file in ('torrent.torrent', 'torrent2.torrent'):
19 20
     torrent = torrentfile.TorrentFile()
20 21
     torrent.readFile(file)
21
-    tracker = torrenttracker.TorrentTracker(torrent)
22
-    r = tracker.start()
23
-    print r
24
-    print tracker.getInterval()
22
+    trackers.append(torrent)
23
+
24
+manager = torrentmanager.TorrentManager(trackers)
25
+manager.start()
26
+run = True
27
+while run:
28
+    t = manager.getNextTime()
29
+    torrent = t['torrent']
30
+    secs = t['time']
31
+    print(torrent.getName() + ": Next update in " + str(secs) + " seconds")
32
+    time.sleep(secs)
33
+    print("Updating...")
34
+    manager.updateNextTracker()
35
+

+ 7
- 1
torrentfile.py Ver arquivo

@@ -4,21 +4,24 @@ import os
4 4
 import hashlib
5 5
 import bencode
6 6
 import torrententry
7
+import torrenttracker
7 8
 
8 9
 class TorrentFile:
9 10
     data = ()
10 11
     info = ()
12
+    tracker = None
11 13
     def readFile(self, path):
12 14
         with open(path, 'rb') as f:
13 15
             bencoded = f.read()
14 16
         self.readBencoded(bencoded)
17
+        self.tracker = torrenttracker.TorrentTracker(self)
15 18
 
16 19
     def readBencoded(self, data):
17 20
         self.data = bencode.bdecode(data)
18 21
         self.info = self.data['info']
19 22
 
20 23
     def getName(self):
21
-        return self.data['name']
24
+        return self.info['name']
22 25
 
23 26
     def getFiles(self):
24 27
         if 'files' in self.info:
@@ -36,6 +39,9 @@ class TorrentFile:
36 39
         return size
37 40
 
38 41
     def getTracker(self):
42
+        return self.tracker
43
+
44
+    def getAnnounce(self):
39 45
         return self.data['announce']
40 46
 
41 47
     def getHash(self):

+ 42
- 0
torrentmanager.py Ver arquivo

@@ -0,0 +1,42 @@
1
+#! /usr/bin/env python
2
+
3
+import copy
4
+import time
5
+import torrenttracker
6
+
7
+class TorrentManager:
8
+    torrents = []
9
+    next_torrents = []
10
+    last_time = 0
11
+    def __init__(self, torrents):
12
+        self.torrents = torrents
13
+
14
+    def start(self):
15
+        for torrent in self.torrents:
16
+            torrent.getTracker().start()
17
+
18
+    def resetNextTorrents(self):
19
+        self.last_time = int(time.time())
20
+        self.next_torrents = copy.copy(self.torrents)
21
+
22
+    def getNextTorrent(self):
23
+        if not any(self.next_torrents):
24
+            self.resetNextTorrents()
25
+        minTorrent = self.next_torrents[0]
26
+        for torrent in self.next_torrents:
27
+            if (torrent.getTracker().getInterval()
28
+                    < minTorrent.getTracker().getInterval()):
29
+                minTorrent = torrent
30
+        return minTorrent
31
+
32
+    def getNextTime(self):
33
+        torrent = self.getNextTorrent()
34
+        t = max(torrent.getTracker().getInterval() - (int(time.time()) - self.last_time), 0)
35
+        return {'time': t, 'torrent': torrent}
36
+
37
+    def updateTorrent(self, torrent = None):
38
+        if torrent == None:
39
+            torrent = self.getNextTorrent()
40
+        torrent.getTracker().interval(0, 0, 0)
41
+        self.next_torrents.remove(torrent)
42
+

+ 3
- 3
torrenttracker.py Ver arquivo

@@ -31,7 +31,7 @@ class TorrentTracker:
31 31
                 "left": left,
32 32
                 "event": event}
33 33
         headers = {"User-Agent": self.user_agent}
34
-        r = self.session.get(self.torrent.getTracker(), headers=headers, params=params)
34
+        r = self.session.get(self.torrent.getAnnounce(), headers=headers, params=params)
35 35
         data = bencode.bdecode(r.content)
36 36
         if 'interval' in data:
37 37
             self.interval = int(data['interval'])
@@ -40,10 +40,10 @@ class TorrentTracker:
40 40
     def start(self):
41 41
         return self.request(0, 0, 0, "started")
42 42
 
43
-    def stop(self):
43
+    def stop(self, uploaded, downloaded, left):
44 44
         return self.request(0, 0, 0, "stopped")
45 45
 
46
-    def complete(self):
46
+    def complete(self, uploaded):
47 47
         return self.request(0, 0, 0, "completed")
48 48
 
49 49
     def interval(self, uploaded, downloaded, left):

+ 0
- 31
trackermanager.py Ver arquivo

@@ -1,31 +0,0 @@
1
-#! /usr/bin/env python
2
-
3
-import copy
4
-import torrenttracker
5
-
6
-class TrackerManager:
7
-    trackers = []
8
-    next_trackers = []
9
-    def __init__(self, trackers):
10
-        self.trackers = trackers
11
-
12
-    def start(self):
13
-        for tracker in self.trackers:
14
-            tracker.start()
15
-
16
-    def resetNextTrackers(self):
17
-        self.next_trackers = copy.copy(self.tracker)
18
-
19
-    def getNextTracker(self):
20
-        if not any(self.next_trackers):
21
-            self.resetNextTrackers()
22
-        minTracker = self.next_trackers[0]
23
-        for tracker in self.next_trackers:
24
-            if tracker.getInterval() < minTracker.getInterval():
25
-                minTracker = tracker
26
-        return minTracker
27
-
28
-    def updateNextTracker(self):
29
-        tracker = self.getNextTracker()
30
-        tracker.interval(0, 0, 0)
31
-        self.next_trackers.remove(tracker)

Carregando…
Cancelar
Salvar