1
0
mirror of https://github.com/vikstrous/pirate-get synced 2025-01-10 10:04:21 +01:00

Add option to save .torrent file

This commit is contained in:
Rnhmjoj 2015-03-25 00:08:35 +01:00
parent db45e50508
commit 4ced152b9c

View File

@ -33,7 +33,7 @@ import urllib.request as request
import urllib.parse as parse import urllib.parse as parse
from html.parser import HTMLParser from html.parser import HTMLParser
from urllib.error import URLError from urllib.error import URLError, HTTPError
from socket import timeout from socket import timeout
from io import BytesIO from io import BytesIO
@ -284,6 +284,22 @@ def config_to_load():
return os.path.expanduser('~/.config/pirate-get') return os.path.expanduser('~/.config/pirate-get')
def get_torrent(info_hash):
url = 'http://torcache.net/torrent/{:X}.torrent'
req = request.Request(url.format(info_hash))
req.add_header('Accept-encoding', 'gzip')
torrent = request.urlopen(req, timeout=default_timeout)
if torrent.info().get('Content-Encoding') == 'gzip':
torrent = gzip.GzipFile(fileobj=BytesIO(torrent.read()))
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 # enhanced print output with column titles
def print_search_results(mags, sizes, uploaded): def print_search_results(mags, sizes, uploaded):
columns = int(os.popen('stty size', 'r').read().split()[1]) - 52 columns = int(os.popen('stty size', 'r').read().split()[1]) - 52
@ -369,8 +385,20 @@ def print_file_lists(chosen_links, mags, site, identifiers):
cur_color = 'zebra_0' if (cur_color == 'zebra_1') else 'zebra_1' cur_color = 'zebra_0' if (cur_color == 'zebra_1') else 'zebra_1'
def id_generator(size=6, chars=string.ascii_uppercase + string.digits): def save_torrents(chosen_links, mags, site, identifiers, folder):
return ''.join(random.choice(chars) for _ in range(size)) 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 + '.torrent')
try:
torrent = get_torrent(info_hash)
open(file,'wb').write(torrent)
print('Saved {:X} in {}'.format(info_hash, file))
except HTTPError:
print('There is no cached file for this torrent :(', color='ERROR')
def main(): def main():
@ -523,6 +551,7 @@ def main():
if code == 'h': if code == 'h':
print('Options:', print('Options:',
'<links>: Download selected torrents', '<links>: Download selected torrents',
'[s<links>]: Save torrent file',
'[d<links>]: Get descriptions', '[d<links>]: Get descriptions',
'[f<links>]: Get files', '[f<links>]: Get files',
'[p] Print search results', '[p] Print search results',
@ -536,6 +565,9 @@ def main():
print_file_lists(choices, mags, site, identifiers) print_file_lists(choices, mags, site, identifiers)
elif code == 'p': elif code == 'p':
print_search_results(mags, sizes, uploaded) print_search_results(mags, sizes, uploaded)
elif code == 's':
downloads = config.get('SaveToFile', 'directory')
save_torrents(choices, mags, site, identifiers, downloads)
elif not l: elif not l:
print('No links entered!', color='WARN') print('No links entered!', color='WARN')
else: else: