def validChannelCastMsg(channelcast_data): """ Returns true if ChannelCastMsg is valid, format: {'signature':{'publisher_id':, 'publisher_name':, 'infohash':, 'torrenthash':, 'torrent_name':, 'timestamp':, 'signature':}} """ if not isinstance(channelcast_data,dict): return False for signature, ch in channelcast_data.items(): if not isinstance(ch,dict): if DEBUG: print >>sys.stderr,time.asctime(),'-', "rvalidChannelCastMsg: a: value not dict" return False if len(ch) !=6: if DEBUG: print >>sys.stderr,time.asctime(),'-', "rvalidChannelCastMsg: a: #keys!=6" return False if not ('publisher_id' in ch and 'publisher_name' in ch and 'infohash' in ch and 'torrenthash' in ch and 'torrentname' in ch and 'time_stamp' in ch): if DEBUG: print >>sys.stderr,time.asctime(),'-', "validChannelCastMsg: a: key missing, got",d.keys() return False if not (validPermid(ch['publisher_id']) and (isinstance(ch['publisher_name'],str) or isinstance(ch['publisher_name'], unicode)) and validInfohash(ch['infohash']) and validInfohash(ch['torrenthash']) and (isinstance(ch['torrentname'],str) or isinstance(ch['torrentname'],unicode)) and validTimestamp(ch['time_stamp'])): if DEBUG: print >>sys.stderr,time.asctime(),'-', "validChannelCastMsg: something not valid" return False # now, verify signature l = (ch['publisher_id'],ch['infohash'], ch['torrenthash'], ch['time_stamp']) if not verify_data(bencode(l),str2bin(ch['publisher_id']),str2bin(signature)): if DEBUG: print >>sys.stderr, time.asctime(),'-', "validChannelCastMsg: verification failed!" return False return True
def test_hasMyPreference(self): infohash_str_126 = 'ByJho7yj9mWY1ORWgCZykLbU1Xc=' infohash_str_1279 = 'R+grUhp884MnFkt6NuLnnauZFsc=' db = MyPreferenceDBHandler.getInstance() assert db.hasMyPreference(str2bin(infohash_str_126)) assert db.hasMyPreference(str2bin(infohash_str_1279)) fake_infoahsh = 'fake_infohash_1' + '0R0\x10\x00\x07*\x86H\xce=\x02' assert not db.hasMyPreference(fake_infoahsh)
def test_hasMyPreference(self): infohash_str_126 = 'ByJho7yj9mWY1ORWgCZykLbU1Xc=' infohash_str_1279 = 'R+grUhp884MnFkt6NuLnnauZFsc=' db = MyPreferenceDBHandler.getInstance() assert db.hasMyPreference(str2bin(infohash_str_126)) assert db.hasMyPreference(str2bin(infohash_str_1279)) fake_infoahsh = 'fake_infohash_1'+'0R0\x10\x00\x07*\x86H\xce=\x02' assert not db.hasMyPreference(fake_infoahsh)
def _updateChannelcastDB(self, query_permid, query, hits, listOfAdditions): if DEBUG: print >> sys.stderr, "channelcast: updating channelcastdb", query, len(hits) publisher_ids = Set() infohashes = Set() for hit in listOfAdditions: publisher_ids.add(hit[0]) infohashes.add(str2bin(hit[2])) if query and query.startswith('CHANNEL p') and len(publisher_ids) == 1: publisher_id = publisher_ids.pop() publisher_ids.add(publisher_id) nr_torrents = self.channelcastdb.getNrTorrentsInChannel(publisher_id) if len(infohashes) > nr_torrents: if len(infohashes) > 50 and len(infohashes) > nr_torrents +1: #peer not behaving according to spec, ignoring if DEBUG: print >> sys.stderr, "channelcast: peer not behaving according to spec, ignoring",len(infohashes), show_permid(query_permid) return #if my channel, never remove all currently received if bin2str(self.session.get_permid()) != publisher_id: self.channelcastdb.deleteTorrentsFromPublisherId(str2bin(publisher_id)) if DEBUG: print >> sys.stderr, 'Received channelcast message with %d hashes'%len(infohashes), show_permid(query_permid) else: #ignore all my favorites, randomness will cause problems with timeframe my_favorites = self.votecastdb.getPublishersWithPosVote(bin2str(self.session.get_permid())) #filter listOfAdditions listOfAdditions = [hit for hit in listOfAdditions if hit[0] not in my_favorites] #request channeltimeframes for subscribed channels for publisher_id in my_favorites: if publisher_id in publisher_ids: self.updateAChannel(publisher_id, [query_permid]) publisher_ids.remove(publisher_id) #filter publisher_ids #08/04/10: Andrea: processing rich metadata part. self.richMetadataInterceptor.handleRMetadata(query_permid, hits, fromQuery = query is not None) self.channelcastdb.addTorrents(listOfAdditions) missing_infohashes = {} for publisher_id in publisher_ids: for infohash in self.channelcastdb.selectTorrentsToCollect(publisher_id): missing_infohashes[str2bin(infohash[0])] = publisher_id def notify(publisher_id): self.notifier.notify(NTFY_CHANNELCAST, NTFY_UPDATE, publisher_id) for infohash, publisher_id in missing_infohashes.iteritems(): if infohash in infohashes: self.rtorrent_handler.download_torrent(query_permid, infohash, lambda infohash, metadata, filename: notify(publisher_id) ,2) else: self.rtorrent_handler.download_torrent(query_permid, infohash, lambda infohash, metadata, filename: notify(publisher_id) ,3)
def setUp(self): AbstractDB.setUp(self) self.p1 = str2bin('MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAAA6SYI4NHxwQ8P7P8QXgWAP+v8SaMVzF5+fSUHdAMrs6NvL5Epe1nCNSdlBHIjNjEiC5iiwSFZhRLsr') self.p2 = str2bin('MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAABo69alKy95H7RHzvDCsolAurKyrVvtDdT9/DzNAGvky6YejcK4GWQXBkIoQGQgxVEgIn8dwaR9B+3U') fake_permid_x = 'fake_permid_x' + '0R0\x10\x00\x07*\x86H\xce=\x02\x01\x06\x05+\x81\x04\x00\x1a\x03>\x00\x04' hp = self.sqlitedb.hasPeer(fake_permid_x) assert not hp self.pdb = PeerDBHandler.getInstance()
def setUp(self): yield super(TestSqlitePeerDBHandler, self).setUp() self.p1 = str2bin( 'MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAAA6SYI4NHxwQ8P7P8QXgWAP+v8SaMVzF5+fSUHdAMrs6NvL5Epe1nCNSdlBHIjNjEiC5iiwSFZhRLsr') self.p2 = str2bin( 'MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAABo69alKy95H7RHzvDCsolAurKyrVvtDdT9/DzNAGvky6YejcK4GWQXBkIoQGQgxVEgIn8dwaR9B+3U') self.pdb = PeerDBHandler(self.session) self.assertFalse(self.pdb.hasPeer(FAKE_PERMID_X))
def test_getPeerSim(self): db = PeerDBHandler.getInstance() permid_str = 'MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEACPJqLjmKeMNRwkCNKkPH51gjQ5e7u4s2vWv9I/AALXtpf+bFPtY8cyFv6OCzisYDo+brgqOxAtuNZwP' permid = str2bin(permid_str) sim = db.getPeerSim(permid) assert sim == 5.82119645394964 permid_str = 'MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAAB0XbUrw5b8CrTrMZST1SPyrzjgSzIE6ynALtlZASGAb+figVXRRGpKW6MSal3KnEm1/q0P3JPWrhCE' permid = str2bin(permid_str) sim = db.getPeerSim(permid) assert sim == 0
def test_getPeerSim(self): permid_str = 'MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEACPJqLjmKeMNRwkCNKkPH51gjQ5e7u4s2vWv9I/AALXtpf+bFPtY8cyFv6OCzisYDo+brgqOxAtuNZwP' permid = str2bin(permid_str) sim = self.pdb.getPeerSim(permid) # 03/02/10 Boudewijn: In contrast to the content of the # database, the similarity value is not 5.82119645394964 but 0 # because it is reset as the database is upgraded. assert sim == 0 permid_str = 'MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAAB0XbUrw5b8CrTrMZST1SPyrzjgSzIE6ynALtlZASGAb+figVXRRGpKW6MSal3KnEm1/q0P3JPWrhCE' permid = str2bin(permid_str) sim = self.pdb.getPeerSim(permid) assert sim == 0
def test_add_get_torrent_ids_return(self): infohash = str2bin('AA8cTG7ZuPsyblbRE7CyxsrKUCg=') new_infohash = unhexlify('50865489ac16e2f34ea0cd3043cfd970cc24ec09') tids, inserted = self.tdb.addOrGetTorrentIDSReturn( [infohash, new_infohash]) self.assertEqual(tids, [1, 4849]) self.assertEqual(len(inserted), 1)
def setUp(self): db_path = TRIBLER_DB_PATH db = SQLiteCacheDB.getInstance() db.openDB(db_path, lib=LIB, autocommit=AUTOCOMMIT, busytimeout=BUSYTIMEOUT) self.sp1 = '0R0\x10\x06\x07*\x86H\xce=\x02\x01\x06\x05+\x81\x04\x00\x1a\x03>\x00\x04\x00\\\xdfXv\xffX\xf2\xfe\x96\xe1_]\xf5\x1b\xb4\x91\x91\xa5I\xf0nl\x81\xd2A\xfb\xb7u)\x01T\xa9*)r\x9b\x81s\xb7j\xd2\xecrSg$;\xc8"7s\xecSF\xd3\x0bgK\x1c' self.sp2 = '0R0\x10\x06\x07*\x86H\xce=\x02\x01\x06\x05+\x81\x04\x00\x1a\x03>\x00\x04\x01\xdb\x80+O\xd9N7`\xfc\xd3\xdd\xf0 \xfdC^\xc9\xd7@\x97\xaa\x91r\x1c\xdeL\xf2n\x9f\x00U\xc1A\xf9Ae?\xd8t}_c\x08\xb3G\xf8g@N! \xa0\x90M\xfb\xca\xcfZ@' self.fr1 = str2bin('MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAL/l2IyVa6lc3KAqQyEnR++rIzi+AamnbzXHCxOFAFy67COiBhrC79PLzzUiURbHDx21QA4p8w3UDHLA')
def test_hasTorrent(self): infohash_str = 'AA8cTG7ZuPsyblbRE7CyxsrKUCg=' infohash = str2bin(infohash_str) self.assertTrue(self.tdb.hasTorrent(infohash)) self.assertTrue(self.tdb.hasTorrent(infohash)) # cache will trigger fake_infohash = 'fake_infohash_100000' self.assertFalse(self.tdb.hasTorrent(fake_infohash))
def test_hasTorrent(self): infohash_str = 'AA8cTG7ZuPsyblbRE7CyxsrKUCg=' infohash = str2bin(infohash_str) assert self.tdb.hasTorrent(infohash) == True assert self.tdb.hasMetaData(infohash) == True fake_infoahsh = 'fake_infohash_100000' assert self.tdb.hasTorrent(fake_infoahsh) == False assert self.tdb.hasMetaData(fake_infoahsh) == False
def test_updateProgress(self): infohash_str_126 = 'ByJho7yj9mWY1ORWgCZykLbU1Xc=' infohash = str2bin(infohash_str_126) torrent_id = self.mdb._db.getTorrentID(infohash) assert torrent_id == 126 assert self.mdb.hasMyPreference(torrent_id) self.mdb.updateProgress(torrent_id, 3.14) p = self.mdb.getOne('progress', torrent_id=torrent_id) assert p == 3.14
def test_updateProgress(self): infohash_str_126 = 'ByJho7yj9mWY1ORWgCZykLbU1Xc=' infohash = str2bin(infohash_str_126) db = MyPreferenceDBHandler.getInstance() assert db.hasMyPreference(infohash) torrent_id = db._db.getTorrentID(infohash) db.updateProgress(torrent_id, 3.14) p = db.getOne('progress', torrent_id=torrent_id) assert p == 3.14
def _test_hasTorrent(self): infohash_str = 'AA8cTG7ZuPsyblbRE7CyxsrKUCg=' infohash = str2bin(infohash_str) db = TorrentDBHandler.getInstance() assert db.hasTorrent(infohash) == True assert db.hasMetaData(infohash) == True fake_infoahsh = 'fake_infohash_1'+'0R0\x10\x00\x07*\x86H\xce=\x02' assert db.hasTorrent(fake_infoahsh) == False assert db.hasMetaData(fake_infoahsh) == False
def _test_hasTorrent(self): infohash_str = 'AA8cTG7ZuPsyblbRE7CyxsrKUCg=' infohash = str2bin(infohash_str) db = TorrentDBHandler.getInstance() assert db.hasTorrent(infohash) == True assert db.hasMetaData(infohash) == True fake_infoahsh = 'fake_infohash_1' + '0R0\x10\x00\x07*\x86H\xce=\x02' assert db.hasTorrent(fake_infoahsh) == False assert db.hasMetaData(fake_infoahsh) == False
def setUp(self): db_path = TRIBLER_DB_PATH db = SQLiteCacheDB.getInstance() db.openDB(db_path, lib=LIB, autocommit=AUTOCOMMIT, busytimeout=BUSYTIMEOUT) self.sp1 = '0R0\x10\x06\x07*\x86H\xce=\x02\x01\x06\x05+\x81\x04\x00\x1a\x03>\x00\x04\x00\\\xdfXv\xffX\xf2\xfe\x96\xe1_]\xf5\x1b\xb4\x91\x91\xa5I\xf0nl\x81\xd2A\xfb\xb7u)\x01T\xa9*)r\x9b\x81s\xb7j\xd2\xecrSg$;\xc8"7s\xecSF\xd3\x0bgK\x1c' self.sp2 = '0R0\x10\x06\x07*\x86H\xce=\x02\x01\x06\x05+\x81\x04\x00\x1a\x03>\x00\x04\x01\xdb\x80+O\xd9N7`\xfc\xd3\xdd\xf0 \xfdC^\xc9\xd7@\x97\xaa\x91r\x1c\xdeL\xf2n\x9f\x00U\xc1A\xf9Ae?\xd8t}_c\x08\xb3G\xf8g@N! \xa0\x90M\xfb\xca\xcfZ@' self.fr1 = str2bin( 'MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAL/l2IyVa6lc3KAqQyEnR++rIzi+AamnbzXHCxOFAFy67COiBhrC79PLzzUiURbHDx21QA4p8w3UDHLA' )
def test_selecteTorrentToCollect(self): db = PreferenceDBHandler.getInstance() tc = SimpleTorrentCollecting(None, None) truth = {3127: 235, 994: 20, 19: 1, 5: 0} for pid in truth: pl = db.getPrefList(str2bin(self.permid[pid])) assert len(pl) == truth[pid], [pid, len(pl)] # test random selection infohash = tc.selecteTorrentToCollect(pl, True) if pid == 994 or pid == 3127: assert len(infohash) == 20, infohash else: assert infohash is None, infohash #tc.updateAllCooccurrence() for pid in truth: pl = db.getPrefList(str2bin(self.permid[pid])) assert len(pl) == truth[pid], [pid, len(pl)] # test selecting most relevant torrent infohash = tc.selecteTorrentToCollect(pl, False) if pid == 994: tid = tc.torrent_db.getTorrentID(infohash) assert tid == 8979 permid = self.permid[pid] infohash = tc.updatePreferences(permid, pl) tid = tc.torrent_db.getTorrentID(infohash) assert tid == 8979 elif pid == 3127: tid = tc.torrent_db.getTorrentID(infohash) assert tid == 9170 permid = self.permid[pid] infohash = tc.updatePreferences(permid, pl) tid = tc.torrent_db.getTorrentID(infohash) assert tid == 9170 else: assert infohash is None, infohash
def test_selecteTorrentToCollect(self): db = PreferenceDBHandler.getInstance() tc = SimpleTorrentCollecting(None,None) truth = {3127:235, 994:20, 19:1, 5:0} for pid in truth: pl = db.getPrefList(str2bin(self.permid[pid])) assert len(pl) == truth[pid], [pid, len(pl)] # test random selection infohash = tc.selecteTorrentToCollect(pl, True) if pid == 994 or pid == 3127: assert len(infohash) == 20, infohash else: assert infohash is None, infohash #tc.updateAllCooccurrence() for pid in truth: pl = db.getPrefList(str2bin(self.permid[pid])) assert len(pl) == truth[pid], [pid, len(pl)] # test selecting most relevant torrent infohash = tc.selecteTorrentToCollect(pl, False) if pid == 994: tid = tc.torrent_db.getTorrentID(infohash) assert tid == 8979 permid = self.permid[pid] infohash = tc.updatePreferences(permid, pl) tid = tc.torrent_db.getTorrentID(infohash) assert tid == 8979 elif pid == 3127: tid = tc.torrent_db.getTorrentID(infohash) assert tid == 9170 permid = self.permid[pid] infohash = tc.updatePreferences(permid, pl) tid = tc.torrent_db.getTorrentID(infohash) assert tid == 9170 else: assert infohash is None, infohash
def test_getPrefList(self): db = PreferenceDBHandler.getInstance() fake_permid_x = 'fake_permid_x'+'0R0\x10\x00\x07*\x86H\xce=\x02\x01\x06\x05+\x81\x04\x00\x1a\x03>\x00\x04' pl = db.getPrefList(fake_permid_x) assert pl == [], pl truth = {3127:235, 994:20, 19:1, 5:0} permid = {} permid[3127] = 'MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAcPezgQ13k1MSOaUrCPisWRhYuNT7Tm+q5rUgHFvAWd9b+BcSut6TCniEgHYHDnQ6TH/vxQBqtY8Loag' permid[994] = 'MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAJUNmwvDaigRaM4cj7cE2O7lessqnnFEQsan7df9AZS8xeNmVsP/XXVrEt4t7e2TNicYmjn34st/sx2P' permid[19] = 'MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAAJv2YLuIWa4QEdOEs4CPRxQZDwZphKd/xK/tgbcALG198nNdT10znJ2sZYl+OJIvj7YfYp75PrrnWNX' permid[5] = 'MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAAB0XbUrw5b8CrTrMZST1SPyrzjgSzIE6ynALtlZASGAb+figVXRRGpKW6MSal3KnEm1/q0P3JPWrhCE' for pid in truth: pl = db.getPrefList(str2bin(permid[pid])) assert len(pl) == truth[pid], [pid, len(pl)]
def updateChannel(self,query_permid, query, hits): """ This function is called when there is a reply from remote peer regarding updating of a channel @param query_permid: the peer who returned the results @param query: the query string @param hits: details of all matching results related to the query """ records = [] for k,v in hits.items(): records.append((v['publisher_id'],v['publisher_name'],v['infohash'],v['torrenthash'],v['torrentname'],v['time_stamp'],k)) for hit in records: if self.channelcastdb.existsTorrent(hit[2]): self.channelcastdb.addTorrent(hit) self.hits.append(hit) else: def usercallback(infohash,metadata,filename): print >> sys.stderr , time.asctime(),'-', "USERCALLBACK" self.channelcastdb.addTorrent(hit) self.hits.append(hit) self.rtorrent_handler.download_torrent(query_permid,str2bin(hit[2]),usercallback)
def test_getPrefList(self): db = PreferenceDBHandler.getInstance() fake_permid_x = 'fake_permid_x' + '0R0\x10\x00\x07*\x86H\xce=\x02\x01\x06\x05+\x81\x04\x00\x1a\x03>\x00\x04' pl = db.getPrefList(fake_permid_x) assert pl == [], pl truth = {3127: 235, 994: 20, 19: 1, 5: 0} permid = {} permid[ 3127] = 'MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAcPezgQ13k1MSOaUrCPisWRhYuNT7Tm+q5rUgHFvAWd9b+BcSut6TCniEgHYHDnQ6TH/vxQBqtY8Loag' permid[ 994] = 'MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAJUNmwvDaigRaM4cj7cE2O7lessqnnFEQsan7df9AZS8xeNmVsP/XXVrEt4t7e2TNicYmjn34st/sx2P' permid[ 19] = 'MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAAJv2YLuIWa4QEdOEs4CPRxQZDwZphKd/xK/tgbcALG198nNdT10znJ2sZYl+OJIvj7YfYp75PrrnWNX' permid[ 5] = 'MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAAB0XbUrw5b8CrTrMZST1SPyrzjgSzIE6ynALtlZASGAb+figVXRRGpKW6MSal3KnEm1/q0P3JPWrhCE' for pid in truth: pl = db.getPrefList(str2bin(permid[pid])) assert len(pl) == truth[pid], [pid, len(pl)]
def deleteTorrent(self): s_infohash = unhexlify('44865489ac16e2f34ea0cd3043cfd970cc24ec09') m_infohash = unhexlify('ed81da94d21ad1b305133f2726cdaec5a57fed98') assert self.tdb.deleteTorrent(s_infohash, delete_file=True) assert self.tdb.deleteTorrent(m_infohash) assert not self.tdb.hasTorrent(s_infohash) assert not self.tdb.hasTorrent(m_infohash) assert not os.path.isfile(os.path.join(self.getStateDir(), 'single.torrent')) m_trackers = self.tdb.getTracker(m_infohash, 0) assert len(m_trackers) == 0 # fake_infoahsh = 'fake_infohash_1'+'0R0\x10\x00\x07*\x86H\xce=\x02' # 02/02/10 Boudewijn: infohashes must be 20 bytes long fake_infoahsh = 'fake_infohash_1' + '0R0\x10\x00' assert not self.tdb.deleteTorrent(fake_infoahsh) my_infohash_str_126 = 'ByJho7yj9mWY1ORWgCZykLbU1Xc=' my_infohash = str2bin(my_infohash_str_126) assert not self.tdb.deleteTorrent(my_infohash)
def _test_deleteTorrent(self): mydb = MyDBHandler.getInstance() mydb.put('torrent_dir', FILES_DIR) db = TorrentDBHandler.getInstance() s_infohash = unhexlify('44865489ac16e2f34ea0cd3043cfd970cc24ec09') m_infohash = unhexlify('ed81da94d21ad1b305133f2726cdaec5a57fed98') assert db.deleteTorrent(s_infohash, delete_file=True) assert db.deleteTorrent(m_infohash) assert not db.hasTorrent(s_infohash) assert not db.hasTorrent(m_infohash) assert not os.path.isfile(S_TORRENT_PATH) m_trackers = db.getTracker(m_infohash, 0) assert len(m_trackers) == 0 fake_infoahsh = 'fake_infohash_1'+'0R0\x10\x00\x07*\x86H\xce=\x02' assert not db.deleteTorrent(fake_infoahsh) my_infohash_str_126 = 'ByJho7yj9mWY1ORWgCZykLbU1Xc=' my_infohash = str2bin(my_infohash_str_126) assert not db.deleteTorrent(my_infohash)
def _test_deleteTorrent(self): mydb = MyDBHandler.getInstance() mydb.put('torrent_dir', FILES_DIR) db = TorrentDBHandler.getInstance() s_infohash = unhexlify('44865489ac16e2f34ea0cd3043cfd970cc24ec09') m_infohash = unhexlify('ed81da94d21ad1b305133f2726cdaec5a57fed98') assert db.deleteTorrent(s_infohash, delete_file=True) assert db.deleteTorrent(m_infohash) assert not db.hasTorrent(s_infohash) assert not db.hasTorrent(m_infohash) assert not os.path.isfile(S_TORRENT_PATH) m_trackers = db.getTracker(m_infohash, 0) assert len(m_trackers) == 0 fake_infoahsh = 'fake_infohash_1' + '0R0\x10\x00\x07*\x86H\xce=\x02' assert not db.deleteTorrent(fake_infoahsh) my_infohash_str_126 = 'ByJho7yj9mWY1ORWgCZykLbU1Xc=' my_infohash = str2bin(my_infohash_str_126) assert not db.deleteTorrent(my_infohash)
torrent["retried_times"] += 1 def tooMuchRetry(self, torrent): if (torrent["retried_times"] > self.retryThreshold): return True return False if __name__ == '__main__': from Tribler.Core.CacheDB.sqlitecachedb import init as init_db, str2bin configure_dir = sys.argv[1] config = {} config['state_dir'] = configure_dir config['install_dir'] = '.' config['peer_icon_path'] = '.' init_db(config) t = TorrentChecking() t.start() t.join() infohash_str = 'TkFX5S4qd2DPW63La/VObgOH/Nc=' infohash = str2bin(infohash_str) del t t = TorrentChecking(infohash) t.start() t.join()
def test_select_torrents_to_collect(self): infohash = str2bin('AA8cTG7ZuPsyblbRE7CyxsrKUCg=') self.assertEqual(len(self.tdb.select_torrents_to_collect(infohash)), 0)
def OnCreate(self, event): self.createBtn.Disable() self.createBtn.SetForegroundColour(wx.Colour(150,150,150)) save = True choice = self.opponentType.GetSelection() min_rating = 1 max_rating = 100 peer_id = -1 #Retrieve min & max ratings, if given if choice == 0: # If we are creating a random invite, first check if there are connected peers gcg = GameCastGossip.getInstance() if (len(gcg.connected_game_buddies) + len(gcg.connected_random_peers)) == 0: dialog = wx.MessageDialog(None, 'There are currectly no peers connected, please try again later.', 'No connected peers', wx.OK | wx.ICON_EXCLAMATION) dialog.ShowModal() self.createBtn.Enable() self.createBtn.SetForegroundColour(wx.WHITE) return text1 = self.minRating.GetValue().strip() text2 = self.maxRating.GetValue().strip() if text1 != '': if text1.isdigit() and int(text1) >= 0 and int(text1) <= 9999: min_rating = int(text1) else: save = False if sys.platform != 'darwin': self.minRating.SetForegroundColour(wx.RED) self.minRating.SetValue('Error') if text2 != '': if text2.isdigit() and int(text2) >= 0 and int(text2) <= 9999: max_rating = int(text2) else: save = False if sys.platform != 'darwin': self.maxRating.SetForegroundColour(wx.RED) self.maxRating.SetValue('Error') if min_rating > max_rating: save = False if sys.platform != 'darwin': self.minRating.SetForegroundColour(wx.RED) self.maxRating.SetForegroundColour(wx.RED) self.minRating.SetValue('Error') self.maxRating.SetValue('Error') #Retrieve permid, if given elif choice == 1: text = self.permid.GetValue().strip() try: peer_id = self.peer_db.getPeerID(str2bin(text)) except: peer_id = None if not peer_id: save = False if sys.platform != 'darwin': self.permid.SetForegroundColour(wx.RED) self.permid.SetValue('Error') #Retrieve the colour that the player will use colour = self.colour.GetStringSelection() #Retrieve the clock time that each of the players should start with text = self.movetime.GetValue().strip() if text != '': if text.isdigit() and int(text) >= 1 and int(text) <= 999: time = int(text) else: save = False if sys.platform != 'darwin': # on mac can't reset the font colour back to black again self.movetime.SetForegroundColour(wx.RED) self.movetime.SetValue('Error') #Retrieve the time with which the player's clock is incremented per move text = self.inctime.GetValue().strip() if text != '': if text.isdigit() and int(text) >= 0 and int(text) <= 999: inc = int(text) else: save = False if sys.platform != 'darwin': # on mac can't reset the font colour back to black again self.inctime.SetForegroundColour(wx.RED) self.inctime.SetValue('Error') if save: # Create a game dict with the appropriate keys, which we will add to the db game = {} game['game_id'] = self.gamecast_db.getNextGameID(0) game['owner_id'] = 0 game['winner_permid'] = '' game['moves'] = bencode([]) mypermid = bin2str(self.session.get_permid()) players = {} players[mypermid] = colour game['players'] = bencode(players) game['gamename'] = 'chess' game['time'] = time game['inc'] = inc game['is_finished'] = 0 game['lastmove_time'] = 0 game['creation_time'] = 0 self.gamecast_db.addGame(game) # For every game we can add multiple invites. Invites are either targetted at a specific peer, # or at any peer that has a rating within a certain range. invite = {} invite['target_id'] = peer_id invite['game_id'] = game['game_id'] invite['min_rating'] = min_rating invite['max_rating'] = max_rating invite['time'] = time invite['inc'] = inc invite['gamename'] = 'chess' if colour == 'white': invite['colour'] = 'black' else: invite['colour'] = 'white' gc = GameCast.getInstance() gc._executeSeekOrMatch(invite) self.LoadMyInvites() wx.Yield() self.createBtn.Enable() self.createBtn.SetForegroundColour(wx.WHITE)
def test_add_get_torrent_id(self): infohash = str2bin('AA8cTG7ZuPsyblbRE7CyxsrKUCg=') self.assertEqual(self.tdb.addOrGetTorrentID(infohash), 1) new_infohash = unhexlify('50865489ac16e2f34ea0cd3043cfd970cc24ec09') self.assertEqual(self.tdb.addOrGetTorrentID(new_infohash), 4849)
def test_getCreationTime(self): db = MyPreferenceDBHandler.getInstance() infohash_str_126 = 'ByJho7yj9mWY1ORWgCZykLbU1Xc=' infohash = str2bin(infohash_str_126) ct = db.getCreationTime(infohash) assert ct == 1194966300, ct
def test_add_external_torrent_no_def_existing(self): infohash = str2bin('AA8cTG7ZuPsyblbRE7CyxsrKUCg=') self.tdb.addExternalTorrentNoDef(infohash, "test torrent", [], [], 1234) self.assertTrue(self.tdb.hasTorrent(infohash))
def test_getCreationTime(self): infohash_str_126 = 'ByJho7yj9mWY1ORWgCZykLbU1Xc=' infohash = str2bin(infohash_str_126) ct = self.mdb.getCreationTime(infohash) assert ct == 1194966300, ct
def _upgrade_22_to_23(self): """ Migrates the database to the new version. """ self.status_update_func(u"Upgrading database from v%s to v%s..." % (22, 23)) self.db.execute(u""" DROP TABLE IF EXISTS BarterCast; DROP INDEX IF EXISTS bartercast_idx; DROP INDEX IF EXISTS Torrent_swift_torrent_hash_idx; """) try: next(self.db.execute(u"SELECT * From sqlite_master WHERE name == '_tmp_Torrent' and type == 'table';")) except StopIteration: # no _tmp_Torrent table, check if the current Torrent table is new lines = [(0, u'torrent_id', u'integer', 1, None, 1), (1, u'infohash', u'text', 1, None, 0), (2, u'name', u'text', 0, None, 0), (3, u'torrent_file_name', u'text', 0, None, 0), (4, u'length', u'integer', 0, None, 0), (5, u'creation_date', u'integer', 0, None, 0), (6, u'num_files', u'integer', 0, None, 0), (7, u'thumbnail', u'integer', 0, None, 0), (8, u'insert_time', u'numeric', 0, None, 0), (9, u'secret', u'integer', 0, None, 0), (10, u'relevance', u'numeric', 0, u'0', 0), (11, u'source_id', u'integer', 0, None, 0), (12, u'category_id', u'integer', 0, None, 0), (13, u'status_id', u'integer', 0, u'0', 0), (14, u'num_seeders', u'integer', 0, None, 0), (15, u'num_leechers', u'integer', 0, None, 0), (16, u'comment', u'text', 0, None, 0), (17, u'dispersy_id', u'integer', 0, None, 0), (18, u'last_tracker_check', u'integer', 0, u'0', 0), (19, u'tracker_check_retries', u'integer', 0, u'0', 0), (20, u'next_tracker_check', u'integer', 0, u'0', 0) ] i = 0 is_new = True for line in self.db.execute(u"PRAGMA table_info(Torrent);"): if line != lines[i]: is_new = False break i += 1 if not is_new: # create the temporary table self.db.execute(u""" CREATE TABLE IF NOT EXISTS _tmp_Torrent ( torrent_id integer PRIMARY KEY AUTOINCREMENT NOT NULL, infohash text NOT NULL, name text, torrent_file_name text, length integer, creation_date integer, num_files integer, thumbnail integer, insert_time numeric, secret integer, relevance numeric DEFAULT 0, source_id integer, category_id integer, status_id integer DEFAULT 0, num_seeders integer, num_leechers integer, comment text, dispersy_id integer, last_tracker_check integer DEFAULT 0, tracker_check_retries integer DEFAULT 0, next_tracker_check integer DEFAULT 0 ); """) # migrate Torrent table keys = (u"torrent_id", u"infohash", u"name", u"torrent_file_name", u"length", u"creation_date", u"num_files", u"thumbnail", u"insert_time", u"secret", u"relevance", u"source_id", u"category_id", u"status_id", u"num_seeders", u"num_leechers", u"comment", u"dispersy_id", u"last_tracker_check", u"tracker_check_retries", u"next_tracker_check") keys_str = u", ".join(keys) values_str = u"?," * len(keys) insert_stmt = u"INSERT INTO _tmp_Torrent(%s) VALUES(%s)" % (keys_str, values_str[:-1]) current_count = 0 results = self.db.execute(u"SELECT %s FROM Torrent;" % keys_str) new_torrents = [] for torrent in results: torrent_id, infohash, name, torrent_file_name = torrent[:4] filepath = os.path.join(self.torrent_collecting_dir, hexlify(str2bin(infohash)) + u".torrent") # Check if we have the actual .torrent torrent_file_name = None if os.path.exists(filepath): torrent_file_name = filepath tdef = TorrentDef.load(filepath) # Use the name on the .torrent file instead of the one stored in the database. name = tdef.get_name_as_unicode() or name new_torrents.append((torrent_id, infohash, name, torrent_file_name) + torrent[4:]) current_count += 1 self.status_update_func(u"Upgrading database, %s records upgraded..." % current_count) self.status_update_func(u"All torrent entries processed, inserting in database...") self.db.executemany(insert_stmt, new_torrents) self.status_update_func(u"All updated torrent entries inserted.") self.db.execute(u""" DROP TABLE IF EXISTS Torrent; ALTER TABLE _tmp_Torrent RENAME TO Torrent; """) # cleanup metadata tables self.db.execute(u""" DROP TABLE IF EXISTS MetadataMessage; DROP TABLE IF EXISTS MetadataData; CREATE TABLE IF NOT EXISTS MetadataMessage ( message_id INTEGER PRIMARY KEY AUTOINCREMENT, dispersy_id INTEGER NOT NULL, this_global_time INTEGER NOT NULL, this_mid TEXT NOT NULL, infohash TEXT NOT NULL, previous_mid TEXT, previous_global_time INTEGER ); CREATE TABLE IF NOT EXISTS MetadataData ( message_id INTEGER, data_key TEXT NOT NULL, data_value INTEGER, FOREIGN KEY (message_id) REFERENCES MetadataMessage(message_id) ON DELETE CASCADE ); """) # cleanup all SearchCommunity and MetadataCommunity data in dispersy database self._purge_old_search_metadata_communities() # update database version self.db.write_version(23)
def test_my_pref_stats_infohash(self): infohash = str2bin('AB8cTG7ZuPsyblbRE7CyxsrKUCg=') self.assertIsNone(self.mdb.getMyPrefStatsInfohash(infohash)) infohash = str2bin('ByJho7yj9mWY1ORWgCZykLbU1Xc=') self.assertTrue(self.mdb.getMyPrefStatsInfohash(infohash))
def test_add_get_torrent_ids_return(self): infohash = str2bin('AA8cTG7ZuPsyblbRE7CyxsrKUCg=') new_infohash = unhexlify('50865489ac16e2f34ea0cd3043cfd970cc24ec09') tids, inserted = self.tdb.addOrGetTorrentIDSReturn([infohash, new_infohash]) self.assertEqual(tids, [1, 4859]) self.assertEqual(len(inserted), 1)
def test_add_get_torrent_id(self): infohash = str2bin('AA8cTG7ZuPsyblbRE7CyxsrKUCg=') self.assertEqual(self.tdb.addOrGetTorrentID(infohash), 1) new_infohash = unhexlify('50865489ac16e2f34ea0cd3043cfd970cc24ec09') self.assertEqual(self.tdb.addOrGetTorrentID(new_infohash), 4859)
def _upgrade_22_to_23(self): """ Migrates the database to the new version. """ self.status_update_func(u"Upgrading database from v%s to v%s..." % (22, 23)) self.db.execute(u""" DROP TABLE IF EXISTS BarterCast; DROP INDEX IF EXISTS bartercast_idx; DROP INDEX IF EXISTS Torrent_swift_torrent_hash_idx; """) try: next( self.db.execute( u"SELECT * From sqlite_master WHERE name == '_tmp_Torrent' and type == 'table';" )) except StopIteration: # no _tmp_Torrent table, check if the current Torrent table is new lines = [(0, u'torrent_id', u'integer', 1, None, 1), (1, u'infohash', u'text', 1, None, 0), (2, u'name', u'text', 0, None, 0), (3, u'torrent_file_name', u'text', 0, None, 0), (4, u'length', u'integer', 0, None, 0), (5, u'creation_date', u'integer', 0, None, 0), (6, u'num_files', u'integer', 0, None, 0), (7, u'thumbnail', u'integer', 0, None, 0), (8, u'insert_time', u'numeric', 0, None, 0), (9, u'secret', u'integer', 0, None, 0), (10, u'relevance', u'numeric', 0, u'0', 0), (11, u'source_id', u'integer', 0, None, 0), (12, u'category_id', u'integer', 0, None, 0), (13, u'status_id', u'integer', 0, u'0', 0), (14, u'num_seeders', u'integer', 0, None, 0), (15, u'num_leechers', u'integer', 0, None, 0), (16, u'comment', u'text', 0, None, 0), (17, u'dispersy_id', u'integer', 0, None, 0), (18, u'last_tracker_check', u'integer', 0, u'0', 0), (19, u'tracker_check_retries', u'integer', 0, u'0', 0), (20, u'next_tracker_check', u'integer', 0, u'0', 0)] i = 0 is_new = True for line in self.db.execute(u"PRAGMA table_info(Torrent);"): if line != lines[i]: is_new = False break i += 1 if not is_new: # create the temporary table self.db.execute(u""" CREATE TABLE IF NOT EXISTS _tmp_Torrent ( torrent_id integer PRIMARY KEY AUTOINCREMENT NOT NULL, infohash text NOT NULL, name text, torrent_file_name text, length integer, creation_date integer, num_files integer, thumbnail integer, insert_time numeric, secret integer, relevance numeric DEFAULT 0, source_id integer, category_id integer, status_id integer DEFAULT 0, num_seeders integer, num_leechers integer, comment text, dispersy_id integer, last_tracker_check integer DEFAULT 0, tracker_check_retries integer DEFAULT 0, next_tracker_check integer DEFAULT 0 ); """) # migrate Torrent table keys = (u"torrent_id", u"infohash", u"name", u"torrent_file_name", u"length", u"creation_date", u"num_files", u"thumbnail", u"insert_time", u"secret", u"relevance", u"source_id", u"category_id", u"status_id", u"num_seeders", u"num_leechers", u"comment", u"dispersy_id", u"last_tracker_check", u"tracker_check_retries", u"next_tracker_check") keys_str = u", ".join(keys) values_str = u"?," * len(keys) insert_stmt = u"INSERT INTO _tmp_Torrent(%s) VALUES(%s)" % ( keys_str, values_str[:-1]) current_count = 0 results = self.db.execute(u"SELECT %s FROM Torrent;" % keys_str) new_torrents = [] for torrent in results: torrent_id, infohash, name, torrent_file_name = torrent[:4] filepath = os.path.join( self.torrent_collecting_dir, hexlify(str2bin(infohash)) + u".torrent") # Check if we have the actual .torrent torrent_file_name = None if os.path.exists(filepath): torrent_file_name = filepath tdef = TorrentDef.load(filepath) # Use the name on the .torrent file instead of the one stored in the database. name = tdef.get_name_as_unicode() or name new_torrents.append((torrent_id, infohash, name, torrent_file_name) + torrent[4:]) current_count += 1 self.status_update_func( u"Upgrading database, %s records upgraded..." % current_count) self.status_update_func( u"All torrent entries processed, inserting in database...") self.db.executemany(insert_stmt, new_torrents) self.status_update_func( u"All updated torrent entries inserted.") self.db.execute(u""" DROP TABLE IF EXISTS Torrent; ALTER TABLE _tmp_Torrent RENAME TO Torrent; """) # cleanup metadata tables self.db.execute(u""" DROP TABLE IF EXISTS MetadataMessage; DROP TABLE IF EXISTS MetadataData; CREATE TABLE IF NOT EXISTS MetadataMessage ( message_id INTEGER PRIMARY KEY AUTOINCREMENT, dispersy_id INTEGER NOT NULL, this_global_time INTEGER NOT NULL, this_mid TEXT NOT NULL, infohash TEXT NOT NULL, previous_mid TEXT, previous_global_time INTEGER ); CREATE TABLE IF NOT EXISTS MetadataData ( message_id INTEGER, data_key TEXT NOT NULL, data_value INTEGER, FOREIGN KEY (message_id) REFERENCES MetadataMessage(message_id) ON DELETE CASCADE ); """) # cleanup all SearchCommunity and MetadataCommunity data in dispersy database self._purge_old_search_metadata_communities() # update database version self.db.write_version(23)
def test_get_dns_from_peerdb(self): permid_str_id_1 = 'MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAAA6SYI4NHxwQ8P7P8QXgWAP+v8SaMVzF5+fSUHdAMrs6NvL5Epe1nCNSdlBHIjNjEiC5iiwSFZhRLsr' permid = str2bin(permid_str_id_1) self.loadData(2500) assert self.datahandler.get_dns_from_peerdb(permid) == ( '68.108.115.221', 6881)
self.torrentdb.getTorrentsFromTracker( tracker, max_last_check, 10 - len(infohashes))) if DEBUG: print >> sys.stderr, "Torrent Checking: Returning %d additional infohashes for tracker %s" % ( len(infohashes), tracker) return infohashes except UnicodeDecodeError: if isLocked: self.queueLock.release() return [] if __name__ == '__main__': from Tribler.Core.CacheDB.sqlitecachedb import init as init_db, str2bin configure_dir = sys.argv[1] config = {} config['state_dir'] = configure_dir config['install_dir'] = '.' config['peer_icon_path'] = '.' init_db(config) t = TorrentChecking() t.start() infohash_str = 'TkFX5S4qd2DPW63La/VObgOH/Nc=' infohash = str2bin(infohash_str) t.addToQueue(infohash)
def test_get_dns_from_peerdb(self): permid_str_id_1 = 'MFIwEAYHKoZIzj0CAQYFK4EEABoDPgAEAAA6SYI4NHxwQ8P7P8QXgWAP+v8SaMVzF5+fSUHdAMrs6NvL5Epe1nCNSdlBHIjNjEiC5iiwSFZhRLsr' permid = str2bin(permid_str_id_1) self.loadData(2500) assert self.datahandler.get_dns_from_peerdb(permid) == ('68.108.115.221', 6881)