Skip to content

Instantly share code, notes, and snippets.

@wenLiangcan
Last active March 22, 2023 09:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wenLiangcan/387f502720b5fb70fc39 to your computer and use it in GitHub Desktop.
Save wenLiangcan/387f502720b5fb70fc39 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import re
import sys
if sys.version_info[0] == 2:
import urllib2
import urllib
import urlparse
input = raw_input
else:
import urllib.request as urllib2
import urllib.parse as urllib
urlparse = urllib
def input_url():
if len(sys.argv) == 1:
url = input('Enter url --> ')
else:
url = sys.argv[1]
return url
def parse(url):
sfx = '.torrent'
ptn = re.compile(r'^magnet:\?xt=urn:btih:(\w{40}).*?$')
token = ptn.findall(url)[0].upper()
name = token + sfx
link = "http://bt.box.n0808.com/{}/{}/{}".format(
token[:2], token[-2:], name)
dn = urlparse.parse_qs(url).get('dn')
if dn is not None and len(dn) > 0:
filename = urllib.unquote(dn[0])
try:
filename = filename.decode('utf-8')
except AttributeError:
pass
filename += sfx
else:
filename = name
return link, filename
def download(source):
url, filename = source
torrent = urllib2.urlopen(url).read()
with open(filename, 'wb') as output:
output.write(torrent)
if __name__ == "__main__":
download(parse(input_url()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment