From 12d4d58f1a9a83c38b9036d488146a20b6848261 Mon Sep 17 00:00:00 2001 From: Brad Horrocks Date: Wed, 16 Apr 2014 12:24:11 -0600 Subject: [PATCH] Added support to save magnet URLs to file You have to create a config file for this to work. $ cat .config/pirate-get/pirate.cfg [SaveToFile] enabled = true directory = ~/Dropbox/pirate-get/ --- pirate-get.py | 52 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/pirate-get.py b/pirate-get.py index 370f5b9..46efa92 100755 --- a/pirate-get.py +++ b/pirate-get.py @@ -6,6 +6,9 @@ import urllib import urllib2 import re import os +import ConfigParser +import string +import random from HTMLParser import HTMLParser import argparse from pprint import pprint @@ -47,6 +50,18 @@ class MyHTMLParser(HTMLParser): def main(): + + # new ConfigParser + config = ConfigParser.ConfigParser() + + # default options so we dont die later + config.add_section('SaveToFile') + config.set('SaveToFile', 'enabled', False) + config.set('SaveToFile', 'directory', '~/Dropbox/pirate-get/') + + # load user options, to override default ones + config.read([os.path.expanduser('~/.config/pirate-get/pirate.cfg')]) + 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('-t',dest='transmission',action='store_true', help="call transmission-remote to start the download", default=False) @@ -218,15 +233,34 @@ def main(): except Exception: choices = () - for choice in choices: - choice = int(choice) - url = mags[choice][0] - print(url) - if args.transmission: - os.system("""transmission-remote --add "%s" """ % (url)) - os.system("transmission-remote -l") - else: - webbrowser.open(url) + if config.get('SaveToFile', 'enabled'): + # Save to file is enabled + fileName = os.path.expanduser(config.get('SaveToFile', 'directory')) + id_generator() + '.magnet' + print ("Saving to File: " + fileName) + f = open(fileName, 'w') + for choice in choices: + choice = int(choice) + url = mags[choice][0] + f.write(url + '\n') + + f.close() + + else: + # use transmission as default + for choice in choices: + choice = int(choice) + url = mags[choice][0] + print(url) + if args.transmission: + os.system("""transmission-remote --add "%s" """ % (url)) + os.system("transmission-remote -l") + else: + webbrowser.open(url) + + + +def id_generator(size=6, chars=string.ascii_uppercase + string.digits): + return ''.join(random.choice(chars) for _ in range(size)) if __name__ == "__main__": main()