diff --git a/pirate-get.py b/pirate-get.py index 2b27294..730186d 100755 --- a/pirate-get.py +++ b/pirate-get.py @@ -34,7 +34,6 @@ from html.parser import HTMLParser from pprint import pprint from io import StringIO, BytesIO -class NoRedirection(urllib2.HTTPErrorProcessor): class NoRedirection(request.HTTPErrorProcessor): def http_response(self, request, response): @@ -76,7 +75,6 @@ class MyHTMLParser(HTMLParser): def main(): - # new ConfigParser config = configparser.ConfigParser() @@ -94,25 +92,108 @@ def main(): config.read([config_to_load()]) - parser = argparse.ArgumentParser(description='Finds and downloads torrents from the Pirate Bay') - parser.add_argument('-b', dest='browse', action='store_true', help="Display in Browse mode", default=False) - parser.add_argument('search', metavar='search', nargs="*", help="Term to search for") - parser.add_argument('-c', dest='category', metavar='category', help="Specify a category to search", default="All") - parser.add_argument('-s', dest='sort', metavar='sort', help="Specify a sort option", default="SeedersDsc") - parser.add_argument('-R', dest='recent', action='store_true', help="Torrents uploaded in the last 48hours. *ignored in searches*", default=False) - parser.add_argument('-l', dest='list_categories', action='store_true', help="List categories", default=False) - parser.add_argument('--list_sorts', dest='list_sorts', action='store_true', help="List Sortable Types", default=False) - parser.add_argument('-t',dest='transmission',action='store_true', help="call transmission-remote to start the download", default=False) - parser.add_argument('--custom',dest='command', help="call custom command, %%s will be replaced with the url") - 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) - parser.add_argument('-0', dest='first', action='store_true', help="choose the top result", default=False) - parser.add_argument('-a', dest='download_all', action='store_true', help="download all results", default=False) - parser.add_argument('--color', dest='color', action='store_true', help="use colored output", default=False) + parser = argparse.ArgumentParser( + description='finds and downloads torrents from the Pirate Bay') + parser.add_argument('-b', dest='browse', + action='store_true', + help="display in Browse mode") + parser.add_argument('search', metavar='search', + nargs="*", help="term to search for") + parser.add_argument('-c', dest='category', metavar='category', + help="specify a category to search", default="All") + parser.add_argument('-s', dest='sort', metavar='sort', + help="specify a sort option", default="SeedersDsc") + parser.add_argument('-R', dest='recent', action='store_true', + help="torrents uploaded in the last 48hours." + "*ignored in searches*") + parser.add_argument('-l', dest='list_categories', + action='store_true', + help="list categories") + parser.add_argument('--list_sorts', dest='list_sorts', + action='store_true', + help="list Sortable Types") + parser.add_argument('-t', dest='transmission', + action='store_true', + help="call transmission-remote to start the download") + parser.add_argument('--custom', dest='command', + help="call custom command, %%s will be replaced with" + "the url") + parser.add_argument('--local', dest='database', + help="an xml file containing the Pirate Bay database") + parser.add_argument('-p', dest='pages', default=1, + help="the number of pages to fetch (doesn't work with" + "--local)") + parser.add_argument('-0', dest='first', + action='store_true', + help="choose the top result") + parser.add_argument('-a', dest='download_all', + action='store_true', + help="download all results") + parser.add_argument('--color', dest='color', + action='store_true', + help="use colored output") - categories = {"All":"0","Audio":"100","Audio/Music":"101","Audio/Audio books":"102","Audio/Sound clips":"103","Audio/FLAC":"104","Audio/Other":"199","Video":"200","Video/Movies":"201","Video/Movies DVDR":"202","Video/Music videos":"203","Video/Movie clips":"204","Video/TV shows":"205","Video/Handheld":"206","Video/HD - Movies":"207","Video/HD - TV shows":"208","Video/3D":"209","Video/Other":"299","Applications":"300","Applications/Windows":"301","Applications/Mac":"302","Applications/UNIX":"303","Applications/Handheld":"304","Applications/IOS (iPad/iPhone)":"305","Applications/Android":"306","Applications/Other OS":"399","Games":"400","Games/PC":"401","Games/Mac":"402","Games/PSx":"403","Games/XBOX360":"404","Games/Wii":"405","Games/Handheld":"406","Games/IOS (iPad/iPhone)":"407","Games/Android":"408","Games/Other":"499","Porn":"500","Porn/Movies":"501","Porn/Movies DVDR":"502","Porn/Pictures":"503","Porn/Games":"504","Porn/HD - Movies":"505","Porn/Movie clips":"506","Porn/Other":"599","Other":"600","Other/E-books":"601","Other/Comics":"602","Other/Pictures":"603","Other/Covers":"604","Other/Physibles":"605","Other/Other":"699"} + categories = { + "All":"0", + "Audio":"100", + "Audio/Music":"101", + "Audio/Audio books":"102", + "Audio/Sound clips":"103", + "Audio/FLAC":"104", + "Audio/Other":"199", + "Video":"200", + "Video/Movies":"201", + "Video/Movies DVDR":"202", + "Video/Music videos":"203", + "Video/Movie clips":"204", + "Video/TV shows":"205", + "Video/Handheld":"206", + "Video/HD - Movies":"207", + "Video/HD - TV shows":"208", + "Video/3D":"209", + "Video/Other":"299", + "Applications":"300", + "Applications/Windows":"301", + "Applications/Mac":"302", + "Applications/UNIX":"303", + "Applications/Handheld":"304", + "Applications/IOS (iPad/iPhone)":"305", + "Applications/Android":"306", + "Applications/Other OS":"399", + "Games":"400", + "Games/PC":"401", + "Games/Mac":"402", + "Games/PSx":"403", + "Games/XBOX360":"404", + "Games/Wii":"405", + "Games/Handheld":"406", + "Games/IOS (iPad/iPhone)":"407", + "Games/Android":"408", + "Games/Other":"499", + "Porn":"500", + "Porn/Movies":"501", + "Porn/Movies DVDR":"502", + "Porn/Pictures":"503", + "Porn/Games":"504", + "Porn/HD - Movies":"505", + "Porn/Movie clips":"506", + "Porn/Other":"599", + "Other":"600", + "Other/E-books":"601", + "Other/Comics":"602", + "Other/Pictures":"603", + "Other/Covers":"604", + "Other/Physibles":"605", + "Other/Other":"699"} - sorts = {"TitleDsc":"1","TitleAsc":"2","DateDsc":"3","DateAsc":"4","SizeDsc":"5","SizeAsc":"6","SeedersDsc":"7","SeedersAsc":"8","LeechersDsc":"9","LeechersAsc":"10","CategoryDsc":"13","CategoryAsc":"14","Default":"99"} + sorts = { + "TitleDsc": "1", "TitleAsc": "2", + "DateDsc": "3", "DateAsc": "4", + "SizeDsc": "5", "SizeAsc": "6", + "SeedersDsc": "7", "SeedersAsc": "8", + "LeechersDsc": "9", "LeechersAsc": "10", + "CategoryDsc": "13", "CategoryAsc": "14", + "Default": "99"} #todo: redo this with html parser instead of regex def remote(args, mirror): @@ -122,7 +203,8 @@ def main(): if pages < 1: raise Exception('') except Exception: - raise Exception("Please provide an integer greater than 0 for the number of pages to fetch.") + raise Exception("Please provide an integer greater than 0" + "for the number of pages to fetch.") if str(args.category) in categories.values(): category = args.category; @@ -130,7 +212,7 @@ def main(): category = categories[args.category] else: category = "0"; - print ("Invalid category ignored", color="WARN") + print("Invalid category ignored", color="WARN") if str(args.sort) in sorts.values(): sort = args.sort; @@ -138,21 +220,20 @@ def main(): sort = sorts[args.sort] else: sort = "99"; - print ("Invalid sort ignored", color="WARN") + print("Invalid sort ignored", color="WARN") # Catch the Ctrl-C exception and exit cleanly try: sizes = [] uploaded = [] identifiers = [] - for page in xrange(pages): - - # + for page in range(pages): if args.browse: path = "/browse/" if(category == "0"): category = '100' - path = '/browse/' + category + '/' + str(page) + '/' + str(sort) + path = '/browse/' + '/'.join(str(i) for i in ( + category, page, sort)) elif len(args.search) == 0: path = "/top/48h" if args.recent else "/top/" if(category == "0"): @@ -160,31 +241,44 @@ def main(): else: path += category else: - path = '/search/' + "+".join(args.search) + '/' + str(page) + '/' + str(sort) + '/' + category + path = '/search/' + '/'.join(str(i) for i in ( + "+".join(args.search), + page, sort, + category)) - request = urllib2.Request(mirror + path) - request.add_header('Accept-encoding', 'gzip') - f = urllib2.urlopen(request) + req = request.Request(mirror + path) + req.add_header('Accept-encoding', 'gzip') + f = request.urlopen(req) if f.info().get('Content-Encoding') == 'gzip': - buf = StringIO(f.read()) - f = gzip.GzipFile(fileobj=buf) - res = f.read() - found = re.findall(""""(magnet\:\?xt=[^"]*)|