Esempio n. 1
0
    def notify_possible_torrent_roothash(self, roothash):
        keys = self.callbacks.keys()
        for key in keys:
            if key[1] == roothash:
                handle_lambda = lambda key=key: self._handleCallback(key, True)
                self.scheduletask(handle_lambda)

        def do_db(tdef):
            if self.torrent_db.hasTorrent(tdef.get_infohash()):
                self.torrent_db.updateTorrent(
                    tdef.get_infohash(),
                    swift_torrent_hash=sdef.get_roothash(),
                    torrent_file_name=swiftpath)
            else:
                self.torrent_db._addTorrentToDB(tdef,
                                                source="SWIFT",
                                                extra_info={
                                                    'filename': swiftpath,
                                                    'swift_torrent_hash':
                                                    roothash,
                                                    'status': 'good'
                                                },
                                                commit=True)

        sdef = SwiftDef(roothash)
        swiftpath = os.path.join(self.session.get_torrent_collecting_dir(),
                                 sdef.get_roothash_as_hex())
        if os.path.exists(swiftpath):
            try:
                tdef = TorrentDef.load(swiftpath)
                startWorker(None, do_db, wargs=(tdef, ))

            except:
                #ignore if tdef loading fails
                pass
Esempio n. 2
0
    def _has_torrent(self, infohash, tor_col_dir, callback):
        #save torrent
        result = False
        torrent = self.torrent_db.getTorrent(
            infohash, ['torrent_file_name', 'swift_torrent_hash'],
            include_mypref=False)
        if torrent:
            if torrent.get('torrent_file_name', False) and os.path.isfile(
                    torrent['torrent_file_name']):
                result = torrent['torrent_file_name']

            elif torrent.get('swift_torrent_hash', False):
                sdef = SwiftDef(torrent['swift_torrent_hash'])
                torrent_filename = os.path.join(tor_col_dir,
                                                sdef.get_roothash_as_hex())

                if os.path.isfile(torrent_filename):
                    self.torrent_db.updateTorrent(
                        infohash,
                        notify=False,
                        torrent_file_name=torrent_filename)
                    result = torrent_filename

        raw_lambda = lambda result=result: callback(result)
        self.scheduletask(raw_lambda)
Esempio n. 3
0
    def __torrentdef_retrieved(self, tdef):
        infohash = tdef.get_infohash()
        if DEBUG:
            print >>sys.stderr, "magnetrequester: received torrent", bin2str(infohash)

        # remove from requested list
        if infohash in self.requestedInfohashes:
            self.requestedInfohashes.remove(infohash)

            # save torrent
            torrent = self.torrent_db.getTorrent(infohash, ["torrent_file_name"], include_mypref=False)
            if torrent and torrent.get("torrent_file_name", False) and not os.path.isabs(torrent["torrent_file_name"]):
                torrent_filename = os.path.join(self.metadatahandler.torrent_dir, torrent["torrent_file_name"])
            else:
                torrent_filename = os.path.join(
                    self.metadatahandler.torrent_dir, get_collected_torrent_filename(infohash)
                )
            tdef.save(torrent_filename)

            # calculate root-hash
            sdef = SwiftDef()
            sdef.add_content(torrent_filename)
            sdef.finalize(self.session.get_swift_path())

            # add this new torrent to db
            self.torrent_db.addExternalTorrent(tdef, extra_info={"swift_torrent_hash": bin2str(sdef.get_roothash())})

            # notify all
            self.remoteTorrentHandler.metadatahandler_got_torrent(infohash, tdef, torrent_filename)
            self.overlay_bridge.add_task(self.__requestMagnet, self.REQUEST_INTERVAL)
Esempio n. 4
0
    def _create_and_reseed(self, session):
        # 1. Create a 500K randomdata file
        storagepath = os.path.join(self.getDestDir(), "output_file")
        with open(storagepath, 'wb') as fout:
            fout.write(os.urandom(512000))

        # 2. Create the SwiftDef
        sdef = SwiftDef()
        sdef.set_tracker("127.0.0.1:%d" % session.get_swift_dht_listen_port())
        sdef.add_content(storagepath)
        sdef.finalize(session.get_swift_path(), destdir=self.getDestDir())

        # 3. Save swift files to metadata dir
        metadir = session.get_swift_meta_dir()
        metapath = os.path.join(metadir, "output_file")
        try:
            move(storagepath + '.mhash', metapath + '.mhash')
            move(storagepath + '.mbinmap', metapath + '.mbinmap')
        except:
            print_exc()

        # 4. Start seeding this file
        defaultDLConfig = DefaultDownloadStartupConfig.getInstance()
        dscfg = defaultDLConfig.copy()
        dscfg.set_dest_dir(storagepath)
        d = session.start_download(sdef, dscfg)
        d.set_state_callback(self.seeder_state_callback)

        return sdef.get_id()
Esempio n. 5
0
    def _create_and_reseed(self, session):
        # 1. Create a 500K randomdata file
        storagepath = os.path.join(self.getDestDir(), "output_file")
        with open(storagepath, 'wb') as fout:
            fout.write(os.urandom(512000))

        # 2. Create the SwiftDef
        sdef = SwiftDef()
        sdef.set_tracker("127.0.0.1:%d" % session.get_swift_dht_listen_port())
        sdef.add_content(storagepath)
        sdef.finalize(session.get_swift_path(), destdir=self.getDestDir())

        # 3. Save swift files to metadata dir
        metadir = session.get_swift_meta_dir()
        metapath = os.path.join(metadir, "output_file")
        try:
            move(storagepath + '.mhash', metapath + '.mhash')
            move(storagepath + '.mbinmap', metapath + '.mbinmap')
        except:
            print_exc()

        # 4. Start seeding this file
        defaultDLConfig = DefaultDownloadStartupConfig.getInstance()
        dscfg = defaultDLConfig.copy()
        dscfg.set_dest_dir(storagepath)
        d = session.start_download(sdef, dscfg)
        d.set_state_callback(self.seeder_state_callback)

        return sdef.get_id()
Esempio n. 6
0
    def _write_to_collected(self, filename):
        #calculate root-hash
        sdef = SwiftDef()
        sdef.add_content(filename)
        sdef.finalize(self.session.get_swift_path(),
                      destdir=self.session.get_torrent_collecting_dir())

        mfpath = os.path.join(self.session.get_torrent_collecting_dir(),
                              sdef.get_roothash_as_hex())
        if not os.path.exists(mfpath):
            download = self.session.get_download(sdef.get_roothash())
            if download:
                self.session.remove_download(download, removestate=True)
                sleep(1)
            elif os.path.exists(mfpath +
                                ".mhash"):  #indicating failed swift download
                os.remove(mfpath + ".mhash")

            try:
                shutil.copy(filename, mfpath)
                shutil.move(filename + '.mhash', mfpath + '.mhash')
                shutil.move(filename + '.mbinmap', mfpath + '.mbinmap')

            except:
                print_exc()

        return sdef, mfpath
 def notify_possible_torrent_roothash(self, roothash):
     keys = self.callbacks.keys()
     for key in keys:
         if key[1] == roothash:
             handle_lambda = lambda key=key: self._handleCallback(key, True)
             self.scheduletask(handle_lambda)
     
     sdef = SwiftDef(roothash)
     swiftpath = os.path.join(self.session.get_torrent_collecting_dir(),sdef.get_roothash_as_hex())
     if os.path.exists(swiftpath):
         tdef = TorrentDef.load(swiftpath)
     
         if self.torrent_db.hasTorrent(tdef.get_infohash()):
             self.torrent_db.updateTorrent(tdef.get_infohash(), swift_torrent_hash = sdef.get_roothash(), torrent_file_name = swiftpath)
         else:
             self.torrent_db._addTorrentToDB(tdef, source = "SWIFT", extra_info = {'filename': swiftpath, 'swift_torrent_hash':roothash, 'status':'good'}, commit = True)
Esempio n. 8
0
    def _doFetch(self, filename, hash, candidates):
        infohash, roothash = hash

        if filename:
            self.remote_th.notify_possible_torrent_infohash(infohash, True)
            self.remote_th.notify_possible_torrent_infohash(hash, True)

        elif candidates:
            candidate = candidates[0]
            candidates = candidates[1:]

            ip, port = candidate.sock_addr
            if not candidate.tunnel:
                port = 7758

            if DEBUG:
                print >> sys.stderr, "rtorrent: requesting torrent", hash, ip, port

            doMagnet = self.prio <= 1
            download = None

            sdef = SwiftDef(roothash, tracker="%s:%d" % (ip, port))
            dcfg = self.dscfg.copy()
            try:
                #hide download from gui
                download = self.session.start_download(sdef, dcfg, hidden=True)

                state_lambda = lambda ds, infohash=infohash, roothash=roothash, doMagnet=doMagnet: self.check_progress(
                    ds, infohash, roothash, doMagnet)
                download.set_state_callback(state_lambda,
                                            getpeerlist=False,
                                            delay=self.SWIFT_CANCEL)
                download.started_downloading = time()

            except DuplicateDownloadException:
                download = self.session.get_download(roothash)
                download.add_peer((ip, port))

            except OperationNotEnabledByConfigurationException:
                doMagnet = True

            if download and candidates:
                try:
                    for candidate in candidates:
                        ip, port = candidate.sock_addr
                        if not candidate.tunnel:
                            port = 7758

                        download.add_peer((ip, port))
                except:
                    print_exc()

            #schedule a magnet lookup after X seconds
            if doMagnet:
                magnet_lambda = lambda infohash=infohash: self.magnet_requester.add_request(
                    infohash, None)
                self.scheduletask(magnet_lambda,
                                  t=self.MAGNET_TIMEOUT * (self.prio))
            return True
 def _write_to_collected(self, filename):
     #calculate root-hash
     sdef = SwiftDef()
     sdef.add_content(filename)
     sdef.finalize(self.session.get_swift_path(), destdir = self.session.get_torrent_collecting_dir())
     
     mfpath = os.path.join(self.session.get_torrent_collecting_dir(),sdef.get_roothash_as_hex())
     if not os.path.exists(mfpath):
         if os.path.exists(mfpath + ".mhash"): #indicating active swift download
             download = self.session.get_download(sdef.get_roothash())
             if download:
                 self.session.remove_download(download, removestate = True)
                 sleep(1)
             else:
                 os.remove(mfpath + ".mhash")
         
         try:
             shutil.copy(filename, mfpath)
             shutil.move(filename+'.mhash', mfpath+'.mhash')
             shutil.move(filename+'.mbinmap', mfpath+'.mbinmap')
             
         except:
             print_exc()
     
     return sdef, mfpath
    def _has_torrent(self, infohash, tor_col_dir, callback):
        #save torrent
        result = False
        torrent = self.torrent_db.getTorrent(infohash, ['torrent_file_name', 'swift_torrent_hash'], include_mypref = False)
        if torrent:
            if torrent.get('torrent_file_name', False) and os.path.isfile(torrent['torrent_file_name']):
                result = torrent['torrent_file_name']

            elif torrent.get('swift_torrent_hash', False):
                sdef = SwiftDef(torrent['swift_torrent_hash'])
                torrent_filename = os.path.join(tor_col_dir, sdef.get_roothash_as_hex())

                if os.path.isfile(torrent_filename):
                    self.torrent_db.updateTorrent(infohash, notify=False, torrent_file_name=torrent_filename)
                    result = torrent_filename

        raw_lambda = lambda result=result: callback(result)
        self.scheduletask(raw_lambda)
Esempio n. 11
0
    def doFetch(self, hash_tuple, candidates):
        roothash, infohash = hash_tuple
        attempting_download = False

        if self.remote_th.has_thumbnail(infohash):
            self.remote_th.notify_possible_thumbnail_roothash(roothash)

        elif candidates:
            candidate = candidates[0]
            candidates = candidates[1:]

            ip, port = candidate.sock_addr
            if not candidate.tunnel:
                port = 7758

            if True or DEBUG:
                print >> sys.stderr, "rtorrent: requesting thumbnail", binascii.hexlify(roothash), ip, port

            download = None

            sdef = SwiftDef(roothash, tracker="%s:%d" % (ip, port))
            dcfg = self.dscfg.copy()
            try:
                # hide download from gui
                download = self.session.start_download(sdef, dcfg, hidden=True)

                state_lambda = lambda ds, roothash = roothash: self.check_progress(ds, roothash)
                download.set_state_callback(state_lambda, delay=self.REQUEST_INTERVAL * (self.prio + 1))
                download.started_downloading = time()

            except DuplicateDownloadException:
                download = self.session.get_download(roothash)
                download.add_peer((ip, port))

            except OperationNotEnabledByConfigurationException:
                pass

            else:
                attempting_download = True

            if download and candidates:
                try:
                    for candidate in candidates:
                        ip, port = candidate.sock_addr
                        if not candidate.tunnel:
                            port = 7758

                        download.add_peer((ip, port))
                except:
                    print_exc()

        return attempting_download
Esempio n. 12
0
    def __torrentdef_retrieved(self, tdef):
        infohash = tdef.get_infohash()
        if DEBUG:
            print >> sys.stderr, 'magnetrequester: received torrent', bin2str(
                infohash)

        #remove from requested list
        if infohash in self.requestedInfohashes:
            self.requestedInfohashes.remove(infohash)

            #save torrent
            torrent = self.torrent_db.getTorrent(infohash,
                                                 ['torrent_file_name'],
                                                 include_mypref=False)
            if torrent and torrent.get(
                    'torrent_file_name',
                    False) and not os.path.isabs(torrent['torrent_file_name']):
                torrent_filename = os.path.join(
                    self.metadatahandler.torrent_dir,
                    torrent['torrent_file_name'])
            else:
                torrent_filename = os.path.join(
                    self.metadatahandler.torrent_dir,
                    get_collected_torrent_filename(infohash))
            tdef.save(torrent_filename)

            #calculate root-hash
            sdef = SwiftDef()
            sdef.add_content(torrent_filename)
            sdef.finalize(self.session.get_swift_path())

            #add this new torrent to db
            self.torrent_db.addExternalTorrent(tdef,
                                               extra_info={
                                                   'swift_torrent_hash':
                                                   bin2str(sdef.get_roothash())
                                               })

            #notify all
            self.remoteTorrentHandler.metadatahandler_got_torrent(
                infohash, tdef, torrent_filename)
            self.overlay_bridge.add_task(self.__requestMagnet,
                                         self.REQUEST_INTERVAL)
Esempio n. 13
0
    def _create_and_seed_metadata(self, videofile, torrent):
        from Tribler.Main.vwxGUI.GuiUtility import GUIUtility

        if prctlimported:
            prctl.set_name("Tribler" + currentThread().getName())

        self.guiutility = GUIUtility.getInstance()
        self.session = self.guiutility.utility.session
        videoanalyser = self.session.get_video_analyser_path()

        torcoldir = self.session.get_torrent_collecting_dir()
        rel_thumbdir = 'thumbs-' + binascii.hexlify(torrent.infohash)
        abs_thumbdir = os.path.join(torcoldir, rel_thumbdir)
        videoname = os.path.basename(videofile)

        if os.path.exists(abs_thumbdir):
            if DEBUG:
                print >> sys.stderr, 'create_and_seed_metadata: already downloaded thumbnails for torrent', torrent.name
            return

        if DEBUG:
            print >> sys.stderr, 'create_and_seed_metadata: going to seed metadata for torrent', torrent.name

        duration, bitrate, resolution = get_videoinfo(videofile, videoanalyser)
        video_info = {'duration': duration, \
                      'bitrate': bitrate, \
                      'resolution': resolution}

        if DEBUG:
            print >> sys.stderr, 'create_and_seed_metadata: FFMPEG - duration = %d, bitrate = %d, resolution = %s' % (
                duration, bitrate, resolution)

        if not os.path.exists(abs_thumbdir):
            os.makedirs(abs_thumbdir)

        thumb_filenames = [
            os.path.join(abs_thumbdir, videoname + postfix)
            for postfix in ["-thumb%d.jpg" % i for i in range(1, 5)]
        ]
        thumb_resolutions = [(1280, 720), (320, 240), (320, 240), (320, 240)]
        thumb_timecodes = preferred_timecodes(videofile,
                                              duration,
                                              limit_resolution(
                                                  resolution, (100, 100)),
                                              videoanalyser,
                                              k=4)

        for filename, max_res, timecode in zip(thumb_filenames,
                                               thumb_resolutions,
                                               thumb_timecodes):
            thumb_res = limit_resolution(resolution, max_res)
            get_thumbnail(videofile, filename, thumb_res, videoanalyser,
                          timecode)
            if DEBUG:
                path_exists = os.path.exists(filename)
                print >> sys.stderr, 'create_and_seed_metadata: FFMPEG - thumbnail created = %s, timecode = %d' % (
                    path_exists, timecode)

        sdef = SwiftDef()
        sdef.set_tracker("127.0.0.1:9999")
        for thumbfile in thumb_filenames:
            if os.path.exists(thumbfile):
                xi = os.path.relpath(thumbfile, torcoldir)
                if sys.platform == "win32":
                    xi = xi.replace("\\", "/")
                si = xi.encode("UTF-8")
                sdef.add_content(thumbfile, si)

        specpn = sdef.finalize(self.session.get_swift_path(),
                               destdir=torcoldir)

        hex_roothash = sdef.get_roothash_as_hex()

        try:
            swift_filename = os.path.join(torcoldir, hex_roothash)
            shutil.move(specpn, swift_filename)
            shutil.move(specpn + '.mhash', swift_filename + '.mhash')
            shutil.move(specpn + '.mbinmap', swift_filename + '.mbinmap')

        except:
            if DEBUG:
                print_exc()

        modifications = {'swift-thumbnails': json.dumps((thumb_timecodes, sdef.get_roothash_as_hex())), \
                         'video-info': json.dumps(video_info)}

        if DEBUG:
            print >> sys.stderr, 'create_and_seed_metadata: modifications =', modifications

        self.channelsearch_manager.modifyTorrent(torrent.channel.id,
                                                 torrent.channeltorrent_id,
                                                 modifications)
Esempio n. 14
0
    def _create_and_seed_metadata(self, videofile, torrent):
        from Tribler.Main.vwxGUI.GuiUtility import GUIUtility

        if prctlimported:
            prctl.set_name("Tribler"+currentThread().getName())

        self.guiutility = GUIUtility.getInstance()                    
        self.session    = self.guiutility.utility.session
        videoanalyser   = self.session.get_video_analyser_path()

        torcoldir    = self.session.get_torrent_collecting_dir()
        rel_thumbdir = 'thumbs-'+binascii.hexlify(torrent.infohash)
        abs_thumbdir = os.path.join(torcoldir, rel_thumbdir)
        videoname    = os.path.basename(videofile)
        
        if os.path.exists(abs_thumbdir):
            if DEBUG:
                print >> sys.stderr, 'create_and_seed_metadata: already downloaded thumbnails for torrent', torrent.name
            return

        if DEBUG:
            print >> sys.stderr, 'create_and_seed_metadata: going to seed metadata for torrent', torrent.name        

        duration, bitrate, resolution = get_videoinfo(videofile, videoanalyser)
        video_info = {'duration': duration, \
                      'bitrate': bitrate, \
                      'resolution': resolution}

        if DEBUG:
            print >> sys.stderr, 'create_and_seed_metadata: FFMPEG - duration = %d, bitrate = %d, resolution = %s' % (duration, bitrate, resolution)        

        if not os.path.exists(abs_thumbdir):
            os.makedirs(abs_thumbdir)
        
        thumb_filenames = [os.path.join(abs_thumbdir, videoname + postfix) for postfix in ["-thumb%d.jpg" % i for i in range(1,5)]]
        thumb_resolutions = [(1280, 720), (320, 240), (320, 240), (320, 240)]
        thumb_timecodes = preferred_timecodes(videofile, duration, limit_resolution(resolution, (100, 100)), videoanalyser, k = 4)
        
        for filename, max_res, timecode in zip(thumb_filenames, thumb_resolutions, thumb_timecodes):
            thumb_res = limit_resolution(resolution, max_res)
            get_thumbnail(videofile, filename, thumb_res, videoanalyser, timecode)
            if DEBUG:
                path_exists = os.path.exists(filename)
                print >> sys.stderr, 'create_and_seed_metadata: FFMPEG - thumbnail created = %s, timecode = %d' % (path_exists, timecode)

        sdef = SwiftDef()
        sdef.set_tracker("127.0.0.1:9999") 
        for thumbfile in thumb_filenames:
            if os.path.exists(thumbfile):
                xi = os.path.relpath(thumbfile, torcoldir)
                if sys.platform == "win32":
                    xi = xi.replace("\\","/")
                si = xi.encode("UTF-8")
                sdef.add_content(thumbfile, si)
                
        specpn = sdef.finalize(self.session.get_swift_path(), destdir = torcoldir)
                    
        hex_roothash = sdef.get_roothash_as_hex()
        
        try:
            swift_filename = os.path.join(torcoldir, hex_roothash)
            shutil.move(specpn, swift_filename)
            shutil.move(specpn+'.mhash', swift_filename+'.mhash')
            shutil.move(specpn+'.mbinmap', swift_filename+'.mbinmap')
            
        except:
            if DEBUG:
                print_exc()
            
        modifications = {'swift-thumbnails': json.dumps((thumb_timecodes, sdef.get_roothash_as_hex())), \
                         'video-info': json.dumps(video_info)}
        
        if DEBUG:
            print >> sys.stderr, 'create_and_seed_metadata: modifications =', modifications
        
        self.channelsearch_manager.modifyTorrent(torrent.channel.id, torrent.channeltorrent_id, modifications)
Esempio n. 15
0
    def resume_download(self, filename, initialdlstatus=None, initialdlstatus_dict={}, commit=True, setupDelay=0):
        tdef = sdef = dscfg = pstate = None

        try:
            pstate = self.load_download_pstate(filename)

            # SWIFTPROC
            if SwiftDef.is_swift_url(pstate['metainfo']):
                sdef = SwiftDef.load_from_url(pstate['metainfo'])
            elif 'infohash' in pstate['metainfo']:
                tdef = TorrentDefNoMetainfo(pstate['metainfo']['infohash'], pstate['metainfo']['name'])
            else:
                tdef = TorrentDef.load_from_dict(pstate['metainfo'])

            dlconfig = pstate['dlconfig']
            if isinstance(dlconfig['saveas'], tuple):
                dlconfig['saveas'] = dlconfig['saveas'][-1]

            if sdef and 'name' in dlconfig and isinstance(dlconfig['name'], basestring):
                sdef.set_name(dlconfig['name'])
            if sdef and sdef.get_tracker().startswith("127.0.0.1:"):
                current_port = int(sdef.get_tracker().split(":")[1])
                if current_port != self.session.get_swift_dht_listen_port():
                    print >> sys.stderr, "Modified SwiftDef to new tracker port"
                    sdef.set_tracker("127.0.0.1:%d" % self.session.get_swift_dht_listen_port())

            dscfg = DownloadStartupConfig(dlconfig)

        except:
            print_exc()
            # pstate is invalid or non-existing
            _, file = os.path.split(filename)

            infohash = binascii.unhexlify(file[:-7])
            torrent = self.torrent_db.getTorrent(infohash, keys=['name', 'torrent_file_name', 'swift_torrent_hash'], include_mypref=False)
            torrentfile = None
            if torrent:
                torrent_dir = self.session.get_torrent_collecting_dir()

                if torrent['swift_torrent_hash']:
                    sdef = SwiftDef(torrent['swift_torrent_hash'])
                    save_name = sdef.get_roothash_as_hex()
                    torrentfile = os.path.join(torrent_dir, save_name)

                if torrentfile and os.path.isfile(torrentfile):
                    # normal torrentfile is not present, see if readable torrent is there
                    save_name = get_readable_torrent_name(infohash, torrent['name'])
                    torrentfile = os.path.join(torrent_dir, save_name)

            if torrentfile and os.path.isfile(torrentfile):
                tdef = TorrentDef.load(torrentfile)

                defaultDLConfig = DefaultDownloadStartupConfig.getInstance()
                dscfg = defaultDLConfig.copy()

                if self.mypref_db != None:
                    preferences = self.mypref_db.getMyPrefStatsInfohash(infohash)
                    if preferences:
                        if os.path.isdir(preferences[2]) or preferences[2] == '':
                            dscfg.set_dest_dir(preferences[2])

        if DEBUG:
            print >> sys.stderr, "tlm: load_checkpoint: pstate is", dlstatus_strings[pstate['dlstate']['status']], pstate['dlstate']['progress']
            if pstate['engineresumedata'] is None:
                print >> sys.stderr, "tlm: load_checkpoint: resumedata None"
            else:
                print >> sys.stderr, "tlm: load_checkpoint: resumedata len", len(pstate['engineresumedata'])

        if (tdef or sdef) and dscfg:
            if dscfg.get_dest_dir() != '':  # removed torrent ignoring
                try:
                    if not self.download_exists((tdef or sdef).get_id()):
                        if tdef:
                            initialdlstatus = initialdlstatus_dict.get(tdef.get_id(), initialdlstatus)
                            self.add(tdef, dscfg, pstate, initialdlstatus, commit=commit, setupDelay=setupDelay)
                        else:
                            initialdlstatus = initialdlstatus_dict.get(sdef.get_id(), initialdlstatus)
                            self.swift_add(sdef, dscfg, pstate, initialdlstatus)
                    else:
                        print >> sys.stderr, "tlm: not resuming checkpoint because download has already been added"

                except Exception as e:
                    self.rawserver_nonfatalerrorfunc(e)
            else:
                print >> sys.stderr, "tlm: removing checkpoint", filename, "destdir is", dscfg.get_dest_dir()
                os.remove(filename)
        else:
            print >> sys.stderr, "tlm: could not resume checkpoint", filename, tdef, dscfg
Esempio n. 16
0
    def resume_download(self,
                        filename,
                        initialdlstatus=None,
                        initialdlstatus_dict={},
                        commit=True,
                        setupDelay=0):
        tdef = sdef = dscfg = pstate = None

        try:
            pstate = self.load_download_pstate(filename)

            # SWIFTPROC
            if SwiftDef.is_swift_url(pstate['metainfo']):
                sdef = SwiftDef.load_from_url(pstate['metainfo'])
            elif 'infohash' in pstate['metainfo']:
                tdef = TorrentDefNoMetainfo(pstate['metainfo']['infohash'],
                                            pstate['metainfo']['name'])
            else:
                tdef = TorrentDef.load_from_dict(pstate['metainfo'])

            dlconfig = pstate['dlconfig']
            if isinstance(dlconfig['saveas'], tuple):
                dlconfig['saveas'] = dlconfig['saveas'][-1]

            if sdef and 'name' in dlconfig and isinstance(
                    dlconfig['name'], basestring):
                sdef.set_name(dlconfig['name'])
            if sdef and sdef.get_tracker().startswith("127.0.0.1:"):
                current_port = int(sdef.get_tracker().split(":")[1])
                if current_port != self.session.get_swift_dht_listen_port():
                    print >> sys.stderr, "Modified SwiftDef to new tracker port"
                    sdef.set_tracker("127.0.0.1:%d" %
                                     self.session.get_swift_dht_listen_port())

            dscfg = DownloadStartupConfig(dlconfig)

        except:
            print_exc()
            # pstate is invalid or non-existing
            _, file = os.path.split(filename)

            infohash = binascii.unhexlify(file[:-7])
            torrent = self.torrent_db.getTorrent(
                infohash,
                keys=['name', 'torrent_file_name', 'swift_torrent_hash'],
                include_mypref=False)
            torrentfile = None
            if torrent:
                torrent_dir = self.session.get_torrent_collecting_dir()

                if torrent['swift_torrent_hash']:
                    sdef = SwiftDef(torrent['swift_torrent_hash'])
                    save_name = sdef.get_roothash_as_hex()
                    torrentfile = os.path.join(torrent_dir, save_name)

                if torrentfile and os.path.isfile(torrentfile):
                    # normal torrentfile is not present, see if readable torrent is there
                    save_name = get_readable_torrent_name(
                        infohash, torrent['name'])
                    torrentfile = os.path.join(torrent_dir, save_name)

            if torrentfile and os.path.isfile(torrentfile):
                tdef = TorrentDef.load(torrentfile)

                defaultDLConfig = DefaultDownloadStartupConfig.getInstance()
                dscfg = defaultDLConfig.copy()

                if self.mypref_db != None:
                    preferences = self.mypref_db.getMyPrefStatsInfohash(
                        infohash)
                    if preferences:
                        if os.path.isdir(
                                preferences[2]) or preferences[2] == '':
                            dscfg.set_dest_dir(preferences[2])

        if DEBUG:
            print >> sys.stderr, "tlm: load_checkpoint: pstate is", dlstatus_strings[
                pstate['dlstate']['status']], pstate['dlstate']['progress']
            if pstate['engineresumedata'] is None:
                print >> sys.stderr, "tlm: load_checkpoint: resumedata None"
            else:
                print >> sys.stderr, "tlm: load_checkpoint: resumedata len", len(
                    pstate['engineresumedata'])

        if (tdef or sdef) and dscfg:
            if dscfg.get_dest_dir() != '':  # removed torrent ignoring
                try:
                    if not self.download_exists((tdef or sdef).get_id()):
                        if tdef:
                            initialdlstatus = initialdlstatus_dict.get(
                                tdef.get_id(), initialdlstatus)
                            self.add(tdef,
                                     dscfg,
                                     pstate,
                                     initialdlstatus,
                                     commit=commit,
                                     setupDelay=setupDelay)
                        else:
                            initialdlstatus = initialdlstatus_dict.get(
                                sdef.get_id(), initialdlstatus)
                            self.swift_add(sdef, dscfg, pstate,
                                           initialdlstatus)
                    else:
                        print >> sys.stderr, "tlm: not resuming checkpoint because download has already been added"

                except Exception as e:
                    self.rawserver_nonfatalerrorfunc(e)
            else:
                print >> sys.stderr, "tlm: removing checkpoint", filename, "destdir is", dscfg.get_dest_dir(
                )
                os.remove(filename)
        else:
            print >> sys.stderr, "tlm: could not resume checkpoint", filename, tdef, dscfg