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

146 lines
5.0 KiB
Python
Raw Normal View History

2015-08-30 03:28:43 +02:00
import builtins
import re
import os
import gzip
import urllib.parse as parse
import urllib.request as request
2015-09-04 08:29:20 +02:00
import shutil
2015-08-30 03:28:43 +02:00
from io import BytesIO
import colorama
2015-09-20 09:07:23 +02:00
import veryprettytable
2015-08-30 03:28:43 +02:00
import pirate.data
def print(*args, **kwargs):
if kwargs.get('color', False) and pirate.data.colored_output:
colorama.init()
color_dict = {
'default': '',
'header': colorama.Back.BLACK + colorama.Fore.WHITE,
'alt': colorama.Fore.YELLOW,
'zebra_0': '',
'zebra_1': colorama.Fore.BLUE,
'WARN': colorama.Fore.MAGENTA,
'ERROR': colorama.Fore.RED}
c = color_dict[kwargs.pop('color')]
args = (c + args[0],) + args[1:] + (colorama.Style.RESET_ALL,)
kwargs.pop('color', None)
return builtins.print(*args, **kwargs)
else:
kwargs.pop('color', None)
return builtins.print(*args, **kwargs)
# TODO: extract the name from the search results instead of from the magnet link when possible
2015-09-04 07:18:38 +02:00
def search_results(results, local=None):
2015-09-04 08:29:20 +02:00
columns = shutil.get_terminal_size((80, 20)).columns
2015-09-20 09:07:23 +02:00
even = True
2015-08-30 03:28:43 +02:00
if local:
2015-09-20 09:07:23 +02:00
table = veryprettytable.VeryPrettyTable(['LINK', 'NAME'])
2015-08-30 03:28:43 +02:00
else:
2015-09-20 09:07:23 +02:00
table = veryprettytable.VeryPrettyTable(['LINK', 'SEED', 'LEECH', 'RATIO', 'SIZE', '', 'UPLOAD', 'NAME'])
table.align['NAME'] = 'l'
table.align['SEED'] = 'r'
table.align['LEECH'] = 'r'
table.align['RATIO'] = 'r'
table.align['SIZE'] = 'r'
table.align['UPLOAD'] = 'l'
table.max_width = columns
table.border = False
table.padding_width = 1
2015-08-30 03:28:43 +02:00
2015-09-04 07:18:38 +02:00
for n, result in enumerate(results):
2015-08-30 03:28:43 +02:00
2015-09-04 07:18:38 +02:00
name = re.search(r'dn=([^\&]*)', result['magnet'])
torrent_name = parse.unquote_plus(name.group(1))
2015-08-30 03:28:43 +02:00
if local:
content = [n, torrent_name[:columns - 7]]
2015-08-30 03:28:43 +02:00
else:
2015-09-04 07:18:38 +02:00
no_seeders = int(result['seeds'])
no_leechers = int(result['leechers'])
if result['size'] != []:
size = float(result['size'][0])
unit = result['size'][1]
else:
size = 0
unit = '???'
date = result['uploaded']
2015-08-30 03:28:43 +02:00
# compute the S/L ratio (Higher is better)
try:
ratio = no_seeders / no_leechers
except ZeroDivisionError:
ratio = float('inf')
2015-09-20 09:07:23 +02:00
content = [n, no_seeders, no_leechers, '{:.1f}'.format(ratio),
'{:.1f}'.format(size), unit, date, torrent_name[:columns - 53]]
if even:
table.add_row(content)
else:
table.add_row(content, fore_color='blue')
2015-08-30 03:28:43 +02:00
2015-09-20 09:07:23 +02:00
# Alternate between colors
even = not even
print(table)
2015-08-30 03:28:43 +02:00
2015-09-04 07:18:38 +02:00
def descriptions(chosen_links, results, site):
2015-08-30 03:28:43 +02:00
for link in chosen_links:
2015-09-04 07:18:38 +02:00
path = '/torrent/%s/' % results[link]['id']
2015-08-30 04:00:06 +02:00
req = request.Request(site + path, headers=pirate.data.default_headers)
2015-08-30 03:28:43 +02:00
req.add_header('Accept-encoding', 'gzip')
2015-08-30 04:00:06 +02:00
f = request.urlopen(req, timeout=pirate.data.default_timeout)
2015-08-30 03:28:43 +02:00
if f.info().get('Content-Encoding') == 'gzip':
f = gzip.GzipFile(fileobj=BytesIO(f.read()))
res = f.read().decode('utf-8')
2015-09-04 07:18:38 +02:00
name = re.search(r'dn=([^\&]*)', results[link]['magnet'])
2015-08-30 03:28:43 +02:00
torrent_name = parse.unquote(name.group(1)).replace('+', ' ')
desc = re.search(r'<div class="nfo">\s*<pre>(.+?)(?=</pre>)',
res, re.DOTALL).group(1)
# Replace HTML links with markdown style versions
desc = re.sub(r'<a href="\s*([^"]+?)\s*"[^>]*>(\s*)([^<]+?)(\s*'
r')</a>', r'\2[\3](\1)\4', desc)
print('Description for "%s":' % torrent_name, color='zebra_1')
print(desc, color='zebra_0')
2015-09-04 07:18:38 +02:00
def file_lists(chosen_links, results, site):
2015-08-30 03:28:43 +02:00
for link in chosen_links:
path = '/ajax_details_filelist.php'
2015-09-04 07:18:38 +02:00
query = '?id=' + results[link]['id']
2015-08-30 04:00:06 +02:00
req = request.Request(site + path + query,
headers=pirate.data.default_headers)
2015-08-30 03:28:43 +02:00
req.add_header('Accept-encoding', 'gzip')
2015-08-30 04:00:06 +02:00
f = request.urlopen(req, timeout=pirate.data.default_timeout)
2015-08-30 03:28:43 +02:00
if f.info().get('Content-Encoding') == 'gzip':
f = gzip.GzipFile(fileobj=BytesIO(f.read()))
2015-09-04 07:18:38 +02:00
# TODO: proper html decoding/parsing
2015-08-30 03:28:43 +02:00
res = f.read().decode('utf-8').replace('&nbsp;', ' ')
2015-09-04 07:18:38 +02:00
if 'File list not available.' in res:
print('File list not available.')
return
2015-08-30 03:28:43 +02:00
files = re.findall(r'<td align="left">\s*([^<]+?)\s*</td><td ali'
r'gn="right">\s*([^<]+?)\s*</tr>', res)
2015-09-04 07:18:38 +02:00
name = re.search(r'dn=([^\&]*)', results[link]['magnet'])
2015-08-30 03:28:43 +02:00
torrent_name = parse.unquote(name.group(1)).replace('+', ' ')
print('Files in "%s":' % torrent_name, color='zebra_1')
cur_color = 'zebra_0'
for f in files:
print('{0[0]:>11} {0[1]}'.format(f), color=cur_color)
2015-08-30 04:00:42 +02:00
cur_color = 'zebra_0' if (cur_color == 'zebra_1') else 'zebra_1'