diff --git a/pirate-get.py b/pirate-get.py index ecda563..d4c2e11 100755 --- a/pirate-get.py +++ b/pirate-get.py @@ -101,6 +101,7 @@ def main(): try: sizes = [] uploaded = [] + identifiers = [] for page in xrange(pages): # @@ -133,6 +134,7 @@ def main(): # print res sizes.extend([match.replace(" ", " ") for match in re.findall("(?<=Size )[0-9.]+\ \;[KMGT]*[i ]*B",res)]) uploaded.extend([match.replace(" ", " ") for match in re.findall("(?<=Uploaded ).+(?=\, Size)",res)]) + identifiers.extend([match.replace(" ", " ") for match in re.findall("(?<=/torrent/)[0-9]+(?=/)",res)]) # pprint(sizes); print len(sizes) # pprint(uploaded); print len(uploaded) state = "seeds" @@ -154,7 +156,7 @@ def main(): exit() # return the sizes in a spearate list - return res_l, sizes, uploaded + return res_l, sizes, uploaded, identifiers args = parser.parse_args() @@ -221,7 +223,7 @@ def main(): for mirror in mirrors: try: print("Trying " + mirror) - mags, sizes, uploaded = remote(args, mirror) + mags, sizes, uploaded, identifiers = remote(args, mirror) break except Exception, e: print(format(e)) @@ -231,31 +233,73 @@ def main(): print("no results") return # enhanced print output with column titles - print("%5s %6s %6s %-5s %-11s %-11s %s" \ - % ( "LINK", "SEED", "LEECH", "RATIO", "SIZE", "UPLOAD", "NAME"), - color="header") - cur_color = "zebra_0" - for m in range(len(mags)): - magnet = mags[m] - no_seeders = int(magnet[1]) - no_leechers = int(magnet[2]) - name = re.search("dn=([^\&]*)", magnet[0]) + def print_search_results(): + print("%5s %6s %6s %-5s %-11s %-11s %s" \ + % ( "LINK", "SEED", "LEECH", "RATIO", "SIZE", "UPLOAD", "NAME"), + color="header") + cur_color = "zebra_0" + for m in range(len(mags)): + magnet = mags[m] + no_seeders = int(magnet[1]) + no_leechers = int(magnet[2]) + name = re.search("dn=([^\&]*)", magnet[0]) - # compute the S/L ratio (Higher is better) - try: - ratio = no_seeders/no_leechers - except ZeroDivisionError: - ratio = -1 + # compute the S/L ratio (Higher is better) + try: + ratio = no_seeders/no_leechers + except ZeroDivisionError: + ratio = -1 - # Alternate between colors - cur_color = "zebra_0" if (cur_color == "zebra_1") else "zebra_1" + # Alternate between colors + cur_color = "zebra_0" if (cur_color == "zebra_1") else "zebra_1" - torrent_name = urllib.unquote(name.group(1).encode('ascii')) \ - .decode('utf-8').replace("+", " ") - # enhanced print output with justified columns - print ("%5d %6d %6d %5.1f %-11s %-11s %s" % ( - m, no_seeders, no_leechers, ratio ,sizes[m], - uploaded[m], torrent_name), color=cur_color) + torrent_name = urllib.unquote(name.group(1).encode('ascii')) \ + .decode('utf-8').replace("+", " ") + # enhanced print output with justified columns + print ("%5d %6d %6d %5.1f %-11s %-11s %s" % ( + m, no_seeders, no_leechers, ratio ,sizes[m], + uploaded[m], torrent_name), color=cur_color) + def print_descriptions(chosen_links): + for link in chosen_links: + path = '/torrent/' + identifiers[int(link)] + '/' + request = urllib2.Request(mirror + path) + request.add_header('Accept-encoding', 'gzip') + f = urllib2.urlopen(request) + if f.info().get('Content-Encoding') == 'gzip': + buf = StringIO(f.read()) + f = gzip.GzipFile(fileobj=buf) + res = f.read() + name = re.search("dn=([^\&]*)", mags[int(link)][0]) + torrent_name = urllib.unquote(name.group(1).encode('ascii')) \ + .decode('utf-8').replace("+", " ") + desc = re.search(r"
(.+?)(?=)", res, re.DOTALL).group(1) + # Replace HTML links with markdown style versions + desc = re.sub(r"]*>(\s*)([^<]+?)(\s*)", r"\2[\3](\1)\4", desc) + print ('Description for "' + torrent_name + '":', color="zebra_1") + print (desc, color="zebra_0") + + def print_fileLists(chosen_links): + for link in chosen_links: + path = '/ajax_details_filelist.php' + query = '?id=' + identifiers[int(link)] + request = urllib2.Request(mirror + path + query) + request.add_header('Accept-encoding', 'gzip') + f = urllib2.urlopen(request) + if f.info().get('Content-Encoding') == 'gzip': + buf = StringIO(f.read()) + f = gzip.GzipFile(fileobj=buf) + res = f.read().replace(" ", " ") + files = re.findall(r"