# -*- coding: utf8 -*- import os import urllib import urllib2 import sys import time import re import uuid import gzip target_dir = "/home/facundo/Música/Discos" SEARCH = "http://kat.ph/usearch/%s/?categories[]=movies" def getit(html, fname): """Download the .torrent.""" m = re.findall('"(https://torcache.net/torrent/[^"]*?)"', html, re.M) if not m: print "WARNING: no url" return for url in m: print url, try: u = urllib2.urlopen(url) data = u.read() except StandardError, e: print "WARNING: no pudimos traer la url", e return uniq = str(uuid.uuid4()) cname = fname + "." + uniq + ".gz" rname = fname + "." + uniq + ".torrent" with open(cname, "wb") as fh: fh.write(data) try: fh = gzip.open(cname) unc = fh.read() fh.close() except StandardError, e: print "WARNING: no decompress", cname return with open(rname, "wb") as fh: fh.write(unc) os.unlink(cname) print "ok" time.sleep(2) all_paths = [] for dirpath, dirnames, fnames in os.walk(target_dir): for fname in fnames: fullpath = os.path.join(dirpath, fname) if fname.endswith('.mp3'): all_paths.append(fullpath) all_paths.sort() all_paths = [x.replace('-', '') for x in all_paths] for path in all_paths: interp, _, tema = path.split("/")[-3:] data = "%s %s" % (interp, tema[4:-4]) search = SEARCH % (urllib.quote_plus(data.lower()),) try: u = urllib2.urlopen(search) html = u.read() except Exception, err: print path, "ERROR:", err time.sleep(60) # extra time just in case else: if "Nothing found!" in html: print path, "nop" else: print path, "YES!" getit(html, data) sys.stdout.flush() time.sleep(5)