def test_add_torrent_with_settings_success(self, qbittorrent_client): rpc_client = qbittorrent_client.return_value rpc_client.torrents_add.return_value = 'Ok.' plugin = QBittorrentClientPlugin() settings = self.DEFAULT_SETTINGS plugin.set_settings(settings) torrent = b'torrent' self.assertEqual('Ok.', plugin.add_torrent(torrent, TopicSettings("/path/to/download")))
def test_add_torrent_with_settings(self, deluge_client): rpc_client = deluge_client.return_value rpc_client.connected = True plugin = DelugeClientPlugin() settings = {'host': 'localhost', 'username': '******', 'password': '******'} plugin.set_settings(settings) rpc_client.call.return_value = True torrent = b'!torrent.content' self.assertTrue(plugin.add_torrent(torrent, TopicSettings('/path/to/download'))) options = { 'download_location': '/path/to/download' } rpc_client.call.assert_called_once_with('core.add_torrent_file', None, base64.b64encode(torrent), options)
def test_add_torrent_with_settings(self, transmission_client): rpc_client = transmission_client.return_value plugin = TransmissionClientPlugin() settings = { 'host': 'localhost', 'username': '******', 'password': '******' } plugin.set_settings(settings) torrent = b'!torrent.content' self.assertTrue( plugin.add_torrent(torrent, TopicSettings('/path/to/download/dir'))) rpc_client.add_torrent.assert_called_once_with( base64.b64encode(torrent).decode('utf-8'), download_dir='/path/to/download/dir')
def execute(self, topics, engine): """ :param topics: result of get_topics func :type engine: Engine :return: None """ for topic in topics: topic_name = topic.display_name try: engine.log.info(u"Check for changes <b>%s</b>" % topic_name) prepared_request = self._prepare_request(topic) download_kwargs = {} if isinstance(prepared_request, tuple) and len(prepared_request) >= 2: download_kwargs = prepared_request[1] or download_kwargs prepared_request = prepared_request[0] download_kwargs.setdefault('timeout', self.tracker_settings.requests_timeout) response, filename = download(prepared_request, **download_kwargs) if hasattr(self, 'check_download'): status = self.check_download(response) if topic.status != status: self.save_topic(topic, None, status) if status != Status.Ok: engine.log.failed(u"Torrent status changed: {}".format(status)) continue elif response.status_code != 200: raise Exception("Can't download url. Status: {}".format(response.status_code)) if not filename: filename = topic_name engine.log.info(u"Downloading <b>%s</b> torrent" % filename) torrent_content = response.content torrent = Torrent(torrent_content) old_hash = topic.hash if torrent.info_hash != old_hash: engine.log.downloaded(u"Torrent <b>%s</b> was changed" % topic_name, torrent_content) last_update = engine.add_torrent(filename, torrent, old_hash, TopicSettings.from_topic(topic)) topic.hash = torrent.info_hash topic.last_update = last_update self.save_topic(topic, last_update, Status.Ok) else: engine.log.info(u"Torrent <b>%s</b> not changed" % topic_name) except Exception as e: engine.log.failed(u"Failed update <b>%s</b>.\nReason: %s" % (topic_name, html.escape(str(e))))
def test_add_torrent_with_settings_success(self, post_mock): response = Response() response._content = b"Ok." response.status_code = 200 good_response = Response() good_response.status_code = 200 post_mock.side_effect = [response, good_response] plugin = QBittorrentClientPlugin() settings = { 'host': self.real_host, 'port': self.real_port, 'username': self.real_login, 'password': self.real_password } plugin.set_settings(settings) torrent = b'torrent' self.assertTrue( plugin.add_torrent(torrent, TopicSettings("/path/to/download")))
def execute(self, topics, engine): """ :param topics: result of get_topics func :type engine: engine.EngineTracker :return: None """ with engine.start(len(topics)) as engine_topics: for i in range(0, len(topics)): topic = topics[i] topic_name = topic.display_name with engine_topics.start(i, topic_name) as engine_topic: changed = False if hasattr(self, 'check_changes'): changed = self.check_changes(topic) if not changed: continue prepared_request = self._prepare_request(topic) download_kwargs = dict( self.tracker_settings.get_requests_kwargs()) if isinstance(prepared_request, tuple) and len(prepared_request) >= 2: if prepared_request[1] is not None: download_kwargs.update(prepared_request[1]) prepared_request = prepared_request[0] response, filename = download(prepared_request, **download_kwargs) if hasattr(self, 'check_download'): status = self.check_download(response) if topic.status != status: self.save_status(topic.id, status) engine_topic.status_changed(topic.status, status) if status != Status.Ok: continue elif response.status_code != 200: raise Exception( u"Can't download url. Status: {}".format( response.status_code)) if not filename: filename = topic_name torrent_content = response.content if not is_torrent_content(torrent_content): headers = [ '{0}: {1}'.format(k, v) for k, v in six.iteritems(response.headers) ] engine.failed( u'Downloaded content is not a torrent file.<br>\r\n' u'Headers:<br>\r\n{0}'.format( u'<br>\r\n'.join(headers))) continue torrent = Torrent(torrent_content) old_hash = topic.hash if torrent.info_hash != old_hash: with engine_topic.start(1) as engine_downloads: last_update = engine_downloads.add_torrent( 0, filename, torrent, old_hash, TopicSettings.from_topic(topic)) engine.downloaded( u"Torrent <b>{0}</b> was changed".format( topic_name), torrent_content) topic.hash = torrent.info_hash topic.last_update = last_update self.save_topic(topic, last_update, Status.Ok) elif changed: engine.info( u"Torrent <b>{0}</b> was determined as changed, but torrent hash wasn't" .format(topic_name)) self.save_topic(topic, None, Status.Ok)
def execute(self, topics, engine): """ :param topics: result of get_topics func :type engine: engine.EngineTracker :rtype: None """ if not self._execute_login(engine): return with engine.start(len(topics)) as engine_topics: for i in range(0, len(topics)): topic = topics[i] display_name = topic.display_name with engine_topics.start(i, display_name) as engine_topic: episodes = self._prepare_request(topic) status = Status.Ok if isinstance(episodes, Response): status = self.check_download(episodes) if topic.status != status: self.save_topic(topic, None, status) engine_topic.status_changed(topic.status, status) if status != Status.Ok: continue if episodes is None or len(episodes) == 0: engine_topic.info( u"Series <b>{0}</b> not changed".format( display_name)) continue with engine_topic.start(len(episodes)) as engine_downloads: for e in range(0, len(episodes)): info, download_info = episodes[e] if download_info is None: engine_downloads.failed( u'Failed get quality "{0}" for series: {1}' .format(topic.quality, html.escape(display_name))) # Should fail to get quality be treated as NotFound? self.save_topic(topic, None, Status.Error) break try: response, filename = download( download_info.download_url, **self.tracker_settings. get_requests_kwargs()) if response.status_code != 200: raise Exception( u"Can't download url. Status: {}". format(response.status_code)) except Exception as e: engine_downloads.failed( u"Failed to download from <b>{0}</b>.\nReason: {1}" .format(download_info.download_url, html.escape(str(e)))) self.save_topic(topic, None, Status.Error) continue if not filename: filename = display_name torrent_content = response.content if not is_torrent_content(torrent_content): headers = [ '{0}: {1}'.format(k, v) for k, v in six.iteritems(response.headers) ] engine.failed( u'Downloaded content is not a torrent file.<br>\r\n' u'Headers:<br>\r\n{0}'.format( u'<br>\r\n'.join(headers))) continue torrent = Torrent(torrent_content) topic.season = info.season topic.episode = info.number last_update = engine_downloads.add_torrent( e, filename, torrent, None, TopicSettings.from_topic(topic)) engine_downloads.downloaded( u'Download new series: {0} ({1}, {2})'.format( display_name, info.season, info.number), torrent_content) self.save_topic(topic, last_update, Status.Ok)
def execute(self, topics, engine): """ :param topics: result of get_topics func :type engine: engine.Engine :rtype: None """ if not self._execute_login(engine): return for topic in topics: try: display_name = topic.display_name episodes = self._prepare_request(topic) status = Status.Ok if isinstance(episodes, Response): status = self.check_download(episodes) if topic.status != status: self.save_topic(topic, None, status) if status != Status.Ok: engine.log.failed(u"Torrent status changed: {}".format(status)) continue if episodes is None or len(episodes) == 0: engine.log.info(u"Series <b>{0}</b> not changed".format(display_name)) continue for episode in episodes: info = episode['season_info'] download_info = episode['download_info'] if download_info is None: engine.log.failed(u'Failed get quality "{0}" for series: {1}' .format(topic.quality, html.escape(display_name))) # Should fail to get quality be treated as NotFound? self.save_topic(topic, None, Status.Error) break try: response, filename = download(download_info['download_url'], **self.tracker_settings.get_requests_kwargs()) if response.status_code != 200: raise Exception("Can't download url. Status: {}".format(response.status_code)) except Exception as e: engine.log.failed(u"Failed to download from <b>{0}</b>.\nReason: {1}" .format(download_info['download_url'], html.escape(str(e)))) self.save_topic(topic, None, Status.Error) continue if not filename: filename = display_name torrent_content = response.content torrent = Torrent(torrent_content) engine.log.downloaded(u'Download new series: {0} ({1}, {2})' .format(display_name, info[0], info[1]), torrent_content) topic.season = info[0] topic.episode = info[1] last_update = engine.add_torrent(filename, torrent, None, TopicSettings.from_topic(topic)) self.save_topic(topic, last_update, Status.Ok) except Exception as e: engine.log.failed(u"Failed update <b>lostfilm</b> series: {0}.\nReason: {1}" .format(topic.search_name, html.escape(str(e))))