def test_get_torrents_by_status(self): torrents = PDbManager.get_torrents_by_status(Torrent._STATUS_NEW) self.assertEqual(len(self.torrents), len(torrents)) self.assertIsInstance(torrents[0], Torrent) with self.assertRaises(ValueError): torrents = PDbManager.get_torrents_by_status(Torrent._STATUS_UNFOLLOW)
def compute_trending(test, **kwargs): if test: logger.debug("get_trending task") return trendings_movies = [] torrents_manager = TorrentsManager(config, PDbManager, CATEGORY_NAME['movies']) try: with db_factory.get_instance(): # trendings_torrents = PDbManager.get_trending_torrents_by_category(Torrent._CAT_MOVIE, mindate, maxdate, delta_hours) trendings_torrents = torrents_manager.torrents_trending trendings_movies = movies_from_torrents( [t for t, _, _ in trendings_torrents]) except ValueError as err: logger.warning(err) return with db_factory.get_instance(): for movie in trendings_movies: try: PDbManager.imdb_object_to_db(movie, update=True) except Exception as err: logger.error('Error during imdb object creation (%s): %s' % (movie.imdb_id, err))
def setUp(self): super().setUp() # init mock tracker self.result_of_scrape = { '85be94b120becfb44f94f97779c61633c7647629': { 'complete': 10, 'downloaded': 100, 'incomplete': 32, } } mock_tracker = UdpTracker(scheme="udp", netloc="netloc:6060", name="tracker_2", path="") # type(mock_tracker).name = PropertyMock(return_value="tracker_name") mock_tracker.scrape = MagicMock(return_value=self.result_of_scrape) self.mock_tracker = mock_tracker # init db self.fake_torrent = Torrent( info_hash="85be94b120becfb44f94f97779c61633c7647629", name='Test 1 titre', pub_date=datetime.datetime.now(), size=1220) PDbManager.save_torrent_tracker(self.fake_torrent, self.mock_tracker)
def test_get_torrents_by_tracker(self): torrents = PDbManager.get_torrents_by_tracker(self.trackers[0]) self.assertEqual(1, len(torrents)) self.assertIsInstance(torrents[0], Torrent) torrents = PDbManager.get_torrents_by_tracker(self.trackers[2]) self.assertEqual(torrents, [])
def torrents_stats_with_tracker(tracker: Tracker, category: list = None): nb_stats = 0 with db_factory.get_instance(): torrents = PDbManager.get_torrents_by_tracker( tracker, status=[Torrent._STATUS_NEW, Torrent._STATUS_FOLLOW], category=category) logger.debug("Torrents number: %s", len(torrents)) stats_scraper = stats.StatsScraper(tracker) stats_scraper.torrents = torrents stats_scraper.run_by_batch() stats_collection = stats_scraper.stats_collection logger.debug("Stats number: %s", stats_collection.count()) if stats_collection.count() != len(torrents): logger.warning("Statistics count is wrong %s/%s", stats_collection.count(), len(torrents)) with db_factory.get_instance(): for tracker_stats in stats_collection.stats: _, created = PDbManager.save_stats(tracker_stats) nb_stats += created return nb_stats
def test_save_stats(self): for stat in self.stats: db_stats, created = PDbManager.save_stats(stat) self.assertIsInstance(db_stats, PStats) self.assertTrue(created) db_stats, created = PDbManager.save_stats(self.stats[0]) self.assertIsInstance(db_stats, PStats) self.assertFalse(created)
def test_update_torrent(self): db_torrent, updated = PDbManager.update(self.torrents[0]) self.assertIsInstance(db_torrent, PTorrent) self.assertFalse(updated) self.torrents[0].name = "name_1" db_torrent, updated = PDbManager.update(self.torrents[0]) self.assertIsInstance(db_torrent, PTorrent) self.assertTrue(updated)
def test_update_tracker(self): db_tracker, updated = PDbManager.update(self.trackers[0]) self.assertIsInstance(db_tracker, PTracker) self.assertFalse(updated) self.trackers[0]._path = '/path/updated' db_tracker, updated = PDbManager.update(self.trackers[0]) self.assertIsInstance(db_tracker, PTracker) self.assertTrue(updated)
def test_db_to_imdb_object(self): db_imdb_obj = PIMDBObject.get_or_none(imdb_id="00455") imdb_obj = PDbManager.db_to_movie(db_imdb_obj, []) self.assertEqual(imdb_obj.genres, ["genre1", "genre2"]) self.assertEqual(imdb_obj.language_codes, None) db_imdb_obj = PIMDBObject.get_or_none(imdb_id="6563") imdb_obj = PDbManager.db_to_movie(db_imdb_obj, []) self.assertEqual(imdb_obj.genres, None) self.assertEqual(imdb_obj.language_codes, ['si'])
def test_get_stats_collection_by_torrent(self): stats_collection = PDbManager.get_stats_collection_by_torrent(self.torrents[0]) self.assertEqual(stats_collection.count(), 2) stats_collection = PDbManager.get_stats_collection_by_torrent(self.torrents[1]) self.assertEqual(stats_collection.count(), 1) self.assertEqual(stats_collection.stats[0].leechers, 10) stats_collection = PDbManager.get_stats_collection_by_torrent(self.torrents[2]) self.assertEqual(stats_collection.count(), 0)
def setUpClass(cls): indexers_config = { "indexer_1": { "movies": { "active": True, "action": "search", "params": { "cat": 102183 } }, "wrong_cat": { "active": False, "action": "search" } }, "indexer_2": { "series": { "active": True, "action": "search", "params": { "cat": 102185 } } } } cls.indexers_patch = patch.dict('mediastrends.indexers_config', indexers_config, clear=True) cls.indexers_mock = cls.indexers_patch.start() trackers_config = { 'tracker_1': { 'active': True, 'scheme': 'http', 'netloc': 'netloc:8080', }, 'tracker_2': { 'active': True, 'scheme': 'udp', 'netloc': 'netloc:6060', }, 'tracker_3': { 'active': False, 'scheme': 'udp', 'netloc': 'netloc:5217', }, } cls.trackers_patch = patch.dict('mediastrends.trackers_config', trackers_config, clear=True) cls.trackers_mock = cls.trackers_patch.start() # db db_factory.defaut_instance = 'sqlite-app-test' PDbManager.create_database(db_factory.get_instance())
def get_trending(test, category: list = None, mindate=None, maxdate=None, delta_hours=1, **kwargs): if category is not None: category = [CATEGORY_NAME.get(c) for c in category] results = None if test: logger.debug("get_trending task") return try: with db_factory.get_instance(): results = PDbManager.get_trending_torrents_by_category( category, mindate, maxdate, delta_hours) for t, score, valid_date in results: print("%s / %s / %s" % (t, score, valid_date)) except ValueError as err: logger.warning(err) return results
def test_imdb_object_to_db(self): imdb_objs = self.movies for idx, imdb_obj in enumerate(imdb_objs): db_imdb_obj, torrents_updated = PDbManager.imdb_object_to_db(imdb_obj) self.assertIsInstance(db_imdb_obj, PIMDBObject) self.assertEqual(torrents_updated, 0) if db_imdb_obj.genres: self.assertEqual(self.movies[idx].genres, db_imdb_obj.genres.split(';'))
def test_get_torrent_by_hash(self): torrent = PDbManager.get_torrent_by_hash(self.torrents[2].info_hash) self.assertEqual(torrent.info_hash, self.torrents[2].info_hash) self.assertEqual(torrent.name, self.torrents[2].name) self.assertEqual(torrent.pub_date, self.torrents[2].pub_date) self.assertEqual(torrent.size, self.torrents[2].size) self.assertEqual(torrent._status, self.torrents[2]._status) self.assertEqual(torrent._category, self.torrents[2]._category)
def test_torrents_add(self, mock_rss, mock_client, mock_tf): instance_rss = mock_rss.return_value instance_rss.items = self.rss_parser_items instance_rss.process_items.return_value = True mock_tf.return_value = {'tracker_urls': ['http://netloc:8080']} nb_torrents_added = torrents_add(False, "indexer_1", ["movies"]) self.assertTrue(mock_client.called) self.assertTrue(mock_rss.called) self.assertTrue(mock_tf.called) self.assertEqual(nb_torrents_added, 2) self.assertEqual( PDbManager.get_tracker_by_name("tracker_1").netloc, "netloc:8080") self.assertEqual( PDbManager.get_torrent_by_hash( "85be94b120becfb44f94f97779c61633c7647628").imdb_id, "2438644") nb_torrents_added = torrents_add(False, "indexer_1", ["series"]) self.assertEqual(nb_torrents_added, 0)
def test_get_stats_collections_by_status(self): max_date = datetime.datetime.now() + datetime.timedelta(days=1) stats_collections = PDbManager.get_stats_collections_by_status(Torrent._STATUS_NEW, max_date=max_date) # nombre de torrents self.assertEqual(len(stats_collections), 3) nb_stats = 0 for stats_collection in stats_collections: nb_stats += len(stats_collection.stats) self.assertEqual(len(self.stats), nb_stats)
def test_save_torrent_tracker(self): torrent = self.torrents[0] tracker = self.trackers[0] db_torrent_tracker, created, torrent_created, tracker_created = PDbManager.save_torrent_tracker(torrent, tracker) self.assertIsInstance(db_torrent_tracker, PTorrentTracker) self.assertTrue(created) self.assertFalse(torrent_created) self.assertFalse(tracker_created) db_torrent_tracker_query = PTorrentTracker.select().first() self.assertEqual(db_torrent_tracker, db_torrent_tracker_query)
def torrents_add(test, indexer: str, category: list = None, **kwargs): nb_torrent = 0 if test: logger.debug("torrents_add task") return nb_torrent assert category is not None assert indexer in indexers_config for cat in category: logger.debug("Category: %s" % cat) logger.debug("Indexer: %s" % indexer) if cat not in ['movies', 'series']: logger.error("category must be movies or series " % cat) break try: client = create_torznab_from_cli_params(indexer, cat) except Exception as err: logger.error("Error during jacket creation: %s" % str(err)) break try: rss_content = client.get_rss_content() rss_parser = TorznabJackettRSS(rss_content) rss_parser.process_items() except Exception as err: logger.error("Error while contacting jackett: %s" % str(err)) if len(rss_parser.items) == 0: logger.warning('RSS feed is empty') break db = db_factory.get_instance() for item in rss_parser.items: try: torznab_result = elements_from_torznab_result(item) if not torznab_result['keep']: pass with db: for tracker in torznab_result['trackers']: _, _, to_created, _ = PDbManager.save_torrent_tracker( torrent=torznab_result['torrent'], tracker=tracker) nb_torrent += to_created except Exception as err: logger.error("Error during elements creation: %s" % str(err)) pass logger.debug("%s torrents added" % nb_torrent) return nb_torrent
def get_trending(test, mindate=None, maxdate=None, delta_hours=1, **kwargs): results = None if test: logger.debug("get_trending task") return try: with db_factory.get_instance(): results = PDbManager.get_trending_movies(mindate, maxdate, delta_hours) for item in results: print(item) except ValueError as err: logger.warning(err) return results
def test_torrents_stats_with_tracker(self): nb_stats = torrents_stats_with_tracker(self.mock_tracker) self.assertEqual(nb_stats, 1) stats_collec = PDbManager.get_stats_collection_by_torrent( self.fake_torrent) self.assertEqual(nb_stats, stats_collec.count()) self.assertEqual(stats_collec.stats[0].leechers, 32) self.assertEqual(stats_collec.stats[0].seeders, 10) self.assertEqual(stats_collec.stats[0].completed, 100) self.assertEqual(stats_collec.stats[0].torrent.name, self.fake_torrent.name) self.assertEqual(stats_collec.stats[0].tracker.name, self.mock_tracker.name) nb_stats = torrents_stats_with_tracker(self.mock_tracker, [0]) self.assertEqual(nb_stats, 0) nb_stats = torrents_stats_with_tracker(self.mock_tracker, []) self.assertEqual(nb_stats, 0)
def test_tracker_to_db(self): for tracker in self.trackers: db_tracker, created, updated = PDbManager.tracker_to_db(tracker) self.assertIsInstance(db_tracker, PTracker) self.assertTrue(created) self.assertFalse(updated)
def tearDownModule(): PDbManager.drop_database(db_factory.get_instance())
def setUpModule(): db_factory.defaut_instance = 'sqlite-app-test' PDbManager.create_database(db_factory.get_instance())
def test_get_max_trend_date_by_category(self): max_date = PDbManager.get_max_trend_date_by_category(Torrent._CAT_MOVIE) self.assertGreaterEqual(self.stats[2].valid_date, max_date) self.assertGreater(max_date, self.stats[1].valid_date)
def test_get_trending_torrents_by_category(self): torrents = PDbManager.get_trending_torrents_by_category(Torrent._CAT_MOVIE) self.assertEqual(2, len(torrents)) self.assertEqual(self.stats_collections[0]._score, torrents[0][1])
def test_update_movie(self): with self.assertRaises(ValueError): db_movie, updated = PDbManager.update(self.movies[0]) db_imdb_obj, torrents_updated = PDbManager.movie_to_db(self.movies[0], update=True) self.assertIsInstance(db_imdb_obj, PIMDBObject)
def test_tracker_to_db_already_exist(self): db_tracker, created, updated = PDbManager.tracker_to_db(self.trackers[0]) self.assertIsInstance(db_tracker, PTracker) self.assertFalse(created) self.assertFalse(updated)
def test_save_stats_collection_as_trends(self): for stats_collection in self.stats_collections: db_trends, created = PDbManager.save_stats_collection_as_trends(stats_collection) self.assertIsInstance(db_trends, PTrends) self.assertTrue(created)
def test_torrent_to_db(self): for torrent in self.torrents: db_torrent, created, updated = PDbManager.torrent_to_db(torrent) self.assertIsInstance(db_torrent, PTorrent) self.assertTrue(created) self.assertFalse(updated)
def test_get_tracker_by_name(self): tracker = PDbManager.get_tracker_by_name(self.trackers[2].name) self.assertEqual(tracker.name, self.trackers[2].name) self.assertEqual(tracker.path, self.trackers[2].path) self.assertEqual(tracker.netloc, self.trackers[2].netloc) self.assertEqual(tracker.scheme, self.trackers[2].scheme)