Fix missing import, pep8

This commit is contained in:
Chase Sterling 2014-02-19 23:46:53 -05:00
parent fff75b51ce
commit c64da3ceb4
1 changed files with 38 additions and 35 deletions

View File

@ -47,6 +47,7 @@ import pkg_resources
import gettext import gettext
import locale import locale
import base64 import base64
import urllib
try: try:
import json import json
@ -505,48 +506,50 @@ def is_magnet(uri):
return True return True
return False return False
def get_magnet_info(uri): def get_magnet_info(uri):
""" """
Return information about a magnet link. Return information about a magnet link.
:param uri: the magnet link :param uri: the magnet link
:type uri: string :type uri: string
:returns: information about the magnet link: :returns: information about the magnet link:
:: ::
{ {
"name": the torrent name, "name": the torrent name,
"info_hash": the torrents info_hash, "info_hash": the torrents info_hash,
"files_tree": empty value for magnet links "files_tree": empty value for magnet links
} }
:rtype: dictionary :rtype: dictionary
""" """
magnet_scheme = 'magnet:?' magnet_scheme = 'magnet:?'
xt_param = 'xt=urn:btih:' xt_param = 'xt=urn:btih:'
dn_param = 'dn=' dn_param = 'dn='
if uri.startswith(magnet_scheme): if uri.startswith(magnet_scheme):
name = None name = None
info_hash = None info_hash = None
for param in uri[len(magnet_scheme):].split('&'): for param in uri[len(magnet_scheme):].split('&'):
if param.startswith(xt_param): if param.startswith(xt_param):
xt_hash = param[len(xt_param):] xt_hash = param[len(xt_param):]
if len(xt_hash) == 32: if len(xt_hash) == 32:
info_hash = base64.b32decode(xt_hash).encode("hex") info_hash = base64.b32decode(xt_hash).encode("hex")
elif len(xt_hash) == 40: elif len(xt_hash) == 40:
info_hash = xt_hash info_hash = xt_hash
else: else:
break break
elif param.startswith(dn_param): elif param.startswith(dn_param):
name = unquote_plus(param[len(dn_param):]) name = urllib.unquote_plus(param[len(dn_param):])
if info_hash:
if not name:
name = info_hash
return {"name": name, "info_hash": info_hash, "files_tree": ''}
return False
if info_hash:
if not name:
name = info_hash
return {"name":name, "info_hash":info_hash, "files_tree":''}
return False
def create_magnet_uri(infohash, name=None, trackers=[]): def create_magnet_uri(infohash, name=None, trackers=[]):
""" """