Ejemplo n.º 1
0
   def setupTorrent(self, torrentFile, savePath=None):

      # Some things to reset on every setup operation
      self.lastStats  = {}
      self.startTime  = None
      self.finishTime = None
      self.dlFailed   = False
      self.bt1dow     = None
      self.response    = None
      self.torrentSize = None
      self.torrentName = None
      self.savePath    = None
      self.savePath_temp = None

      # Set torrent file, bail if it doesn't exist
      self.torrent = torrentFile
      self.torrentDNE = False

      if not self.torrent or not os.path.exists(self.torrent):
         LOGERROR('Attempted to setup TDM with non-existent torrent:')
         if self.torrent:
            LOGERROR('Torrent path:  %s', self.torrent)
         self.torrentDNE = True
         return

      self.lastUpdate = RightNow()

      # Get some info about the torrent
      if not self.torrentDNE:
         self.response = get_response(self.torrent, '', self.errorFunc)
         self.torrentSize = self.response['info']['length']
         self.torrentName = self.response['info']['name']
         LOGINFO('Torrent name is: %s' %  self.torrentName)
         LOGINFO('Torrent size is: %0.2f MB' %  (self.torrentSize/float(MEGABYTE)))


         self.savePath = savePath
         if self.savePath is None:
            self.savePath = os.path.join(BTC_HOME_DIR, self.torrentName)
         self.savePath_temp = self.savePath + '.partial'
def run(scrwin, errlist, params):
    doneflag = Event()
    d = CursesDisplayer(scrwin, errlist, doneflag)
    try:
        while 1:
            configdir = ConfigDir('downloadcurses')
            defaultsToIgnore = ['responsefile', 'url', 'priority']
            configdir.setDefaults(defaults,defaultsToIgnore)
            configdefaults = configdir.loadConfig()
            defaults.append(('save_options',0,
             "whether to save the current options as the new default configuration " +
             "(only for btdownloadcurses.py)"))
            try:
                config = parse_params(params, configdefaults)
            except ValueError, e:
                d.error('error: ' + str(e) + '\nrun with no args for parameter explanations')
                break
            if not config:
                d.error(get_usage(defaults, d.fieldw, configdefaults))
                break
            if config['save_options']:
                configdir.saveConfig(config)
            configdir.deleteOldCacheData(config['expire_cache_data'])

            myid = createPeerID()
            seed(myid)

            rawserver = RawServer(doneflag, config['timeout_check_interval'],
                                  config['timeout'], ipv6_enable = config['ipv6_enabled'],
                                  failfunc = d.failed, errorfunc = d.error)

            upnp_type = UPnP_test(config['upnp_nat_access'])
            while True:
                try:
                    listen_port = rawserver.find_and_bind(config['minport'], config['maxport'],
                                    config['bind'], ipv6_socket_style = config['ipv6_binds_v4'],
                                    upnp = upnp_type, randomizer = config['random_port'])
                    break
                except socketerror, e:
                    if upnp_type and e == UPnP_ERROR:
                        d.error('WARNING: COULD NOT FORWARD VIA UPnP')
                        upnp_type = 0
                        continue
                    d.error("Couldn't listen - " + str(e))
                    d.failed()
                    return

            response = get_response(config['responsefile'], config['url'], d.error)
            if not response:
                break

            infohash = sha(bencode(response['info'])).digest()
            
            dow = BT1Download(d.display, d.finished, d.error, d.error, doneflag,
                            config, response, infohash, myid, rawserver, listen_port,
                            configdir)
            
            if not dow.saveAs(d.chooseFile):
                break

            if not dow.initFiles(old_style = True):
                break
            if not dow.startEngine():
                dow.shutdown()
                break
            dow.startRerequester()
            dow.autoStats()

            if not dow.am_I_finished():
                d.display(activity = 'connecting to peers')
            rawserver.listen_forever(dow.getPortHandler())
            d.display(activity = 'shutting down')
            dow.shutdown()
            break
Ejemplo n.º 3
0
            while True:
                try:
                    listen_port = rawserver.find_and_bind(config['minport'], config['maxport'],
                                config['bind'], ipv6_socket_style = config['ipv6_binds_v4'],
                                upnp = upnp_type, randomizer = config['random_port'])
                    break
                except socketerror, e:
                    if upnp_type and e == UPnP_ERROR:
                        print 'WARNING: COULD NOT FORWARD VIA UPnP'
                        upnp_type = 0
                        continue
                    print "error: Couldn't listen - " + str(e)
                    h.failed()
                    return

            response = get_response(config['responsefile'], config['url'], h.error)
            if not response:
                break

            infohash = sha(bencode(response['info'])).digest()

            h.dow = BT1Download(h.display, h.finished, h.error, disp_exception, doneflag,
                        config, response, infohash, myid, rawserver, listen_port)

            if not h.dow.saveAs(h.chooseFile, h.newpath):
                break

            if not h.dow.initFiles(old_style = True):
                break

            if not h.dow.startEngine():
Ejemplo n.º 4
0
def run(autoDie,shareKill,statusFile,userName,params):

    if __debug__: traceMsg('run - begin')

    try:
        f=open(statusFile+".pid",'w')
        f.write(str(getpid()).strip() + "\n")
        f.flush()
        f.close()
    except:
        if __debug__: traceMsg('run - Failed to Create PID file')
        pass

    try:

        h = HeadlessDisplayer()
        h.statFile = statusFile
        h.autoShutdown = autoDie
        h.shareKill = shareKill
        h.user = userName
    
        while 1:
            try:
                config = parse_params(params)
            except ValueError, e:
                print 'error: ' + str(e) + '\nrun with no args for parameter explanations'
                break
            if not config:
                print get_usage()
                break

            myid = createPeerID()
            seed(myid)
        
            doneflag = Event()
            def disp_exception(text):
                print text
            rawserver = RawServer(doneflag, config['timeout_check_interval'],
                              config['timeout'], ipv6_enable = config['ipv6_enabled'],
                              failfunc = h.failed, errorfunc = disp_exception)
            upnp_type = UPnP_test(config['upnp_nat_access'])
            while True:
                try:
                    listen_port = rawserver.find_and_bind(config['minport'], config['maxport'],
                                config['bind'], ipv6_socket_style = config['ipv6_binds_v4'],
                                upnp = upnp_type, randomizer = config['random_port'])
                    break
                except socketerror, e:
                    if upnp_type and e == UPnP_ERROR:
                        print 'WARNING: COULD NOT FORWARD VIA UPnP'
                        upnp_type = 0
                        continue
                    print "error: Couldn't listen - " + str(e)
                    h.failed()
                    return

            response = get_response(config['responsefile'], config['url'], h.error)
            if not response:
                break

            infohash = sha(bencode(response['info'])).digest()

            dow = BT1Download(h.display, h.finished, h.error, disp_exception, doneflag,
                        config, response, infohash, myid, rawserver, listen_port)
        
            if not dow.saveAs(h.chooseFile, h.newpath): 
                break

            if not dow.initFiles(old_style = True): 
                break

            if not dow.startEngine():
                dow.shutdown()
                break
            dow.startRerequester()
            dow.autoStats()

            if not dow.am_I_finished():
                h.display(activity = 'connecting to peers')
                
            rawserver.listen_forever(dow.getPortHandler())
            h.display(activity = 'shutting down')
            dow.shutdown()
            break
Ejemplo n.º 5
0
def run(params):
    h = HeadlessDisplayer()
    while 1:
        configdir = ConfigDir('downloadheadless')
        defaultsToIgnore = ['responsefile', 'url', 'priority']
        configdir.setDefaults(defaults, defaultsToIgnore)
        configdefaults = configdir.loadConfig()
        defaults.append(
            ('save_options', 0, 'whether to save the current options as the '
             'new default configuration (only for btdownloadheadless.py)'))
        try:
            config = parse_params(params, configdefaults)
        except ValueError as e:
            print 'error: {}\nrun with no args for parameter explanations' \
                ''.format(e)
            break
        if not config:
            print get_usage(defaults, 80, configdefaults)
            break
        if config['save_options']:
            configdir.saveConfig(config)
        configdir.deleteOldCacheData(config['expire_cache_data'])

        myid = createPeerID()
        random.seed(myid)

        doneflag = threading.Event()

        def disp_exception(text):
            print text
        rawserver = RawServer(
            doneflag, config['timeout_check_interval'], config['timeout'],
            ipv6_enable=config['ipv6_enabled'], failfunc=h.failed,
            errorfunc=disp_exception)
        upnp_type = UPnP_test(config['upnp_nat_access'])
        while True:
            try:
                listen_port = rawserver.find_and_bind(
                    config['minport'], config['maxport'], config['bind'],
                    ipv6_socket_style=config['ipv6_binds_v4'],
                    upnp=upnp_type, randomizer=config['random_port'])
                break
            except socket.error as e:
                if upnp_type and e == UPnP_ERROR:
                    print 'WARNING: COULD NOT FORWARD VIA UPnP'
                    upnp_type = 0
                    continue
                print "error: Couldn't listen - " + str(e)
                h.failed()
                return

        response = get_response(config['responsefile'], config['url'], h.error)
        if not response:
            break

        infohash = hashlib.sha1(bencode(response['info'])).digest()

        dow = BT1Download(
            h.display, h.finished, h.error, disp_exception, doneflag, config,
            response, infohash, myid, rawserver, listen_port, configdir)

        if not dow.saveAs(h.chooseFile, h.newpath):
            break

        if not dow.initFiles(old_style=True):
            break
        if not dow.startEngine():
            dow.shutdown()
            break
        dow.startRerequester()
        dow.autoStats()

        if not dow.am_I_finished():
            h.display(activity='connecting to peers')
        rawserver.listen_forever(dow.getPortHandler())
        h.display(activity='shutting down')
        dow.shutdown()
        break
    try:
        rawserver.shutdown()
    except:
        pass
    if not h.done:
        h.failed()
Ejemplo n.º 6
0
                    config["bind"],
                    ipv6_socket_style=config["ipv6_binds_v4"],
                    upnp=upnp_type,
                    randomizer=config["random_port"],
                )
                break
            except socketerror, e:
                if upnp_type and e == UPnP_ERROR:
                    print "WARNING: COULD NOT FORWARD VIA UPnP"
                    upnp_type = 0
                    continue
                print "error: Couldn't listen - " + str(e)
                h.failed()
                return

        response = get_response(config["responsefile"], config["url"], h.error)
        if not response:
            break

        infohash = sha(bencode(response["info"])).digest()

        dow = BT1Download(
            h.display,
            h.finished,
            h.error,
            disp_exception,
            doneFlag,
            config,
            response,
            infohash,
            myid,
Ejemplo n.º 7
0
    def start(self):
        rawserver = None

        try:
            config = dict([(v[0], v[1]) for v in defaults])
            config['ip'] = self.ip
            config['responsefile'] = self.torrent_file
            config['saveas'] = self.destination_file

            random.seed(self.id)

            rawserver = RawServer(
                doneflag=self.done_flag,
                timeout_check_interval=config['timeout_check_interval'],
                timeout=config['timeout'],
                ipv6_enable=config['ipv6_enabled'],
                failfunc=self.on_fail,
                errorfunc=self.on_exception)
            rawserver.bind(
                port=self.port,
                bind=config['bind'],
                reuse=True,
                ipv6_socket_style=config['ipv6_binds_v4'])

            # Download torrent metadata.
            response = get_response(
                file=config['responsefile'],
                url=config['url'],
                errorfunc=self.on_error)

            # Bail if tracker is done.
            if not response:
                return

            dow = BT1Download(
                statusfunc=self.on_status,
                finfunc=self.on_finish,
                errorfunc=self.on_error,
                excfunc=self.on_exception,
                doneflag=self.done_flag,
                config=config,
                response=response,
                infohash=hashlib.sha1(bencode(response['info'])).digest(),
                id=self.id,
                rawserver=rawserver,
                port=self.port)

            if not dow.saveAs(lambda default, size, saveas, dir: saveas if saveas else default):
                return

            if not dow.initFiles(old_style=True):
                return

            if not dow.startEngine():
                dow.shutdown()
                return

            dow.startRerequester()
            dow.autoStats()

            if not dow.am_I_finished():
                self.on_status(activity='connecting to peers')

            rawserver.listen_forever(dow.getPortHandler())

            self.on_status(activity='shutting down')

            dow.shutdown()
        finally:
            if rawserver:
                rawserver.shutdown()

            if not self.finished_at:
                self.on_fail()
Ejemplo n.º 8
0
        while True:
            try:
                listen_port = rawserver.find_and_bind(config['minport'], config['maxport'],
                                config['bind'], ipv6_socket_style = config['ipv6_binds_v4'],
                                upnp = upnp_type, randomizer = config['random_port'])
                break
            except socketerror, e:
                if upnp_type and e == UPnP_ERROR:
                    print 'WARNING: COULD NOT FORWARD VIA UPnP'
                    upnp_type = 0
                    continue
                print "error: Couldn't listen - " + str(e)
                h.failed()
                return

        response = get_response(config['responsefile'], config['url'], h.error)
        if not response:
            break

        infohash = sha(bencode(response['info'])).digest()

        dow = BT1Download(h.display, h.finished, h.error, disp_exception, doneFlag,
                        config, response, infohash, myid, rawserver, listen_port,
                        configdir)

        if not dow.saveAs(h.chooseFile, h.newpath):
            break

        if not dow.initFiles(old_style = True):
            break
        if not dow.startEngine():