def log(self, permid, decoded_message): lt = T.localtime(T.time()) timestamp = '%04d-%02d-%02d %02d:%02d:%02d' % (lt[0], lt[1], lt[2], lt[3], lt[4], lt[5]) ip = self.peer_db.getPeer(permid, 'ip') s = '%s\t%s\t%s\t%s\n' % (timestamp, bin2str(permid), ip, decoded_message) print dunno2unicode(s) self.logfile.write(dunno2unicode(s)) self.logfile.flush()
def load_from_url(url, use_cache = True): if url.startswith(P2PURL_SCHEME): metainfo, swarmid = makeurl.p2purl2metainfo(url) metainfo['info']['url-compat'] = 1 t = TorrentDef._create(metainfo) return t else: b64_url = b64encode(url) if use_cache: if b64_url in TorrentDef.torrent_cache: tdef_from_cache = TorrentDef.torrent_cache[b64_url] if DEBUG: log('TorrentDef::load_from_url: found in cache: url', url, 'timestamp', tdef_from_cache['timestamp']) if tdef_from_cache['timestamp'] < time.time() - TORRENT_CACHE_EXPIRE: if DEBUG: log('TorrentDef::load_from_url: expired, delete from cache') del TorrentDef.torrent_cache[b64_url] else: return tdef_from_cache['tdef'] if url.startswith('file:///'): try: url = dunno2unicode(urllib2.unquote(url)) except: log_exc() f = urlOpenTimeout(url) tdef = TorrentDef._read(f) if DEBUG: log('TorrentDef::load_from_url: add to cache, url', url) TorrentDef.torrent_cache[b64_url] = {'tdef': tdef.copy(), 'timestamp': time.time()} return tdef
def load_from_url(url, use_cache=True): if url.startswith(P2PURL_SCHEME): metainfo, swarmid = makeurl.p2purl2metainfo(url) metainfo['info']['url-compat'] = 1 t = TorrentDef._create(metainfo) return t else: b64_url = b64encode(url) if use_cache: if b64_url in TorrentDef.torrent_cache: tdef_from_cache = TorrentDef.torrent_cache[b64_url] if DEBUG: log('TorrentDef::load_from_url: found in cache: url', url, 'timestamp', tdef_from_cache['timestamp']) if tdef_from_cache['timestamp'] < time.time( ) - TORRENT_CACHE_EXPIRE: if DEBUG: log('TorrentDef::load_from_url: expired, delete from cache' ) del TorrentDef.torrent_cache[b64_url] else: return tdef_from_cache['tdef'] if url.startswith('file:///'): try: url = dunno2unicode(urllib2.unquote(url)) except: log_exc() f = urlOpenTimeout(url) tdef = TorrentDef._read(f) if DEBUG: log('TorrentDef::load_from_url: add to cache, url', url) TorrentDef.torrent_cache[b64_url] = { 'tdef': tdef.copy(), 'timestamp': time.time() } return tdef
def load_from_url(url, use_cache=True): if url.startswith(P2PURL_SCHEME): metainfo, swarmid = makeurl.p2purl2metainfo(url) metainfo["info"]["url-compat"] = 1 t = TorrentDef._create(metainfo) return t else: b64_url = b64encode(url) if use_cache: if b64_url in TorrentDef.torrent_cache: tdef_from_cache = TorrentDef.torrent_cache[b64_url] if DEBUG: log( "TorrentDef::load_from_url: found in cache: url", url, "timestamp", tdef_from_cache["timestamp"], ) if tdef_from_cache["timestamp"] < time.time() - TORRENT_CACHE_EXPIRE: if DEBUG: log("TorrentDef::load_from_url: expired, delete from cache") del TorrentDef.torrent_cache[b64_url] else: return tdef_from_cache["tdef"] if url.startswith("file:///"): try: url = dunno2unicode(urllib2.unquote(url)) except: log_exc() f = urlOpenTimeout(url) tdef = TorrentDef._read(f) if DEBUG: log("TorrentDef::load_from_url: add to cache, url", url) TorrentDef.torrent_cache[b64_url] = {"tdef": tdef.copy(), "timestamp": time.time()} return tdef
def get_comment_as_unicode(self): return dunno2unicode(self.input['comment'])