Skip to content

Instantly share code, notes, and snippets.

@dyjjones
Created July 6, 2015 03:56
Show Gist options
  • Save dyjjones/52150d729df8a7e23e42 to your computer and use it in GitHub Desktop.
Save dyjjones/52150d729df8a7e23e42 to your computer and use it in GitHub Desktop.
download torrent files
import urllib2
import json
from sys import argv
from bs4 import BeautifulSoup
import StringIO
import gzip
import sh
import zlib
url = "http://metasearch.torrentproject.com/torrenttojson4.php?site=kat.cr&search={}&output=json".format('%20'.join(argv[1:]))
response = urllib2.urlopen(url)
raw_data = json.loads(response.read())
print '\nSearch Term:\t{}\n'.format(' '.join(argv[1:]))
#print raw_data
print "Select torrent:"
data = filter(lambda d: d.get('name'), raw_data)
for i,t in enumerate(data):
if t.get('name'):
try:
print "{})\tName:\t\t{}".format(i+1,t["name"].encode('utf-8'))
print "\tSize:\t\t{}".format(t["size"])
print "\tType:\t\t{}".format(t["cat"])
print "\tSeeds:\t\t{}".format(t["seeds"])
print "\tPeers:\t\t{}".format(t["peers"])
print "\tVerified:\t{}".format(True if t["verified"] else False)
print
except Exception as e:
print e, '\n\n', t
try:
selection = int(raw_input("> "))
except:
exit()
if selection and selection-1 in range(len(data)):
print 'success {}'.format(data[selection-1]['info'])
torrent_page_url = data[selection-1]['info']
torrent_page_response = urllib2.urlopen(torrent_page_url)
#print 'NNNNN', dir(torrent_page_response)
buf = StringIO.StringIO(torrent_page_response.read())
gzip_f = gzip.GzipFile(fileobj=buf)
torrent_page_data = gzip_f.read()
#print torrent_page_data
soup = BeautifulSoup(torrent_page_data, 'html.parser')
for link in soup.find_all('a'):
classes = link.get('class')
if classes and 'verifTorrentButton' in classes:
torrent_link = link.get('href')
with open(torrent_link.split('title=')[1] + '.torrent', 'w') as f:
req = urllib2.Request(torrent_link)
torrent_response = urllib2.urlopen(req)
torrent_data = torrent_response.read()
decompressed_torrent_data = zlib.decompress(torrent_data, 16+zlib.MAX_WBITS)
f.write(decompressed_torrent_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment