From 9343dbba34cb67a623653146775dbe3310191ed5 Mon Sep 17 00:00:00 2001 From: Rnhmjoj Date: Thu, 26 Mar 2015 23:42:03 +0100 Subject: [PATCH] Save magnets in separated files --- README.md | 2 +- pirate-get.py | 28 +++++++++++++--------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 89f95a1..06a72ec 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ These are the default options: ```INI [Save] directory = ~/downloads/pirate-get ; directory where to save files -magnets = false ; save every selected torrent as a magnet in a single file +magnets = false ; save each selected magnet link in a .magnet file torrents = false ; save each selected torrent in a .torrent file [LocalDB] diff --git a/pirate-get.py b/pirate-get.py index da99e67..d8f655f 100755 --- a/pirate-get.py +++ b/pirate-get.py @@ -19,7 +19,6 @@ import os import sys -import random import re import string import gzip @@ -326,10 +325,6 @@ def get_torrent(info_hash): return torrent.read() -def id_generator(size=6, chars=string.ascii_uppercase + string.digits): - return ''.join(random.choice(chars) for _ in range(size)) - - # enhanced print output with column titles def print_search_results(mags, sizes, uploaded): columns = int(os.popen('stty size', 'r').read().split()[1]) - 52 @@ -432,13 +427,16 @@ def save_torrents(chosen_links, mags, folder): def save_magnets(chosen_links, mags, folder): - filename = os.path.join(folder, id_generator() + '.magnets') - print('Saving magnets to', filename) + for link in chosen_links: + magnet = mags[int(link)][0] + name = re.search(r'dn=([^\&]*)', magnet) + torrent_name = parse.unquote(name.group(1)).replace('+', ' ') + info_hash = int(re.search(r'btih:([a-f0-9]{40})', magnet).group(1), 16) + file = os.path.join(folder, torrent_name + '.magnet') - with open(filename, 'w') as f: - for link in chosen_links: - url = mags[int(link)][0] - f.write(url + '\n') + print('Saved {:X} in {}'.format(info_hash, file)) + with open(file, 'w') as f: + f.write(magnet + '\n') def main(): @@ -483,7 +481,7 @@ def main(): help='open magnets with transmission-remote') parser.add_argument('-M', '--save-magnets', action='store_true', default=False, - help='save magnets links into a file') + help='save magnets links as files') parser.add_argument('-T', '--save-torrents', action='store_true', default=False, help='save torrent files') @@ -598,7 +596,7 @@ def main(): if code == 'h': print('Options:', ': Download selected torrents', - '[m]: Save magnets in a file', + '[m]: Save magnets as files', '[t]: Save .torrent files', '[d]: Get descriptions', '[f]: Get files', @@ -648,13 +646,13 @@ def main(): if args.transmission or config.getboolean('Misc', 'transmission'): os.system('transmission-remote --add "%s" ' % url) os.system('transmission-remote -l') - + elif args.command or config.get('Misc', 'openCommand'): command = config.get('Misc', 'openCommand') if args.command: command = args.command os.system(command % url) - + else: webbrowser.open(url)