def test_time_convert(): """ Test converting various datetime objects to float """ test_time_list = [ datetime.datetime(2005, 7, 14, 12, 30, 12), datetime.datetime(2039, 7, 14, 12, 30, 12), datetime.datetime.utcnow().replace(second=0, microsecond=0), ] for test_time in test_time_list: assert test_time == int2time(time2int(test_time))
def test_negative_time(): """ Test whether we are able to deal with time below the epoch time """ negative_time = EPOCH - datetime.timedelta(1) assert negative_time == int2time(time2int(negative_time))
def test_zero_time(self): """ Test whether a time of zero converts to the epoch time """ self.assertTrue(int2time(0.0) == EPOCH)
def test_zero_time(): """ Test whether a time of zero converts to the epoch time """ assert int2time(0.0) == EPOCH
def get_old_torrents(self, personal_channel_only=False, batch_size=10000, offset=0, sign=False): with contextlib.closing(sqlite3.connect(self.tribler_db)) as connection, connection: cursor = connection.cursor() cursor.execute("PRAGMA temp_store = 2") personal_channel_filter = "" if self.personal_channel_id: equality_sign = " == " if personal_channel_only else " != " personal_channel_filter = f"AND ct.channel_id {equality_sign} {self.personal_channel_id}" torrents = [] batch_not_empty = False # This is a dumb way to indicate that this batch got zero entries from DB for tracker_url, channel_id, name, infohash, length, creation_date, torrent_id, category, num_seeders, \ num_leechers, last_tracker_check in cursor.execute( f"{self.select_full} {personal_channel_filter} group by infohash " f"LIMIT {batch_size} OFFSET {offset}" ): batch_not_empty = True # check if name is valid unicode data try: name = str(name) except UnicodeDecodeError: continue try: invalid_decoding = len(base64.decodebytes(infohash.encode('utf-8'))) != 20 invalid_id = not torrent_id or int(torrent_id) == 0 invalid_length = not length or (int(length) <= 0) or (int(length) > (1 << 45)) invalid_name = not name or is_forbidden(name) if invalid_decoding or invalid_id or invalid_length or invalid_name: continue infohash = base64.decodebytes(infohash.encode()) torrent_date = datetime.datetime.utcfromtimestamp(creation_date or 0) torrent_date = torrent_date if 0 <= time2int(torrent_date) <= self.conversion_start_timestamp_int \ else int2time(0) torrent_dict = { "status": NEW, "infohash": infohash, "size": int(length), "torrent_date": torrent_date, "title": name or '', "tags": category or '', "tracker_info": tracker_url or '', "xxx": int(category == u'xxx')} if not sign: torrent_dict.update({"origin_id": infohash_to_id(channel_id)}) seeders = int(num_seeders or 0) leechers = int(num_leechers or 0) last_tracker_check = int(last_tracker_check or 0) health_dict = { "seeders": seeders, "leechers": leechers, "last_check": last_tracker_check } if (last_tracker_check >= 0 and seeders >= 0 and leechers >= 0) else None torrents.append((torrent_dict, health_dict)) except Exception as e: self._logger.warning("During retrieval of old torrents an exception was raised: %s", e) continue return torrents if batch_not_empty else None