def get_url(url): """Downloads the .torrent metainfo file specified by the passed URL and returns data, the raw contents of the metainfo file. Any exception raised while trying to obtain the metainfo file is caught and GetTorrent.URLException is raised instead. """ data = None err_str = ((_('Could not download or open "%s"')% url) + '\n' + _("Try using a web browser to download the torrent file.")) u = None # pending protocol changes, convert: # torrent://http://path.to/file # and: # bittorrent://http://path.to/file # to: # http://path.to/file url = urlpat_torrent.sub('', url) url = urlpat_bittorrent.sub('', url) try: u = zurllib.urlopen(url) data = u.read() u.close() except Exception, e: if u is not None: u.close() raise URLException(err_str + "\n(%s)" % e)
def _rerequest(self, url, peerid): if self.config['ip']: try: url += '&ip=' + socket.gethostbyname(self.config['ip']) except: self.errorfunc(logging.WARNING, _("Problem resolving config ip (%s), gethostbyname failed") % self.config['ip'], exc_info=sys.exc_info()) request = Request(url) request.add_header('User-Agent', 'BitTorrent/' + version) if self.config['tracker_proxy']: request.set_proxy(self.config['tracker_proxy'], 'http') try: h = urlopen(request) data = h.read() h.close() # urllib2 can raise various crap that doesn't have a common base # exception class especially when proxies are used, at least # ValueError and stuff from httplib except Exception, e: try: s = unicode(e.args[0]) except: s = unicode(e) r = _("Problem connecting to tracker - %s: %s") % (e.__class__, s) def f(): self._postrequest(errormsg=r, exc=e, peerid=peerid)
def get_language(name): from BTL import LOCALE_URL url = LOCALE_URL + name + ".tar.gz" socket.setdefaulttimeout(5) r = urllib.urlopen(url) # urllib seems to ungzip for us tarname = os.path.join(locale_root, name + ".tar") f = file(tarname, 'wb') f.write(r.read()) f.close() tar = tarfile.open(tarname, "r") for tarinfo in tar: tar.extract(tarinfo, path=locale_root) tar.close()
def _rerequest(self, url, peerid): if self.config['ip']: url += '&ip=' + gethostbyname(self.config['ip']) request = Request(url) if self.config['tracker_proxy']: request.set_proxy(self.config['tracker_proxy'], 'http') try: h = urlopen(request) data = h.read() h.close() # urllib2 can raise various crap that doesn't have a common base # exception class especially when proxies are used, at least # ValueError and stuff from httplib except Exception, e: def f(r='Problem connecting to tracker - ' + str(e)): self._postrequest(errormsg=r, peerid=peerid)
def _rerequest(self, url, peerid): if self.config['ip']: url += '&ip=' + gethostbyname(self.config['ip']) request = Request(url) request.add_header('User-Agent', 'BitTorrent/' + version) if self.config['tracker_proxy']: request.set_proxy(self.config['tracker_proxy'], 'http') try: h = urlopen(request) data = h.read() h.close() # urllib2 can raise various crap that doesn't have a common base # exception class especially when proxies are used, at least # ValueError and stuff from httplib except Exception, e: def f(r='Problem connecting to tracker - ' + str(e)): self._postrequest(errormsg=r, peerid=peerid)
def _get_signature(self, installer_url): """Get the signature (.sign) file from the version site, and unpickle the signature. The sign file is a signature of the .torrent file created with the auto-update tool in auto-update/sign_file.py.""" signature = None try: signfile = zurllib.urlopen(installer_url + '.sign') except: self.debug('_get_signature() run#%d: failed to download signfile %s.sign' % (self.runs, installer_url)) pass else: try: signature = pickle.load(signfile) except: self.debug('_get_signature() run#%d: failed to unpickle signfile %s' % (self.runs, signfile)) pass return signature
def _get_available(self, url): """Get the available version from the version site. The command line option --new_version X.Y.Z overrides this method and returns 'X.Y.Z' instead.""" self.debug('_get_available() run#%d: hitting url %s' % (self.runs, url)) try: u = zurllib.urlopen(url) s = u.read() s = s.strip() except: raise BTFailure(_("Could not get latest version from %s")%url) try: # we're luck asserts are turned off in production. # this assert is false for 4.20.X #assert len(s) == 5 available_version = Version.from_str(s) except: raise BTFailure(_("Could not parse new version string from %s")%url) return available_version
def _get_signature(self, installer_url): """Get the signature (.sign) file from the version site, and unpickle the signature. The sign file is a signature of the .torrent file created with the auto-update tool in auto-update/sign_file.py.""" signature = None try: signfile = zurllib.urlopen(installer_url + '.sign') except: self.debug( '_get_signature() run#%d: failed to download signfile %s.sign' % (self.runs, installer_url)) pass else: try: signature = pickle.load(signfile) except: self.debug( '_get_signature() run#%d: failed to unpickle signfile %s' % (self.runs, signfile)) pass return signature
def _get_available(self, url): """Get the available version from the version site. The command line option --new_version X.Y.Z overrides this method and returns 'X.Y.Z' instead.""" self.debug('_get_available() run#%d: hitting url %s' % (self.runs, url)) try: u = zurllib.urlopen(url) s = u.read() s = s.strip() except: raise BTFailure(_("Could not get latest version from %s") % url) try: # we're luck asserts are turned off in production. # this assert is false for 4.20.X #assert len(s) == 5 available_version = Version.from_str(s) except: raise BTFailure( _("Could not parse new version string from %s") % url) return available_version
def __init__(self,url,save_in,selfish=1): self.status = 0 if __btversion__ >= 4.2: uiname = 'bittorrent-console' else: uiname = 'btdownloadheadless' defaults = get_defaults(uiname) try: config, args = configfile.parse_configuration_and_args(defaults, uiname) config["url"] = url config["save_in"] = save_in config["selfish"] = selfish if args: if config['responsefile']: raise BTFailure, 'must have responsefile as arg or ' \ 'parameter, not both' config['responsefile'] = args[0] try: if config['responsefile']: h = file(config['responsefile'], 'rb') metainfo = h.read() h.close() elif config['url']: h = urlopen(config['url']) metainfo = h.read() h.close() else: raise BTFailure('you need to specify a .torrent file') except IOError, e: raise BTFailure('Error reading .torrent file: ', str(e)) except BTFailure, e: print str(e) self.status = 1
def rerequest(self, url, set): try: if self.config['ip']: url += '&ip=' + gethostbyname(self.config['ip']) h = urlopen(url) r = h.read() h.close() if set(): def add(self=self, r=r): self.last_failed = False self.postrequest(r) self.externalsched(add, 0) except (IOError, error), e: if set(): def fail(self=self, r='Problem connecting to tracker - ' + str(e)): self.errorfunc(WARNING, r) self.last_failed = True self.externalsched(fail, 0)
class Rerequester(object): def __init__(self, url, config, sched, howmany, connect, externalsched, amount_left, up, down, port, myid, infohash, errorfunc, doneflag, upratefunc, downratefunc, ever_got_incoming, diefunc, sfunc): self.baseurl = url self.infohash = infohash self.peerid = None self.wanted_peerid = myid self.port = port self.url = None self.config = config self.last = None self.trackerid = None self.announce_interval = 30 * 60 self.sched = sched self.howmany = howmany self.connect = connect self.externalsched = externalsched self.amount_left = amount_left self.up = up self.down = down self.errorfunc = errorfunc self.doneflag = doneflag self.upratefunc = upratefunc self.downratefunc = downratefunc self.ever_got_incoming = ever_got_incoming self.diefunc = diefunc self.successfunc = sfunc self.finish = False self.current_started = None self.fail_wait = None self.last_time = None self.previous_down = 0 self.previous_up = 0 self.tracker_num_peers = None self.tracker_num_seeds = None def _makeurl(self, peerid, port): return ('%s?info_hash=%s&peer_id=%s&port=%s&key=%s' % (self.baseurl, quote(self.infohash), quote(peerid), str(port), b2a_hex(''.join([chr(randrange(256)) for i in xrange(4)])))) def change_port(self, peerid, port): self.wanted_peerid = peerid self.port = port self.last = None self.trackerid = None self._check() def begin(self): if self.sched: self.sched(self.begin, 60) self._check() def announce_finish(self): self.finish = True self._check() def announce_stop(self): self._announce(2) def _check(self): if self.current_started is not None: if self.current_started <= bttime() - 58: self.errorfunc( WARNING, _("Tracker announce still not complete " "%d seconds after starting it") % int(bttime() - self.current_started)) return if self.peerid is None: self.peerid = self.wanted_peerid self.url = self._makeurl(self.peerid, self.port) self._announce(0) return if self.peerid != self.wanted_peerid: self._announce(2) self.peerid = None self.previous_up = self.up() self.previous_down = self.down() return if self.finish: self.finish = False self._announce(1) return if self.fail_wait is not None: if self.last_time + self.fail_wait <= bttime(): self._announce() return if self.last_time > bttime() - self.config['rerequest_interval']: return if self.ever_got_incoming(): getmore = self.howmany() <= self.config['min_peers'] / 3 else: getmore = self.howmany() < self.config['min_peers'] if getmore or bttime() - self.last_time > self.announce_interval: self._announce() def _announce(self, event=None): self.current_started = bttime() s = ('%s&uploaded=%s&downloaded=%s&left=%s' % (self.url, str(self.up() - self.previous_up), str(self.down() - self.previous_down), str(self.amount_left()))) if self.last is not None: s += '&last=' + quote(str(self.last)) if self.trackerid is not None: s += '&trackerid=' + quote(str(self.trackerid)) if self.howmany() >= self.config['max_initiate']: s += '&numwant=0' else: s += '&compact=1' if event is not None: s += '&event=' + ['started', 'completed', 'stopped'][event] t = Thread(target=self._rerequest, args=[s, self.peerid]) t.setDaemon(True) t.start() # Must destroy all references that could cause reference circles def cleanup(self): self.sched = None self.howmany = None self.connect = None self.externalsched = lambda *args: None self.amount_left = None self.up = None self.down = None self.errorfunc = None self.upratefunc = None self.downratefunc = None self.ever_got_incoming = None self.diefunc = None self.successfunc = None def _rerequest(self, url, peerid): if self.config['ip']: try: url += '&ip=' + gethostbyname(self.config['ip']) except Exception, e: self.errorfunc( WARNING, _("Problem connecting to tracker, gethostbyname failed - ") + str(e)) request = Request(url) request.add_header('User-Agent', 'BitTorrent/' + version) if self.config['tracker_proxy']: request.set_proxy(self.config['tracker_proxy'], 'http') try: h = urlopen(request) data = h.read() h.close() # urllib2 can raise various crap that doesn't have a common base # exception class especially when proxies are used, at least # ValueError and stuff from httplib except Exception, e: def f(r=_("Problem connecting to tracker - ") + str(e)): self._postrequest(errormsg=r, peerid=peerid)
def _update(self, doc=None): u = zurllib.urlopen(self.url) self.data = u.read() u.close() self.main.show_status(self.get_items()) self.main.feed_was_updated(self.url)
sys.exit(1) try: config, args = configfile.parse_configuration_and_args(defaults, uiname, sys.argv[1:], 0, 1) if args: if config['responsefile']: raise BTFailure, 'must have responsefile as arg or ' \ 'parameter, not both' config['responsefile'] = args[0] try: if config['responsefile']: h = file(config['responsefile'], 'rb') metainfo = h.read() h.close() elif config['url']: h = urlopen(config['url']) metainfo = h.read() h.close() else: raise BTFailure('you need to specify a .torrent file') except IOError, e: raise BTFailure('Error reading .torrent file: ', str(e)) except BTFailure, e: print str(e) sys.exit(1) errlist = [] dl = DL(metainfo, config, errlist) curses_wrapper(dl.run) if errlist:
sys.exit(1) try: config, args = configfile.parse_configuration_and_args( defaults, uiname, sys.argv[1:], 0, 1) if args: if config['responsefile']: raise BTFailure, 'must have responsefile as arg or ' \ 'parameter, not both' config['responsefile'] = args[0] try: if config['responsefile']: h = file(config['responsefile'], 'rb') metainfo = h.read() h.close() elif config['url']: h = urlopen(config['url']) metainfo = h.read() h.close() else: raise BTFailure('you need to specify a .torrent file') except IOError, e: raise BTFailure('Error reading .torrent file: ', str(e)) except BTFailure, e: print str(e) sys.exit(1) errlist = [] dl = DL(metainfo, config, errlist) curses_wrapper(dl.run) if errlist:
params = [ "--url", torrent, "--max_upload_rate", maxUploadRate, "--minport", iPXSettings.torrentMinPort, "--maxport", iPXSettings.torrentMinPort, "--save_as", saveLocation + "/" + saveName, ] config, args = configfile.parse_configuration_and_args(defaults, uiname, params, 0, 1) if config["url"]: h = urlopen(config["url"]) metainfo = h.read() h.close() except Exception, msg: logIt("Torrent Download Failed") logIt("ERRORMSG: %s" % str(msg)) status = 0 try: dl = DL(metainfo, config) dl.run() if globals()["torrentStatus"] == 1: logIt("Completed Download: %s" % saveName) status = 1 else: logIt("Torrent Download Failed")