#! /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 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, uploaded, downloaded, left, event=""): params = {"info_hash": self.torrent.getHash().digest(), "peer_id": self.peer_id, "port": self.port, "uploaded": uploaded, "downloaded": downloaded, "left": 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): return self.request(0, 0, 0, "started") def stop(self, uploaded, downloaded, left): return self.request(0, 0, 0, "stopped") def complete(self, uploaded): return self.request(0, 0, 0, "completed") def interval(self, uploaded, downloaded, left): return self.request(uploaded, downloaded, left)