def get_bitrate_from_metainfo(file, metainfo): info = metainfo['info'] if file is None or 'files' not in info: # if no file is specified or this is a single file torrent bitrate = None try: playtime = None if 'playtime' in info: # print >>sys.stderr,"TorrentDef: get_bitrate: Bitrate in info field" playtime = parse_playtime_to_secs(info['playtime']) elif 'playtime' in metainfo: # HACK: encode playtime in non-info part of existing torrent # print >>sys.stderr,"TorrentDef: get_bitrate: Bitrate in metainfo" playtime = parse_playtime_to_secs(metainfo['playtime']) elif 'azureus_properties' in metainfo: azprop = metainfo['azureus_properties'] if 'Content' in azprop: content = metainfo['azureus_properties']['Content'] if 'Speed Bps' in content: bitrate = float(content['Speed Bps']) # print >>sys.stderr,"TorrentDef: get_bitrate: Bitrate in Azureus metainfo",bitrate if playtime is not None: bitrate = info['length'] / playtime if DEBUG: print >> sys.stderr, "TorrentDef: get_bitrate: Found bitrate", bitrate except: print_exc() return bitrate else: for i in range(len(info['files'])): x = info['files'][i] intorrentpath = '' for elem in x['path']: intorrentpath = os.path.join(intorrentpath, elem) bitrate = None try: playtime = None if 'playtime' in x: playtime = parse_playtime_to_secs(x['playtime']) elif 'playtime' in metainfo: # HACK: encode playtime in non-info part of existing torrent playtime = parse_playtime_to_secs(metainfo['playtime']) elif 'azureus_properties' in metainfo: azprop = metainfo['azureus_properties'] if 'Content' in azprop: content = metainfo['azureus_properties']['Content'] if 'Speed Bps' in content: bitrate = float(content['Speed Bps']) # print >>sys.stderr,"TorrentDef: get_bitrate: Bitrate in Azureus metainfo",bitrate if playtime is not None: bitrate = x['length'] / playtime except: print_exc() if intorrentpath == file: return bitrate raise ValueError("File not found in torrent")
def get_bitrate_from_metainfo(file, metainfo): info = metainfo["info"] if file is None or "files" not in info: # if no file is specified or this is a single file torrent bitrate = None try: playtime = None if info.has_key("playtime"): # print >>sys.stderr,"TorrentDef: get_bitrate: Bitrate in info field" playtime = parse_playtime_to_secs(info["playtime"]) elif "playtime" in metainfo: # HACK: encode playtime in non-info part of existing torrent # print >>sys.stderr,"TorrentDef: get_bitrate: Bitrate in metainfo" playtime = parse_playtime_to_secs(metainfo["playtime"]) elif "azureus_properties" in metainfo: azprop = metainfo["azureus_properties"] if "Content" in azprop: content = metainfo["azureus_properties"]["Content"] if "Speed Bps" in content: bitrate = float(content["Speed Bps"]) # print >>sys.stderr,"TorrentDef: get_bitrate: Bitrate in Azureus metainfo",bitrate if playtime is not None: bitrate = info["length"] / playtime if DEBUG: print >>sys.stderr, "TorrentDef: get_bitrate: Found bitrate", bitrate except: print_exc() return bitrate else: for i in range(len(info["files"])): x = info["files"][i] intorrentpath = "" for elem in x["path"]: intorrentpath = os.path.join(intorrentpath, elem) bitrate = None try: playtime = None if x.has_key("playtime"): playtime = parse_playtime_to_secs(x["playtime"]) elif "playtime" in metainfo: # HACK: encode playtime in non-info part of existing torrent playtime = parse_playtime_to_secs(metainfo["playtime"]) elif "azureus_properties" in metainfo: azprop = metainfo["azureus_properties"] if "Content" in azprop: content = metainfo["azureus_properties"]["Content"] if "Speed Bps" in content: bitrate = float(content["Speed Bps"]) # print >>sys.stderr,"TorrentDef: get_bitrate: Bitrate in Azureus metainfo",bitrate if playtime is not None: bitrate = x["length"] / playtime except: print_exc() if intorrentpath == file: return bitrate raise ValueError("File not found in torrent")
def make_torrent_file(input, userabortflag = None, userprogresscallback = lambda x: None): """ Create a torrent file from the supplied input. Returns a (infohash,metainfo) pair, or (None,None) on userabort. """ (info,piece_length) = makeinfo(input,userabortflag,userprogresscallback) if userabortflag is not None and userabortflag.isSet(): return (None,None) if info is None: return (None,None) #if DEBUG: # print >>sys.stderr,"mktorrent: makeinfo returned",`info` check_info(info) metainfo = {'info': info, 'encoding': input['encoding'], 'creation date': long(time())} # http://www.bittorrent.org/DHT_protocol.html says both announce and nodes # are not allowed, but some torrents (Azureus?) apparently violate this. if input['nodes'] is None and input['announce'] is None: raise ValueError('No tracker set') for key in ['announce','announce-list','nodes','comment','created by','httpseeds']: if input[key] is not None and len(input[key]) > 0: metainfo[key] = input[key] if key == 'comment': metainfo['comment.utf-8'] = uniconvert(input['comment'],'utf-8') # Assuming 1 file, Azureus format no support multi-file torrent with diff # bitrates bitrate = None for file in input['files']: if file['playtime'] is not None: secs = parse_playtime_to_secs(file['playtime']) bitrate = file['length']/secs break if input.get('bps') is not None: bitrate = input['bps'] break if bitrate is not None or input['thumb'] is not None: mdict = {} mdict['Publisher'] = 'Tribler' if input['comment'] is None: descr = '' else: descr = input['comment'] mdict['Description'] = descr if bitrate is not None: mdict['Progressive'] = 1 mdict['Speed Bps'] = bitrate else: mdict['Progressive'] = 0 mdict['Title'] = metainfo['info']['name'] mdict['Creation Date'] = long(time()) # Azureus client source code doesn't tell what this is, so just put in random value from real torrent mdict['Content Hash'] = 'PT3GQCPW4NPT6WRKKT25IQD4MU5HM4UY' mdict['Revision Date'] = long(time()) if input['thumb'] is not None: mdict['Thumbnail'] = input['thumb'] cdict = {} cdict['Content'] = mdict metainfo['azureus_properties'] = cdict if input['torrentsigkeypairfilename'] is not None: create_torrent_signature(metainfo,input['torrentsigkeypairfilename']) infohash = sha(bencode(info)).digest() return (infohash,metainfo)
def make_torrent_file(input, userabortflag = None, userprogresscallback = lambda x: None): """ Create a torrent file from the supplied input. Returns a (infohash,metainfo) pair, or (None,None) on userabort. """ (info,piece_length) = makeinfo(input,userabortflag,userprogresscallback) if userabortflag is not None and userabortflag.isSet(): return (None,None) if info is None: return (None,None) #if DEBUG: # print >>sys.stderr,"mktorrent: makeinfo returned",`info` check_info(info) metainfo = {'info': info, 'encoding': input['encoding'], 'creation date': long(time())} # http://www.bittorrent.org/DHT_protocol.html says both announce and nodes # are not allowed, but some torrents (Azureus?) apparently violate this. if input['nodes'] is None and input['announce'] is None: raise ValueError('No tracker set') for key in ['announce','announce-list','nodes','comment','created by','httpseeds', 'url-list']: if input[key] is not None and len(input[key]) > 0: metainfo[key] = input[key] if key == 'comment': metainfo['comment.utf-8'] = uniconvert(input['comment'],'utf-8') # Assuming 1 file, Azureus format no support multi-file torrent with diff # bitrates bitrate = None for file in input['files']: if file['playtime'] is not None: secs = parse_playtime_to_secs(file['playtime']) bitrate = file['length']/secs break if input.get('bps') is not None: bitrate = input['bps'] break if bitrate is not None or input['thumb'] is not None: mdict = {} mdict['Publisher'] = 'Tribler' if input['comment'] is None: descr = '' else: descr = input['comment'] mdict['Description'] = descr if bitrate is not None: mdict['Progressive'] = 1 mdict['Speed Bps'] = int(bitrate) # bencode fails for float else: mdict['Progressive'] = 0 mdict['Title'] = metainfo['info']['name'] mdict['Creation Date'] = long(time()) # Azureus client source code doesn't tell what this is, so just put in random value from real torrent mdict['Content Hash'] = 'PT3GQCPW4NPT6WRKKT25IQD4MU5HM4UY' mdict['Revision Date'] = long(time()) if input['thumb'] is not None: mdict['Thumbnail'] = input['thumb'] cdict = {} cdict['Content'] = mdict metainfo['azureus_properties'] = cdict if input['torrentsigkeypairfilename'] is not None: from Tribler.Core.Overlay.permid import create_torrent_signature create_torrent_signature(metainfo,input['torrentsigkeypairfilename']) if 'url-compat' in input: metainfo['info']['url-compat'] = input['url-compat'] # Arno, 2010-03-02: # Theoretically should go into 'info' field, to get infohash protection # because the video won't play without them. In the future we'll sign # the whole .torrent IMHO so it won't matter. Keeping it out of 'info' # at the moment makes the .tstream files more stable (in case you restart # the live source, and the Ogg header generated contains some date or # what not, we'd need a new .tstream to be distributed to all. # if 'ogg-headers' in input: metainfo['ogg-headers'] = input['ogg-headers'] # Two places where infohash calculated, here and in TorrentDef. # Elsewhere: must use TorrentDef.get_infohash() to allow P2PURLs. infohash = sha(bencode(info)).digest() return (infohash,metainfo)
from Tribler.Core.APIImplementation.miscutils import parse_playtime_to_secs assert parse_playtime_to_secs("0") == 0 assert parse_playtime_to_secs("0.1") == 0 assert parse_playtime_to_secs("1:00") == 60 assert parse_playtime_to_secs("1:0.3") == 60 assert parse_playtime_to_secs("10:00") == 600 assert parse_playtime_to_secs("10:56:11") == 39371 assert parse_playtime_to_secs("10:56:11.77") == 39371
def make_torrent_file(input, userabortflag=None, userprogresscallback=lambda x: None): """ Create a torrent file from the supplied input. Returns a (infohash,metainfo) pair, or (None,None) on userabort. """ (info, piece_length) = makeinfo(input, userabortflag, userprogresscallback) if userabortflag is not None and userabortflag.isSet(): return (None, None) if info is None: return (None, None) # if DEBUG: # print >>sys.stderr,"mktorrent: makeinfo returned",`info` metainfo = {'info': info, 'encoding': input['encoding'], 'creation date': long(time())} validTorrentFile(metainfo) # http://www.bittorrent.org/DHT_protocol.html says both announce and nodes # are not allowed, but some torrents (Azureus?) apparently violate this. if input['nodes'] is None and input['announce'] is None: raise ValueError('No tracker set') for key in ['announce', 'announce-list', 'nodes', 'comment', 'created by', 'httpseeds', 'url-list']: if input[key] is not None and len(input[key]) > 0: metainfo[key] = input[key] if key == 'comment': metainfo['comment.utf-8'] = uniconvert(input['comment'], 'utf-8') # Assuming 1 file, Azureus format no support multi-file torrent with diff # bitrates bitrate = None for file in input['files']: if file['playtime'] is not None: secs = parse_playtime_to_secs(file['playtime']) bitrate = file['length'] / secs break if input.get('bps') is not None: bitrate = input['bps'] break if bitrate is not None or input['thumb'] is not None: mdict = {} mdict['Publisher'] = 'Tribler' if input['comment'] is None: descr = '' else: descr = input['comment'] mdict['Description'] = descr if bitrate is not None: mdict['Progressive'] = 1 mdict['Speed Bps'] = int(bitrate) # bencode fails for float else: mdict['Progressive'] = 0 mdict['Title'] = metainfo['info']['name'] mdict['Creation Date'] = long(time()) # Azureus client source code doesn't tell what this is, so just put in random value from real torrent mdict['Content Hash'] = 'PT3GQCPW4NPT6WRKKT25IQD4MU5HM4UY' mdict['Revision Date'] = long(time()) if input['thumb'] is not None: mdict['Thumbnail'] = input['thumb'] cdict = {} cdict['Content'] = mdict metainfo['azureus_properties'] = cdict if input['torrentsigkeypairfilename'] is not None: from Tribler.Core.Overlay.permid import create_torrent_signature create_torrent_signature(metainfo, input['torrentsigkeypairfilename']) if 'url-compat' in input: metainfo['info']['url-compat'] = input['url-compat'] # Arno, 2010-03-02: # Theoretically should go into 'info' field, to get infohash protection # because the video won't play without them. In the future we'll sign # the whole .torrent IMHO so it won't matter. Keeping it out of 'info' # at the moment makes the .tstream files more stable (in case you restart # the live source, and the Ogg header generated contains some date or # what not, we'd need a new .tstream to be distributed to all. # if 'ogg-headers' in input: metainfo['ogg-headers'] = input['ogg-headers'] # Two places where infohash calculated, here and in TorrentDef. # Elsewhere: must use TorrentDef.get_infohash() to allow P2PURLs. infohash = sha(bencode(info)).digest() return (infohash, metainfo)
def make_torrent_file(input, userabortflag=None, userprogresscallback=lambda x: None): """ Create a torrent file from the supplied input. Returns a (infohash,metainfo) pair, or (None,None) on userabort. """ (info, piece_length) = makeinfo(input, userabortflag, userprogresscallback) if userabortflag is not None and userabortflag.isSet(): return (None, None) if info is None: return (None, None) #if DEBUG: # print >>sys.stderr,"mktorrent: makeinfo returned",`info` check_info(info) metainfo = { 'info': info, 'encoding': input['encoding'], 'creation date': long(time()) } # http://www.bittorrent.org/DHT_protocol.html says both announce and nodes # are not allowed, but some torrents (Azureus?) apparently violate this. if input['nodes'] is None and input['announce'] is None: raise ValueError('No tracker set') for key in [ 'announce', 'announce-list', 'nodes', 'comment', 'created by', 'httpseeds' ]: if input[key] is not None and len(input[key]) > 0: metainfo[key] = input[key] if key == 'comment': metainfo['comment.utf-8'] = uniconvert(input['comment'], 'utf-8') # Assuming 1 file, Azureus format no support multi-file torrent with diff # bitrates bitrate = None for file in input['files']: if file['playtime'] is not None: secs = parse_playtime_to_secs(file['playtime']) bitrate = file['length'] / secs break if input.get('bps') is not None: bitrate = input['bps'] break if bitrate is not None or input['thumb'] is not None: mdict = {} mdict['Publisher'] = 'Tribler' if input['comment'] is None: descr = '' else: descr = input['comment'] mdict['Description'] = descr if bitrate is not None: mdict['Progressive'] = 1 mdict['Speed Bps'] = bitrate else: mdict['Progressive'] = 0 mdict['Title'] = metainfo['info']['name'] mdict['Creation Date'] = long(time()) # Azureus client source code doesn't tell what this is, so just put in random value from real torrent mdict['Content Hash'] = 'PT3GQCPW4NPT6WRKKT25IQD4MU5HM4UY' mdict['Revision Date'] = long(time()) if input['thumb'] is not None: mdict['Thumbnail'] = input['thumb'] cdict = {} cdict['Content'] = mdict metainfo['azureus_properties'] = cdict if input['torrentsigkeypairfilename'] is not None: create_torrent_signature(metainfo, input['torrentsigkeypairfilename']) infohash = sha(bencode(info)).digest() return (infohash, metainfo)