1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- #! /usr/bin/env python
-
- import requests
- import time
- import hashlib
- import bencode
-
- class TorrentTracker:
- torrent = None
- session = None
- peer_id = ""
- port = 0
- user_agent = ""
- interval = 300
- uploaded = 0
- downloaded = 0
- left = 0
-
- def __init__(self, torrent, port=6882, user_agent="rtorrent/0.8.42/0.12.42"):
- self.torrent = torrent
- self.session = requests.session()
- self.peer_id = hashlib.sha1(str(time.time())).digest()
- self.port = port
- self.user_agent = user_agent
-
- def getInterval(self):
- return self.interval
-
- def request(self, event=""):
- params = {"info_hash": self.torrent.getHash().digest(),
- "peer_id": self.peer_id,
- "port": self.port,
- "uploaded": self.uploaded,
- "downloaded": self.downloaded,
- "left": self.left,
- "event": event}
- headers = {"User-Agent": self.user_agent}
- r = self.session.get(self.torrent.getAnnounce(), headers=headers, params=params)
- data = bencode.bdecode(r.content)
- if 'interval' in data:
- self.interval = int(data['interval'])
- return data;
-
- def start(self, left = 0):
- self.uploaded = 0
- self.downloaded = 0
- self.left = left
- return self.request("started")
-
- def stop(self):
- return self.request("stopped")
-
- def complete(self):
- self.left = 0
- return self.request("completed")
-
- def update(self, uploaded, downloaded = 0, left = 0):
- self.uploaded += uploaded
- self.downloaded += downloaded
- self.left -= left
- return self.request()
|