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
 [General]
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
             return None
37
             return None
38
 
38
 
39
     def getMaxRatio(self):
39
     def getMaxRatio(self):
40
-        return int(self.getOption('maxratio'))
40
+        return float(self.getOption('maxratio'))
41
 
41
 
42
     def getMaxRate(self):
42
     def getMaxRate(self):
43
-        return int(self.getOption('maxrate'))
43
+        return float(self.getOption('maxrate'))
44
 
44
 
45
     def getTorrentsPath(self):
45
     def getTorrentsPath(self):
46
         return str(self.getOption('torrentspath'))
46
         return str(self.getOption('torrentspath'))

+ 7
- 2
fake-tracker.py View File

20
 maxRate = config.getMaxRate()
20
 maxRate = config.getMaxRate()
21
 torrentsPath = config.getTorrentsPath()
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
 manager = torrentmanager.TorrentManager(maxRatio, maxRate)
27
 manager = torrentmanager.TorrentManager(maxRatio, maxRate)
24
 run = True
28
 run = True
25
 while run:
29
 while run:
48
     if secs > 0:
52
     if secs > 0:
49
         print(torrent.getName() + ": Next update in " + str(secs) + " seconds")
53
         print(torrent.getName() + ": Next update in " + str(secs) + " seconds")
50
         time.sleep(secs)
54
         time.sleep(secs)
51
-    print("Updating", torrent.getName())
55
+    print("Updating", torrent.getName(),
56
+            "ratio =", (torrent.getTracker().getUploaded() / float(torrent.getSize())))
52
     try:
57
     try:
53
-        manager.updateTorrent(torrent)
58
+        manager.updateTorrent()
54
     except:
59
     except:
55
         warning("could not update")
60
         warning("could not update")
56
 
61
 

+ 5
- 0
torrentfile.py View File

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

+ 14
- 5
torrentmanager.py View File

1
 #! /usr/bin/env python
1
 #! /usr/bin/env python
2
 
2
 
3
+import os
3
 import copy
4
 import copy
4
 import time
5
 import time
5
 import random
6
 import random
11
     last_time = 0
12
     last_time = 0
12
     max_ratio = 0
13
     max_ratio = 0
13
     max_rate = 0
14
     max_rate = 0
14
-    def __init__(self, max_ratio = 1, max_rate = 120):
15
+    def __init__(self, max_ratio, max_rate):
15
         self.max_ratio = max_ratio
16
         self.max_ratio = max_ratio
16
         self.max_rate = max_rate * 1024
17
         self.max_rate = max_rate * 1024
17
 
18
 
18
     def appendTorrent(self, torrent):
19
     def appendTorrent(self, torrent):
19
         self.torrents.append(torrent)
20
         self.torrents.append(torrent)
20
 
21
 
22
+    def removeTorrent(self, torrent):
23
+        self.torrents.remove(torrent)
24
+
21
     def getTorrents(self):
25
     def getTorrents(self):
22
         return self.torrents
26
         return self.torrents
23
 
27
 
40
         t = max(torrent.getTracker().getInterval() - (int(time.time()) - self.last_time), 0)
44
         t = max(torrent.getTracker().getInterval() - (int(time.time()) - self.last_time), 0)
41
         return {'time': t, 'torrent': torrent}
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
         uploaded += random.randint(-uploaded / 10, uploaded / 10)
50
         uploaded += random.randint(-uploaded / 10, uploaded / 10)
48
         self.next_torrents.remove(torrent)
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
         torrent.getTracker().update(uploaded)
58
         torrent.getTracker().update(uploaded)
50
 
59
 

+ 4
- 0
torrenttracker.py View File

60
         self.left -= left
60
         self.left -= left
61
         return self.request()
61
         return self.request()
62
 
62
 
63
+
64
+    def getUploaded(self):
65
+        return self.uploaded
66
+

Loading…
Cancel
Save