diff --git a/pirate-get.py b/pirate-get.py index 4af33a2..0e6d217 100755 --- a/pirate-get.py +++ b/pirate-get.py @@ -5,6 +5,7 @@ import urllib2 import re from HTMLParser import HTMLParser import argparse +import os # create a subclass and override the handler methods @@ -39,9 +40,14 @@ class MyHTMLParser(HTMLParser): def main(): parser = argparse.ArgumentParser(description='Finds and downloads torrents from the Pirate Bay') parser.add_argument('q', metavar='search_term', help="The term to search for") + parser.add_argument('-d', dest='transmission', help="Should add to transmission", default=False) + parser.add_argument('--download',dest='transmission',action='store_true', help="call transmission-remote to start the download", default=True) + parser.add_argument('--auto',dest='auto',action='store_true', help="auto select the url to download", default=False) parser.add_argument('--local', dest='database', help="An xml file containing the Pirate Bay database") parser.add_argument('-p', dest='pages', help="The number of pages to fetch (doesn't work with --local)", default=1) + def compare(a,b): + return -1 * cmp(a['s'], b['s']) def local(args): xml_str = '' with open(args.database, 'r') as f: @@ -86,20 +92,41 @@ def main(): else: mags = remote(args) + L = [] if mags and len(mags) > 0: print "S=seeders" print "L=leechers" for m in range(len(mags)): magnet = mags[m] name = re.search("dn=([^\&]*)", magnet[0]) + d = dict() + d['i'] = m + # d['u'] = magnet[0] + d['s'] = (int(magnet[1]) + int(magnet[2])) + L.append(d) print str(m) + '. S:' + str(magnet[1]) + ' L:' + str(magnet[2]) + ' ', urllib.unquote(name.group(1).encode('ascii')).decode('utf-8').replace("+", " ") - l = raw_input("Select a link: ") - try: - choice = int(l) - except Exception: - choice = None + L.sort(compare) + if not args.auto: + l = raw_input("Select a link: ") + try: + choice = int(l) + except Exception: + choice = None + else: + try: + choice = int(L[0]['i']) + except Exception: + choice = None + url = mags[choice][0] + if not choice == None: - webbrowser.open(mags[choice][0]) + print "" + print "url:" + print url + c = """transmission-remote --add "%s" """ % (url) + if args.transmission: + os.system(c) + os.system("transmission-remote -l") else: print "Cancelled." else: