Browse Source

ratio management

master
Robin Thoni 9 years ago
parent
commit
4d761246cb
6 changed files with 35 additions and 12 deletions
  1. 3
    3
      config.conf
  2. 2
    2
      config.py
  3. 7
    2
      fake-tracker.py
  4. 5
    0
      torrentfile.py
  5. 14
    5
      torrentmanager.py
  6. 4
    0
      torrenttracker.py

+ 3
- 3
config.conf View File

@@ -1,4 +1,4 @@
1 1
 [General]
2
-maxRatio: 3
3
-maxRate: 120
4
-torrentsPath: "."
2
+maxRatio: 1
3
+maxRate: 512
4
+torrentsPath: .

+ 2
- 2
config.py View File

@@ -37,10 +37,10 @@ class Config:
37 37
             return None
38 38
 
39 39
     def getMaxRatio(self):
40
-        return int(self.getOption('maxratio'))
40
+        return float(self.getOption('maxratio'))
41 41
 
42 42
     def getMaxRate(self):
43
-        return int(self.getOption('maxrate'))
43
+        return float(self.getOption('maxrate'))
44 44
 
45 45
     def getTorrentsPath(self):
46 46
         return str(self.getOption('torrentspath'))

+ 7
- 2
fake-tracker.py View File

@@ -20,6 +20,10 @@ maxRatio = config.getMaxRatio()
20 20
 maxRate = config.getMaxRate()
21 21
 torrentsPath = config.getTorrentsPath()
22 22
 
23
+print("Max ratio:", maxRatio)
24
+print("Max rate:", maxRate, "kb/s")
25
+print("Torrents path:", torrentsPath)
26
+
23 27
 manager = torrentmanager.TorrentManager(maxRatio, maxRate)
24 28
 run = True
25 29
 while run:
@@ -48,9 +52,10 @@ while run:
48 52
     if secs > 0:
49 53
         print(torrent.getName() + ": Next update in " + str(secs) + " seconds")
50 54
         time.sleep(secs)
51
-    print("Updating", torrent.getName())
55
+    print("Updating", torrent.getName(),
56
+            "ratio =", (torrent.getTracker().getUploaded() / float(torrent.getSize())))
52 57
     try:
53
-        manager.updateTorrent(torrent)
58
+        manager.updateTorrent()
54 59
     except:
55 60
         warning("could not update")
56 61
 

+ 5
- 0
torrentfile.py View File

@@ -10,7 +10,9 @@ class TorrentFile:
10 10
     data = ()
11 11
     info = ()
12 12
     tracker = None
13
+    filepath = ""
13 14
     def readFile(self, path):
15
+        self.filepath = path
14 16
         with open(path, 'rb') as f:
15 17
             bencoded = f.read()
16 18
         self.readBencoded(bencoded)
@@ -20,6 +22,9 @@ class TorrentFile:
20 22
         self.data = bencode.bdecode(data)
21 23
         self.info = self.data['info']
22 24
 
25
+    def getFilepath(self):
26
+        return self.filepath
27
+
23 28
     def getName(self):
24 29
         return self.info['name']
25 30
 

+ 14
- 5
torrentmanager.py View File

@@ -1,5 +1,6 @@
1 1
 #! /usr/bin/env python
2 2
 
3
+import os
3 4
 import copy
4 5
 import time
5 6
 import random
@@ -11,13 +12,16 @@ class TorrentManager:
11 12
     last_time = 0
12 13
     max_ratio = 0
13 14
     max_rate = 0
14
-    def __init__(self, max_ratio = 1, max_rate = 120):
15
+    def __init__(self, max_ratio, max_rate):
15 16
         self.max_ratio = max_ratio
16 17
         self.max_rate = max_rate * 1024
17 18
 
18 19
     def appendTorrent(self, torrent):
19 20
         self.torrents.append(torrent)
20 21
 
22
+    def removeTorrent(self, torrent):
23
+        self.torrents.remove(torrent)
24
+
21 25
     def getTorrents(self):
22 26
         return self.torrents
23 27
 
@@ -40,11 +44,16 @@ class TorrentManager:
40 44
         t = max(torrent.getTracker().getInterval() - (int(time.time()) - self.last_time), 0)
41 45
         return {'time': t, 'torrent': torrent}
42 46
 
43
-    def updateTorrent(self, torrent = None):
44
-        if torrent == None:
45
-            torrent = self.getNextTorrent()
46
-        uploaded = (self.max_rate / len(self.torrents)) * torrent.getTracker().getInterval()
47
+    def updateTorrent(self):
48
+        torrent = self.getNextTorrent()
49
+        uploaded = int(self.max_rate / len(self.torrents)) * torrent.getTracker().getInterval()
47 50
         uploaded += random.randint(-uploaded / 10, uploaded / 10)
48 51
         self.next_torrents.remove(torrent)
52
+        if torrent.getSize() * self.max_ratio <= torrent.getTracker().getUploaded():
53
+            self.torrents.remove(torrent)
54
+            try:
55
+                os.remove(torrent.getFilepath())
56
+            except:
57
+                print("WARNING: failed to remove torrent", torrent.getFilepath())
49 58
         torrent.getTracker().update(uploaded)
50 59
 

+ 4
- 0
torrenttracker.py View File

@@ -60,3 +60,7 @@ class TorrentTracker:
60 60
         self.left -= left
61 61
         return self.request()
62 62
 
63
+
64
+    def getUploaded(self):
65
+        return self.uploaded
66
+

Loading…
Cancel
Save