當前位置: 首頁>>代碼示例>>Python>>正文


Python TorrentDBHandler.addOrGetTorrentID方法代碼示例

本文整理匯總了Python中Tribler.Core.CacheDB.SqliteCacheDBHandler.TorrentDBHandler.addOrGetTorrentID方法的典型用法代碼示例。如果您正苦於以下問題:Python TorrentDBHandler.addOrGetTorrentID方法的具體用法?Python TorrentDBHandler.addOrGetTorrentID怎麽用?Python TorrentDBHandler.addOrGetTorrentID使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Tribler.Core.CacheDB.SqliteCacheDBHandler.TorrentDBHandler的用法示例。


在下文中一共展示了TorrentDBHandler.addOrGetTorrentID方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: TestTorrentDBHandler

# 需要導入模塊: from Tribler.Core.CacheDB.SqliteCacheDBHandler import TorrentDBHandler [as 別名]
# 或者: from Tribler.Core.CacheDB.SqliteCacheDBHandler.TorrentDBHandler import addOrGetTorrentID [as 別名]

#.........這裏部分代碼省略.........
        self.tdb.torrent_dir = TESTS_DATA_DIR
        self.tdb.category = Category()
        self.tdb.mypref_db = MyPreferenceDBHandler(self.session)

    @inlineCallbacks
    def tearDown(self):
        self.tdb.mypref_db.close()
        self.tdb.mypref_db = None
        self.tdb.close()
        self.tdb = None

        yield super(TestTorrentDBHandler, self).tearDown()

    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_get_infohash(self):
        self.assertTrue(self.tdb.getInfohash(1))
        self.assertFalse(self.tdb.getInfohash(1234567))

    def test_add_update_torrent(self):
        self.addTorrent()
        self.updateTorrent()

    def test_update_torrent_from_metainfo(self):
        # Add torrent first
        infohash = unhexlify('ed81da94d21ad1b305133f2726cdaec5a57fed98')
        # Only infohash is added to the database
        self.tdb.addOrGetTorrentID(infohash)

        # Then update the torrent with metainfo
        metainfo = {'info': {'files': [{'path': ['Something.something.pdf'], 'length': 123456789},
                                       {'path': ['Another-thing.jpg'], 'length': 100000000}],
                             'piece length': 2097152,
                             'name': '\xc3Something awesome (2015)',
                             'pieces': ''},
                    'seeders': 0, 'initial peers': [],
                    'leechers': 36, 'download_exists': False, 'nodes': []}
        self.tdb.update_torrent_with_metainfo(infohash, metainfo)

        # Check updates are correct
        torrent_id = self.tdb.getTorrentID(infohash)
        name = self.tdb.getOne('name', torrent_id=torrent_id)
        self.assertEqual(name, u'\xc3Something awesome (2015)')
        num_files = self.tdb.getOne('num_files', torrent_id=torrent_id)
        self.assertEqual(num_files, 2)
        length = self.tdb.getOne('length', torrent_id=torrent_id)
        self.assertEqual(length, 223456789)

    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_add_external_torrent_no_def_no_files(self):
        infohash = unhexlify('48865489ac16e2f34ea0cd3043cfd970cc24ec09')
        self.tdb.addExternalTorrentNoDef(infohash, "test torrent", [], [], 1234)
        self.assertFalse(self.tdb.hasTorrent(infohash))

    def test_add_external_torrent_no_def_one_file(self):
        infohash = unhexlify('49865489ac16e2f34ea0cd3043cfd970cc24ec09')
開發者ID:synctext,項目名稱:tribler,代碼行數:70,代碼來源:test_sqlitecachedbhandler_torrents.py

示例2: TriblerLaunchMany

# 需要導入模塊: from Tribler.Core.CacheDB.SqliteCacheDBHandler import TorrentDBHandler [as 別名]
# 或者: from Tribler.Core.CacheDB.SqliteCacheDBHandler.TorrentDBHandler import addOrGetTorrentID [as 別名]

#.........這裏部分代碼省略.........
            except OSError:
                self._logger.error("Unable to create the download destination directory.")

            if dscfg.get_time_added() == 0:
                dscfg.set_time_added(int(timemod.time()))

            # Check if running or saved on disk
            if infohash in self.downloads:
                self._logger.info("Torrent already exists in the downloads. Infohash:%s", infohash.encode('hex'))

            from Tribler.Core.Libtorrent.LibtorrentDownloadImpl import LibtorrentDownloadImpl
            d = LibtorrentDownloadImpl(self.session, tdef)

            if pstate is None:  # not already resuming
                pstate = self.load_download_pstate_noexc(infohash)
                if pstate is not None:
                    self._logger.debug("tlm: add: pstate is %s %s",
                                       pstate.get('dlstate', 'status'), pstate.get('dlstate', 'progress'))

            # Store in list of Downloads, always.
            self.downloads[infohash] = d
            setup_deferred = d.setup(dscfg, pstate, wrapperDelay=setupDelay,
                                     share_mode=share_mode, checkpoint_disabled=checkpoint_disabled)
            setup_deferred.addCallback(self.on_download_handle_created)

        if d and not hidden and self.session.config.get_megacache_enabled():
            @forceDBThread
            def write_my_pref():
                torrent_id = self.torrent_db.getTorrentID(infohash)
                data = {'destination_path': d.get_dest_dir()}
                self.mypref_db.addMyPreference(torrent_id, data)

            if isinstance(tdef, TorrentDefNoMetainfo):
                self.torrent_db.addOrGetTorrentID(tdef.get_infohash())
                self.torrent_db.updateTorrent(tdef.get_infohash(), name=tdef.get_name_as_unicode())
                self.torrent_db._db.commit_now()
                write_my_pref()
            elif self.rtorrent_handler:
                self.rtorrent_handler.save_torrent(tdef, write_my_pref)
            else:
                self.torrent_db.addExternalTorrent(tdef, extra_info={'status': 'good'})
                write_my_pref()

        return d

    def on_download_handle_created(self, download):
        """
        This method is called when the download handle has been created.
        Immediately checkpoint the download and write the resume data.
        """
        return download.checkpoint()

    def remove(self, d, removecontent=False, removestate=True, hidden=False):
        """ Called by any thread """
        out = None
        with self.session_lock:
            out = d.stop_remove(removestate=removestate, removecontent=removecontent)
            infohash = d.get_def().get_infohash()
            if infohash in self.downloads:
                del self.downloads[infohash]

        if not hidden:
            self.remove_id(infohash)

        if self.tunnel_community:
            self.tunnel_community.on_download_removed(d)
開發者ID:synctext,項目名稱:tribler,代碼行數:70,代碼來源:LaunchManyCore.py

示例3: TriblerLaunchMany

# 需要導入模塊: from Tribler.Core.CacheDB.SqliteCacheDBHandler import TorrentDBHandler [as 別名]
# 或者: from Tribler.Core.CacheDB.SqliteCacheDBHandler.TorrentDBHandler import addOrGetTorrentID [as 別名]

#.........這裏部分代碼省略.........
                raise ValueError("TorrentDef not finalized")

            infohash = tdef.get_infohash()

            # Check if running or saved on disk
            if infohash in self.downloads:
                raise DuplicateDownloadException()

            from Tribler.Core.Libtorrent.LibtorrentDownloadImpl import LibtorrentDownloadImpl
            d = LibtorrentDownloadImpl(self.session, tdef)

            if pstate is None:  # not already resuming
                pstate = self.load_download_pstate_noexc(infohash)
                if pstate is not None:
                    self._logger.debug("tlm: add: pstate is %s %s",
                                       pstate.get('dlstate', 'status'), pstate.get('dlstate', 'progress'))

            # Store in list of Downloads, always.
            self.downloads[infohash] = d
            d.setup(dscfg, pstate, initialdlstatus, self.network_engine_wrapper_created_callback,
                    wrapperDelay=setupDelay)

        finally:
            self.sesslock.release()

        if d and not hidden and self.session.get_megacache():
            @forceDBThread
            def write_my_pref():
                torrent_id = self.torrent_db.getTorrentID(infohash)
                data = {'destination_path': d.get_dest_dir()}
                self.mypref_db.addMyPreference(torrent_id, data)

            if isinstance(tdef, TorrentDefNoMetainfo):
                self.torrent_db.addOrGetTorrentID(tdef.get_infohash())
                self.torrent_db.updateTorrent(tdef.get_infohash(), name=tdef.get_name_as_unicode())
                write_my_pref()
            elif self.rtorrent_handler:
                self.rtorrent_handler.save_torrent(tdef, write_my_pref)
            else:
                self.torrent_db.addExternalTorrent(tdef, extra_info={'status': 'good'})
                write_my_pref()

        return d

    def network_engine_wrapper_created_callback(self, d, pstate):
        """ Called by network thread """
        try:
            if pstate is None:
                # Checkpoint at startup
                (infohash, pstate) = d.network_checkpoint()
                self.save_download_pstate(infohash, pstate)
        except:
            print_exc()

    def remove(self, d, removecontent=False, removestate=True, hidden=False):
        """ Called by any thread """
        with self.sesslock:
            d.stop_remove(removestate=removestate, removecontent=removecontent)
            infohash = d.get_def().get_infohash()
            if infohash in self.downloads:
                del self.downloads[infohash]

        if not hidden:
            self.remove_id(infohash)

    def remove_id(self, infohash):
開發者ID:Antiade,項目名稱:tribler,代碼行數:70,代碼來源:LaunchManyCore.py


注:本文中的Tribler.Core.CacheDB.SqliteCacheDBHandler.TorrentDBHandler.addOrGetTorrentID方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。