コード例 #1
0
ファイル: test_payload.py プロジェクト: xiaomuweb/tribler
    def test_search_result_payload_serialization(self):
        """ Test serialization & deserialization of search payload """
        # sample search response items
        sample_items = []
        for index in range(10):
            infohash = self.random_infohash()
            name = self.random_string()
            length = random.randint(1000, 9999)
            num_files = random.randint(1, 10)
            category_list = ['video', 'audio']
            creation_date = random.randint(1000000, 111111111)
            seeders = random.randint(10, 200)
            leechers = random.randint(5, 1000)
            cid = self.random_string(size=20)

            sample_items.append(
                SearchResponseItemPayload(infohash, name, length, num_files,
                                          category_list, creation_date,
                                          seeders, leechers, cid))

        # Search identifier
        identifier = 111
        response_type = 1

        # Serialize the results
        results = ''
        for item in sample_items:
            results += self.serializer.pack_multiple(item.to_pack_list())[0]
        serialized_results = self.serializer.pack_multiple(
            SearchResponsePayload(identifier, response_type,
                                  results).to_pack_list())[0]

        # De-serialize the response payload and check the identifier and get the results
        response_format = SearchResponsePayload.format_list
        (search_results,
         _) = self.serializer.unpack_multiple(response_format,
                                              serialized_results)

        # De-serialize each individual search result items
        item_format = SearchResponseItemPayload.format_list
        (all_items,
         _) = self.serializer.unpack_multiple_as_list(item_format,
                                                      search_results[2])
        for index in xrange(len(all_items)):
            response_item = SearchResponseItemPayload.from_unpack_list(
                *all_items[index])
            sample_item = sample_items[index]

            self.assertEqual(sample_item.infohash, response_item.infohash)
            self.assertEqual(sample_item.name, response_item.name)
            self.assertEqual(sample_item.length, response_item.length)
            self.assertEqual(sample_item.num_files, response_item.num_files)
            self.assertEqual(sample_item.creation_date,
                             response_item.creation_date)
            self.assertEqual(sample_item.category_list,
                             response_item.category_list)
            self.assertEqual(sample_item.seeders, response_item.seeders)
            self.assertEqual(sample_item.leechers, response_item.leechers)
            self.assertEqual(sample_item.cid, response_item.cid)
コード例 #2
0
ファイル: repository.py プロジェクト: map-lijie/tribler
    def update_from_torrent_search_results(self, search_results):
        """
        Updates the torrent database with the provided search results. It also checks for conflicting torrents, meaning
        if torrent already exists in the database, we simply ignore the search result.
        """
        for result in search_results:
            (infohash, name, length, num_files, category_list, creation_date,
             seeders, leechers, cid) = result
            torrent_item = SearchResponseItemPayload(infohash, name, length,
                                                     num_files, category_list,
                                                     creation_date, seeders,
                                                     leechers, cid)
            if self.has_torrent(infohash):
                db_torrent = self.get_torrent(infohash)
                if db_torrent['name'] and db_torrent[
                        'name'] == torrent_item.name:
                    self.logger.info(
                        "Conflicting names for torrent. Ignoring the response")
                    continue
            else:
                self.logger.debug(
                    "Adding new torrent from search results to database")
                self.torrent_db.addOrGetTorrentID(infohash)

            # Update local database
            self.torrent_db.updateTorrent(
                infohash,
                notify=False,
                name=torrent_item.name,
                length=torrent_item.length,
                creation_date=torrent_item.creation_date,
                num_files=torrent_item.num_files,
                comment='')
コード例 #3
0
    def search_torrent(self, query):
        """
        Searches for best torrents for the given query and packs them into a list of SearchResponseItemPayload.
        :param query: Search query
        :return: List<SearchResponseItemPayload>
        """

        db_results = self.torrent_db.searchNames(query, local=True,
                                                 keys=['infohash', 'T.name', 'T.length', 'T.num_files', 'T.category',
                                                       'T.creation_date', 'T.num_seeders', 'T.num_leechers'])
        if not db_results:
            return []

        results = []
        for dbresult in db_results:
            channel_details = dbresult[-10:]

            dbresult = list(dbresult[:8])
            dbresult[2] = long(dbresult[2])  # length
            dbresult[3] = int(dbresult[3])  # num_files
            dbresult[4] = [dbresult[4]]  # category
            dbresult[5] = long(dbresult[5])  # creation_date
            dbresult[6] = int(dbresult[6] or 0)  # num_seeders
            dbresult[7] = int(dbresult[7] or 0)  # num_leechers

            # cid
            if channel_details[1]:
                channel_details[1] = str(channel_details[1])
            dbresult.append(channel_details[1])

            results.append(SearchResponseItemPayload(*tuple(dbresult)))

        return results
コード例 #4
0
ファイル: test_payload.py プロジェクト: synctext/tribler
    def test_search_result_payload_serialization(self):
        """ Test serialization & deserialization of search payload """
        # sample search response items
        sample_items = []
        for index in range(10):
            infohash = self.random_infohash()
            name = self.random_string()
            length = random.randint(1000, 9999)
            num_files = random.randint(1, 10)
            category_list = ['video', 'audio']
            creation_date = random.randint(1000000, 111111111)
            seeders = random.randint(10, 200)
            leechers = random.randint(5, 1000)
            cid = self.random_string(size=20)

            sample_items.append(SearchResponseItemPayload(infohash, name, length, num_files, category_list,
                                                          creation_date, seeders, leechers, cid))

        # Search identifier
        identifier = 111
        response_type = 1

        # Serialize the results
        results = ''
        for item in sample_items:
            results += self.serializer.pack_multiple(item.to_pack_list())[0]
        serialized_results = self.serializer.pack_multiple(
            SearchResponsePayload(identifier, response_type, results).to_pack_list())[0]

        # De-serialize the response payload and check the identifier and get the results
        response_format = SearchResponsePayload.format_list
        (search_results, _) = self.serializer.unpack_multiple(response_format, serialized_results)

        # De-serialize each individual search result items
        item_format = SearchResponseItemPayload.format_list
        (all_items, _) = self.serializer.unpack_multiple_as_list(item_format, search_results[2])
        for index in xrange(len(all_items)):
            response_item = SearchResponseItemPayload.from_unpack_list(*all_items[index])
            sample_item = sample_items[index]

            self.assertEqual(sample_item.infohash, response_item.infohash)
            self.assertEqual(sample_item.name, response_item.name)
            self.assertEqual(sample_item.length, response_item.length)
            self.assertEqual(sample_item.num_files, response_item.num_files)
            self.assertEqual(sample_item.creation_date, response_item.creation_date)
            self.assertEqual(sample_item.category_list, response_item.category_list)
            self.assertEqual(sample_item.seeders, response_item.seeders)
            self.assertEqual(sample_item.leechers, response_item.leechers)
            self.assertEqual(sample_item.cid, response_item.cid)
コード例 #5
0
 def search_torrent(self, _):
     sample_items = []
     for torrent in self.sample_torrents:
         sample_items.append(SearchResponseItemPayload(*torrent))
     return sample_items