def add_callback(self, callback): """ Adds a callback to the polling thread :param callback: a function that is call when a torrent finishes """ if self.polling_thread: self.polling_thread.add_callback(callback) else: self.polling_thread = TorrentCloudPollingThread(self.torrents) self.polling_thread.daemon = True self.polling_thread.add_callback(callback) self.polling_thread.start()
class TorrentCloud(object): def __init__(self, url, customer_id, pin): self.url = url self.customer_id = customer_id self.pin = pin self.headers = {'cookie': 'login={0}:{1}'.format(self.customer_id, self.pin)} self.polling_thread = None def __iter__(self): return self.torrents() def torrents(self): """ Lists all torrents currently added to the users account :returns: an iterator over all torrents """ request = requests.get(self.url.torrent_list, headers=self.headers) request_json = request.json() if request_json['status'] == 'error': raise Exception(request_json['message']) torrents = [self._build_torrent(torrent) for torrent in request_json['torrents']] for torrent in torrents: yield torrent def add(self, link): """ Adds a torrent to the users account, in case it is already cached, it's immediately available for download :param link: a magnet or torrent link """ payload = {'url': link, 'seed': '2or48h'} request = requests.post(self.url.torrent_add, headers=self.headers, data=payload) request_json = request.json() if request_json['status'] == 'error': raise Exception(request_json['message']) def remove(self, hash): """ Removes a torrent from the users account :param hash: the hash of the torrent to be removed """ payload = {'hash': hash.lower()} request = requests.post(self.url.torrent_remove, headers=self.headers, data=payload) request_json = request.json() if request_json['status'] == 'error': raise Exception(request_json['message']) def _build_torrent(self, torrent): """ Creates a torrent object from an API response :param torrent: a json object containing information about the torrent :returns: a Torrent object """ request = partial(requests.get, self.url.torrent_browse, headers=self.headers) eta = leecher = seeder = speed_up = speed_down = percent_done = ratio = 0 if torrent['eta']: eta = int(torrent['eta']) if torrent['leecher']: leecher = int(torrent['leecher']) if torrent['percent_done']: percent_done = float(torrent['percent_done']) if torrent['ratio']: ratio = float(torrent['ratio']) if torrent['seeder']: seeder = int(torrent['seeder']) if torrent['speed_down']: speed_down = float(torrent['speed_down']) if torrent['speed_up']: speed_up = float(torrent['speed_up']) return Torrent(eta, torrent['hash'], leecher, torrent['name'], percent_done, ratio, seeder, torrent['size'], speed_down, speed_up, torrent['status'], request) def add_callback(self, callback): """ Adds a callback to the polling thread :param callback: a function that is call when a torrent finishes """ if self.polling_thread: self.polling_thread.add_callback(callback) else: self.polling_thread = TorrentCloudPollingThread(self.torrents) self.polling_thread.daemon = True self.polling_thread.add_callback(callback) self.polling_thread.start() def remove_callback(self, callback): """ Removes a callback to the polling thread :param callback: a function that is call when a torrent finishes """ self.polling_thread.remove_callback(callback) if not self.polling_thread.callbacks: self.polling_thread.stop() self.polling_thread = None
class TorrentCloud(object): def __init__(self, url, customer_id, pin): self.url = url self.customer_id = customer_id self.pin = pin self.headers = { 'cookie': 'login={0}:{1}'.format(self.customer_id, self.pin) } self.polling_thread = None def __iter__(self): return self.torrents() def torrents(self): """ Lists all torrents currently added to the users account :returns: an iterator over all torrents """ request = requests.get(self.url.torrent_list, headers=self.headers) request_json = request.json() if request_json['status'] == 'error': raise Exception(request_json['message']) torrents = [ self._build_torrent(torrent) for torrent in request_json['torrents'] ] for torrent in torrents: yield torrent def add(self, link): """ Adds a torrent to the users account, in case it is already cached, it's immediately available for download :param link: a magnet or torrent link """ payload = {'url': link, 'seed': '2or48h'} request = requests.post(self.url.torrent_add, headers=self.headers, data=payload) request_json = request.json() if request_json['status'] == 'error': raise Exception(request_json['message']) def remove(self, hash): """ Removes a torrent from the users account :param hash: the hash of the torrent to be removed """ payload = {'hash': hash.lower()} request = requests.post(self.url.torrent_remove, headers=self.headers, data=payload) request_json = request.json() if request_json['status'] == 'error': raise Exception(request_json['message']) def _build_torrent(self, torrent): """ Creates a torrent object from an API response :param torrent: a json object containing information about the torrent :returns: a Torrent object """ request = partial(requests.get, self.url.torrent_browse, headers=self.headers) eta = leecher = seeder = speed_up = speed_down = percent_done = ratio = 0 if torrent['eta']: eta = int(torrent['eta']) if torrent['leecher']: leecher = int(torrent['leecher']) if torrent['percent_done']: percent_done = float(torrent['percent_done']) if torrent['ratio']: ratio = float(torrent['ratio']) if torrent['seeder']: seeder = int(torrent['seeder']) if torrent['speed_down']: speed_down = float(torrent['speed_down']) if torrent['speed_up']: speed_up = float(torrent['speed_up']) return Torrent(eta, torrent['hash'], leecher, torrent['name'], percent_done, ratio, seeder, torrent['size'], speed_down, speed_up, torrent['status'], request) def add_callback(self, callback): """ Adds a callback to the polling thread :param callback: a function that is call when a torrent finishes """ if self.polling_thread: self.polling_thread.add_callback(callback) else: self.polling_thread = TorrentCloudPollingThread(self.torrents) self.polling_thread.daemon = True self.polling_thread.add_callback(callback) self.polling_thread.start() def remove_callback(self, callback): """ Removes a callback to the polling thread :param callback: a function that is call when a torrent finishes """ self.polling_thread.remove_callback(callback) if not self.polling_thread.callbacks: self.polling_thread.stop() self.polling_thread = None