From 3affc9171d0d520dca95b1e2c7ae7a607005f0e7 Mon Sep 17 00:00:00 2001 From: repic Date: Mon, 11 Nov 2013 22:39:40 +0100 Subject: [PATCH] Enhanced output Added column titles, justified columns, S/L ratio and file size --- pirate-get.py | 69 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/pirate-get.py b/pirate-get.py index 4af33a2..0140239 100755 --- a/pirate-get.py +++ b/pirate-get.py @@ -60,44 +60,67 @@ def main(): except Exception: raise Exception("Please provide an integer greater than 0 for the number of pages to fetch.") - for page in xrange(pages): - f = urllib2.urlopen('http://thepiratebay.se/search/' + args.q.replace(" ", "+") + '/' + str(page) + '/7/0') - res = f.read() - found = re.findall(""""(magnet\:\?xt=[^"]*)|([^<]+)""", res) - state = "seeds" - curr = ['',0,0] #magnet, seeds, leeches - for f in found: - if f[1] == '': - curr[0] = f[0] - else: - if state == 'seeds': - curr[1] = f[1] - state = 'leeches' + # Catch the Ctrl-C exception and exit cleanly + try: + for page in xrange(pages): + f = urllib2.urlopen('http://thepiratebay.se/search/' + args.q.replace(" ", "+") + '/' + str(page) + '/7/0') + res = f.read() + found = re.findall(""""(magnet\:\?xt=[^"]*)|([^<]+)""", res) + + # get sizes as well and substitute the   character + sizes = [ match.replace(" ", " ") for match in re.findall("(?<=Size )[0-9]+\.[0-9]+\ \;[KMGT]iB",res) ] + + state = "seeds" + curr = ['',0,0] #magnet, seeds, leeches + for f in found: + if f[1] == '': + curr[0] = f[0] else: - curr[2] = f[1] - state = 'seeds' - res_l.append(curr) - curr = ['', 0, 0] - return res_l + if state == 'seeds': + curr[1] = f[1] + state = 'leeches' + else: + curr[2] = f[1] + state = 'seeds' + res_l.append(curr) + curr = ['', 0, 0] + except KeyboardInterrupt : + print "\nCancelled." + exit() + + # return the sizes in a spearate list + return res_l, sizes args = parser.parse_args() if args.database: mags = local(args) else: - mags = remote(args) + mags, sizes = remote(args) if mags and len(mags) > 0: - print "S=seeders" - print "L=leechers" + # enhanced print output with column titles + print "\n%-5s %-6s %-6s %-5s %-11s %s" % ( "LINK", "SEED", "LEECH", "RATIO", "SIZE", "NAME") for m in range(len(mags)): magnet = mags[m] name = re.search("dn=([^\&]*)", magnet[0]) - 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: ") + + # compute the S/L ratio (Higher is better) + ratio = float(magnet[1])/float(magnet[2]) + + # enhanced print output with justified columns + print "%-5s %-6s %-6s %5.1f %-11s %s" % (m, magnet[1], magnet[2], ratio ,sizes[m], urllib.unquote(name.group(1).encode('ascii')).decode('utf-8').replace("+", " ") ) + + try: + l = raw_input("Select a link: ") + except KeyboardInterrupt : + print "\nCancelled." + exit() + try: choice = int(l) except Exception: choice = None + if not choice == None: webbrowser.open(mags[choice][0]) else: