From ccb5bfd11df2f6f561790b361796bec6e7a7f09b Mon Sep 17 00:00:00 2001 From: Szero Date: Sat, 13 May 2017 16:44:22 +0200 Subject: [PATCH 1/4] unquote filename from magnet on % symbol --- Magnet2Torrent.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Magnet2Torrent.py b/Magnet2Torrent.py index dcfc00d..c0009bd 100755 --- a/Magnet2Torrent.py +++ b/Magnet2Torrent.py @@ -166,7 +166,7 @@ def main(): # in a magnet link it is between '&dn' and '&tr' if output_name is None: output_name = magnet.split('&dn=')[1].split('&tr')[0] - if '+' in output_name: + if '%' in output_name: output_name = unquote(output_name) output_name += '.torrent' From d9ff0a0c690900a7aa5e5214c6b939f70cb09933 Mon Sep 17 00:00:00 2001 From: Szero Date: Sat, 13 May 2017 20:03:55 +0200 Subject: [PATCH 2/4] get filename from torrent handle if magnet doesn't have dn field --- Magnet2Torrent.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Magnet2Torrent.py b/Magnet2Torrent.py index c0009bd..8259ff2 100755 --- a/Magnet2Torrent.py +++ b/Magnet2Torrent.py @@ -109,6 +109,8 @@ def run(self): torinfo.name() + ".torrent")) elif pt.isdir(pt.dirname(pt.abspath(self.output_name))): output = pt.abspath(self.output_name) + else: + output = pt.abspath(torinfo.name() + ".torrent") print("Saving torrent file here : " + output + " ...") with open(output, "wb") as outfile: @@ -165,13 +167,16 @@ def main(): # guess the name if output name is not given. # in a magnet link it is between '&dn' and '&tr' if output_name is None: - output_name = magnet.split('&dn=')[1].split('&tr')[0] - if '%' in output_name: - output_name = unquote(output_name) - output_name += '.torrent' + try: + output_name = magnet.split('&dn=')[1].split('&tr')[0] + if '%' in output_name: + output_name = unquote(output_name) + output_name += '.torrent' + except IndexError: + pass # return if user wants to skip existing file. - if pt.isfile(output_name) and args.skip_file: + if output_name is not None and pt.isfile(output_name) and args.skip_file: print('File [{}] already exists.'.format(output_name)) # still open file if file already exists. if args.open_file: @@ -179,7 +184,7 @@ def main(): return # create fullname if file exists. - if pt.isfile(output_name) and not args.rewrite_file: + if output_name is not None and pt.isfile(output_name) and not args.rewrite_file: new_output_name = output_name counter = 1 while pt.isfile(new_output_name): From bca11021ac4f0ce72adc6fb7ce959f26acabc888 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Sat, 10 Jun 2017 17:44:24 +0800 Subject: [PATCH 3/4] chg: dev: update with patch from @Szero - use python3 interpreter - use unquote_plus instead of unquote --- Magnet2Torrent.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/Magnet2Torrent.py b/Magnet2Torrent.py index 8259ff2..a447fde 100755 --- a/Magnet2Torrent.py +++ b/Magnet2Torrent.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """convert magnet link to torrent file. Created on Apr 19, 2012 @author: dan, Faless @@ -28,9 +28,9 @@ from argparse import ArgumentParser from time import sleep try: - from urllib.parse import unquote + from urllib.parse import unquote_plus except ImportError: - from urllib import unquote + from urllib import unquote_plus import libtorrent as lt @@ -90,7 +90,7 @@ def run(self): soft_limit += 30 wait_time += 1 except KeyboardInterrupt: - print("Aborting...") + print("\nAborting...") self.ses.pause() print("Cleanup dir " + self.tempdir) shutil.rmtree(self.tempdir) @@ -166,14 +166,13 @@ def main(): # guess the name if output name is not given. # in a magnet link it is between '&dn' and '&tr' - if output_name is None: - try: + try: + if output_name is None: output_name = magnet.split('&dn=')[1].split('&tr')[0] - if '%' in output_name: - output_name = unquote(output_name) + output_name = unquote_plus(output_name) output_name += '.torrent' - except IndexError: - pass + except IndexError: + pass # return if user wants to skip existing file. if output_name is not None and pt.isfile(output_name) and args.skip_file: @@ -197,8 +196,8 @@ def main(): output_name = new_output_name # encode magnet link if it's url decoded. - if magnet != unquote(magnet): - magnet = unquote(magnet) + if magnet != unquote_plus(magnet): + magnet = unquote_plus(magnet) conv = Magnet2Torrent(magnet, output_name) conv.run() From 35d8b5a674b652debe33f4b737fd773d6fa37211 Mon Sep 17 00:00:00 2001 From: rachmadaniHaryono Date: Sat, 10 Jun 2017 17:48:04 +0800 Subject: [PATCH 4/4] chg: dev: log when index error. --- Magnet2Torrent.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Magnet2Torrent.py b/Magnet2Torrent.py index a447fde..501e458 100755 --- a/Magnet2Torrent.py +++ b/Magnet2Torrent.py @@ -21,6 +21,7 @@ """ +import logging import os.path as pt import shutil import sys @@ -172,7 +173,7 @@ def main(): output_name = unquote_plus(output_name) output_name += '.torrent' except IndexError: - pass + logging.error('magnet: {}'.format(magnet)) # return if user wants to skip existing file. if output_name is not None and pt.isfile(output_name) and args.skip_file: