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

429 lines
15 KiB
Python
Raw Normal View History

import sys
2015-08-30 03:28:43 +02:00
import re
import os
import argparse
import subprocess
import configparser
2015-08-30 04:00:42 +02:00
import socket
2015-08-30 03:28:43 +02:00
import urllib.request as request
import urllib.error
2015-09-21 02:22:31 +02:00
import builtins
2015-09-20 07:27:05 +02:00
from collections import OrderedDict
2015-09-15 05:21:13 +02:00
2015-08-30 03:28:43 +02:00
import webbrowser
import pirate.data
import pirate.torrent
import pirate.local
from os.path import expanduser, expandvars
from pirate.print import Printer
2015-08-30 03:28:43 +02:00
2015-09-15 05:21:13 +02:00
def parse_config_file(text):
2015-11-05 07:42:53 +01:00
config = configparser.RawConfigParser()
2015-08-30 03:28:43 +02:00
# default options
config.add_section('Save')
config.set('Save', 'magnets', 'false')
config.set('Save', 'torrents', 'false')
config.set('Save', 'directory', os.getcwd())
config.add_section('LocalDB')
config.set('LocalDB', 'enabled', 'false')
config.set('LocalDB', 'path', expanduser('~/downloads/pirate-get/db'))
2015-08-30 04:00:42 +02:00
config.add_section('Misc')
2016-07-07 03:51:13 +02:00
# TODO: try to use configparser.BasicInterpolation
# for interpolating in the command
2015-08-30 03:28:43 +02:00
config.set('Misc', 'openCommand', '')
config.set('Misc', 'transmission', 'false')
config.set('Misc', 'colors', 'true')
2016-09-03 14:53:00 +02:00
config.set('Misc', 'mirror', pirate.data.default_mirror)
2015-08-30 03:28:43 +02:00
2015-09-15 05:21:13 +02:00
config.read_string(text)
2015-08-30 03:28:43 +02:00
# expand env variables
directory = expanduser(expandvars(config.get('Save', 'Directory')))
path = expanduser(expandvars(config.get('LocalDB', 'path')))
config.set('Save', 'Directory', directory)
config.set('LocalDB', 'path', path)
return config
2015-09-17 08:15:27 +02:00
2015-09-15 05:21:13 +02:00
def load_config():
# user-defined config files
2016-09-03 14:53:00 +02:00
config_home = os.getenv('XDG_CONFIG_HOME', '~/.config')
config = expanduser(os.path.join(config_home, 'pirate-get'))
2015-09-15 05:21:13 +02:00
# read config file
2016-09-03 14:53:00 +02:00
if os.path.isfile(config):
with open(config) as f:
2015-09-15 05:21:13 +02:00
return parse_config_file(f.read())
return parse_config_file("")
2015-08-30 03:28:43 +02:00
def parse_cmd(cmd, url):
cmd_args_regex = r'''(('[^']*'|"[^"]*"|(\\\s|[^\s])+)+ *)'''
ret = re.findall(cmd_args_regex, cmd)
ret = [i[0].strip().replace('%s', url) for i in ret]
ret_no_quotes = []
for item in ret:
2015-08-30 04:00:42 +02:00
if ((item[0] == "'" and item[-1] == "'") or
(item[0] == '"' and item[-1] == '"')):
2015-08-30 03:28:43 +02:00
ret_no_quotes.append(item[1:-1])
else:
ret_no_quotes.append(item)
return ret_no_quotes
2015-09-04 08:00:40 +02:00
def parse_torrent_command(l):
# Very permissive handling
# Check for any occurances or d, f, p, t, m, or q
cmd_code_match = re.search(r'([hdfpmtq])', l,
flags=re.IGNORECASE)
if cmd_code_match:
code = cmd_code_match.group(0).lower()
else:
code = None
# Clean up command codes
# Substitute multiple consecutive spaces/commas for single
# comma remove anything that isn't an integer or comma.
# Turn into list
l = re.sub(r'^[hdfp, ]*|[hdfp, ]*$', '', l)
l = re.sub('[ ,]+', ',', l)
l = re.sub('[^0-9,-]', '', l)
parsed_input = l.split(',')
# expand ranges
choices = []
# loop will generate a list of lists
for elem in parsed_input:
left, sep, right = elem.partition('-')
if right:
choices.append(list(range(int(left), int(right) + 1)))
elif left != '':
choices.append([int(left)])
# flatten list
choices = sum(choices, [])
# the current code stores the choices as strings
# instead of ints. not sure if necessary
choices = [elem for elem in choices]
return code, choices
2015-09-15 05:21:13 +02:00
def parse_args(args_in):
2015-08-30 03:28:43 +02:00
parser = argparse.ArgumentParser(
2015-08-30 04:00:42 +02:00
description='finds and downloads torrents from the Pirate Bay')
2015-08-30 03:28:43 +02:00
parser.add_argument('-b', dest='browse',
action='store_true',
help='display in Browse mode')
parser.add_argument('search', metavar='search',
nargs='*', help='term to search for')
parser.add_argument('-c', dest='category', metavar='category',
help='specify a category to search', default='All')
parser.add_argument('-s', dest='sort', metavar='sort',
help='specify a sort option', default='SeedersDsc')
parser.add_argument('-R', dest='recent', action='store_true',
help='torrents uploaded in the last 48hours.'
2015-08-30 04:00:42 +02:00
'*ignored in searches*')
2015-08-30 03:28:43 +02:00
parser.add_argument('-l', dest='list_categories',
action='store_true',
help='list categories')
parser.add_argument('--list_sorts', dest='list_sorts',
action='store_true',
help='list Sortable Types')
parser.add_argument('-L', '--local', dest='database',
help='an xml file containing the Pirate Bay database')
2015-09-04 05:25:24 +02:00
parser.add_argument('-p', dest='pages', default=1, type=int,
2015-08-30 03:28:43 +02:00
help='the number of pages to fetch '
"(doesn't work with --local)")
parser.add_argument('-0', dest='first',
action='store_true',
help='choose the top result')
parser.add_argument('-a', '--download-all',
action='store_true',
help='download all results')
parser.add_argument('-t', '--transmission',
action='store_true',
help='open magnets with transmission-remote')
parser.add_argument('-P', '--port', dest='port',
help='transmission-remote rpc port. default is 9091')
parser.add_argument('-C', '--custom', dest='command',
help='open magnets with a custom command'
' (%%s will be replaced with the url)')
parser.add_argument('-M', '--save-magnets',
action='store_true',
help='save magnets links as files')
parser.add_argument('-T', '--save-torrents',
action='store_true',
help='save torrent files')
parser.add_argument('-S', '--save-directory',
type=str, metavar='DIRECTORY',
help='directory where to save downloaded files'
' (if none is given $PWD will be used)')
parser.add_argument('--disable-colors', dest='color',
action='store_false',
help='disable colored output')
2016-09-03 14:53:00 +02:00
parser.add_argument('-m', '--mirror',
type=str, nargs='+',
help='the pirate bay mirror(s) to use')
parser.add_argument('-v', '--version',
action='store_true',
help='print pirate-get version number')
2015-09-15 05:21:13 +02:00
args = parser.parse_args(args_in)
2015-08-30 03:28:43 +02:00
2015-09-17 08:15:27 +02:00
return args
def combine_configs(config, args):
# figure out the action - browse, search, top, etc.
if args.browse:
2015-09-17 08:15:27 +02:00
args.action = 'browse'
elif args.recent:
2015-09-17 08:15:27 +02:00
args.action = 'recent'
elif args.list_categories:
args.action = 'list_categories'
elif args.list_sorts:
args.action = 'list_sorts'
elif len(args.search) == 0:
args.action = 'top'
else:
2015-09-17 08:15:27 +02:00
args.action = 'search'
2015-09-15 05:21:13 +02:00
2015-09-17 08:15:27 +02:00
args.source = 'tpb'
if args.database or config.getboolean('LocalDB', 'enabled'):
args.source = 'local_tpb'
2015-09-15 05:21:13 +02:00
2015-09-17 08:15:27 +02:00
if not args.database:
args.database = config.get('LocalDB', 'path')
2015-09-15 05:21:13 +02:00
2015-09-17 08:15:27 +02:00
if not args.color or not config.getboolean('Misc', 'colors'):
# TODO: consider how this can be moved to the args
2015-08-30 03:28:43 +02:00
pirate.data.colored_output = False
2015-09-17 08:15:27 +02:00
if not args.save_directory:
args.save_directory = config.get('Save', 'directory')
2015-08-30 03:28:43 +02:00
2016-09-03 14:53:00 +02:00
if not args.mirror:
args.mirror = config.get('Misc', 'mirror').split()
2015-09-17 08:15:27 +02:00
args.transmission_command = ['transmission-remote']
2015-08-30 03:28:43 +02:00
if args.port:
2015-09-17 08:15:27 +02:00
args.transmission_command.append(args.port)
2015-08-30 03:28:43 +02:00
2015-09-17 08:15:27 +02:00
args.output = 'browser_open'
2015-08-30 03:28:43 +02:00
if args.transmission or config.getboolean('Misc', 'transmission'):
2015-09-17 08:15:27 +02:00
args.output = 'transmission'
elif args.save_magnets or config.getboolean('Save', 'magnets'):
args.output = 'save_magnet_files'
elif args.save_torrents or config.getboolean('Save', 'torrents'):
args.output = 'save_torrent_files'
elif args.command or config.get('Misc', 'openCommand'):
args.output = 'open_command'
args.open_command = args.command
if not args.open_command:
args.open_command = config.get('Misc', 'openCommand')
return args
2016-09-03 14:53:00 +02:00
def connect_mirror(mirror, printer, args):
2016-07-07 03:30:34 +02:00
try:
printer.print('Trying', mirror, end='... ')
results = pirate.torrent.remote(
printer=printer,
2016-09-03 14:53:00 +02:00
pages=args.pages,
category=pirate.torrent.parse_category(printer, args.category),
sort=pirate.torrent.parse_sort(printer, args.sort),
mode=args.action,
terms=args.search,
2016-07-07 03:30:34 +02:00
mirror=mirror)
except (urllib.error.URLError, socket.timeout, IOError, ValueError):
printer.print('Failed', color='WARN')
2016-08-31 21:48:25 +02:00
return None
2016-07-07 03:30:34 +02:00
else:
printer.print('Ok', color='alt')
return results, mirror
2016-09-03 14:53:00 +02:00
def search_mirrors(printer, args):
# try default or user mirrors
for mirror in args.mirror:
result = connect_mirror(mirror, printer, args)
if result:
return result
2016-07-07 03:30:34 +02:00
# download mirror list
try:
2016-08-31 21:48:25 +02:00
req = request.Request(pirate.data.mirror_list,
2016-07-07 03:30:34 +02:00
headers=pirate.data.default_headers)
f = request.urlopen(req, timeout=pirate.data.default_timeout)
2016-08-31 21:48:25 +02:00
except urllib.error.URLError as e:
raise IOError('Could not fetch mirrors', e.reason)
2016-07-07 03:30:34 +02:00
if f.getcode() != 200:
2016-08-31 21:48:25 +02:00
raise IOError('The proxy bay responded with an error',
f.read().decode('utf-8'))
2016-07-07 03:30:34 +02:00
mirrors = [i.decode('utf-8').strip() for i in f.readlines()][3:]
# try mirrors
for mirror in mirrors:
if mirror in pirate.data.blacklist:
continue
2016-09-03 14:53:00 +02:00
result = connect_mirror(mirror, printer, args)
2016-07-07 03:30:34 +02:00
if result:
return result
2015-09-17 08:15:27 +02:00
else:
2016-08-31 21:48:25 +02:00
raise IOError('No more available mirrors')
2015-09-17 08:15:27 +02:00
2015-09-21 00:32:22 +02:00
def pirate_main(args):
printer = Printer(args.color)
# print version
if args.version:
printer.print('pirate-get, version {}'.format(pirate.data.version))
sys.exit(0)
2015-09-17 08:15:27 +02:00
# check it transmission is running
if args.transmission:
ret = subprocess.call(args.transmission_command + ['-l'],
2015-08-30 03:28:43 +02:00
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
if ret != 0:
printer.print('Transmission is not running.')
2015-09-17 08:15:27 +02:00
sys.exit(1)
# non-torrent fetching actions
2015-08-30 03:28:43 +02:00
2015-09-17 08:15:27 +02:00
if args.action == 'list_categories':
2015-08-30 03:28:43 +02:00
cur_color = 'zebra_0'
for key, value in sorted(pirate.data.categories.items()):
cur_color = 'zebra_0' if cur_color == 'zebra_1' else 'zebra_1'
printer.print(str(value), '\t', key, sep='', color=cur_color)
2015-08-30 03:28:43 +02:00
return
2015-09-17 08:15:27 +02:00
if args.action == 'list_sorts':
2015-08-30 03:28:43 +02:00
cur_color = 'zebra_0'
2015-08-30 04:00:06 +02:00
for key, value in sorted(pirate.data.sorts.items()):
2015-08-30 03:28:43 +02:00
cur_color = 'zebra_0' if cur_color == 'zebra_1' else 'zebra_1'
printer.print(str(value), '\t', key, sep='', color=cur_color)
2015-08-30 03:28:43 +02:00
return
2015-09-17 08:15:27 +02:00
# fetch torrents
2015-08-30 03:28:43 +02:00
2015-09-17 08:15:27 +02:00
if args.source == 'local_tpb':
results = pirate.local.search(args.database, args.search)
elif args.source == 'tpb':
2016-08-31 21:48:25 +02:00
try:
2016-09-03 14:53:00 +02:00
results, site = search_mirrors(printer, args)
2016-08-31 21:48:25 +02:00
except IOError as e:
printer.print(e.args[0] + ' :( ', color='ERROR')
if len(e.args) > 1:
printer.print(e.args[1])
sys.exit(1)
2015-08-30 03:28:43 +02:00
2015-09-04 07:18:38 +02:00
if len(results) == 0:
printer.print('No results')
2015-08-30 03:28:43 +02:00
return
printer.search_results(results, local=args.source == 'local_tpb')
2015-08-30 03:28:43 +02:00
2015-09-17 08:15:27 +02:00
# number of results to pick
2015-08-30 03:28:43 +02:00
if args.first:
printer.print('Choosing first result')
2015-08-30 03:28:43 +02:00
choices = [0]
elif args.download_all:
printer.print('Downloading all results')
2015-09-04 07:18:38 +02:00
choices = range(len(results))
2015-08-30 03:28:43 +02:00
else:
2015-09-17 08:15:27 +02:00
# interactive loop for per-torrent actions
2015-08-30 03:28:43 +02:00
while True:
printer.print("\nSelect links (Type 'h' for more options"
2016-07-07 03:51:13 +02:00
", 'q' to quit)", end='\b', color='alt')
2015-08-30 03:28:43 +02:00
try:
2015-09-21 02:22:31 +02:00
l = builtins.input(': ')
2015-09-17 08:15:27 +02:00
except (KeyboardInterrupt, EOFError):
printer.print('\nCancelled.')
2015-08-30 03:28:43 +02:00
return
try:
2015-09-04 08:00:40 +02:00
code, choices = parse_torrent_command(l)
2015-08-30 03:28:43 +02:00
# Act on option, if supplied
printer.print('')
2015-08-30 03:28:43 +02:00
if code == 'h':
printer.print('Options:',
2016-07-07 03:51:13 +02:00
'<links>: Download selected torrents',
'[m<links>]: Save magnets as files',
'[t<links>]: Save .torrent files',
'[d<links>]: Get descriptions',
'[f<links>]: Get files',
'[p] Print search results',
'[q] Quit', sep='\n')
2015-08-30 03:28:43 +02:00
elif code == 'q':
printer.print('Bye.', color='alt')
2015-08-30 03:28:43 +02:00
return
elif code == 'd':
printer.descriptions(choices, results, site)
2015-08-30 03:28:43 +02:00
elif code == 'f':
printer.file_lists(choices, results, site)
2015-08-30 03:28:43 +02:00
elif code == 'p':
printer.search_results(results)
2015-08-30 03:28:43 +02:00
elif code == 'm':
2016-07-07 03:51:13 +02:00
pirate.torrent.save_magnets(printer, choices, results,
args.save_directory)
2015-08-30 03:28:43 +02:00
elif code == 't':
2016-07-07 03:51:13 +02:00
pirate.torrent.save_torrents(printer, choices, results,
args.save_directory)
2015-08-30 03:28:43 +02:00
elif not l:
printer.print('No links entered!', color='WARN')
2015-08-30 03:28:43 +02:00
else:
break
except Exception as e:
printer.print('Exception:', e, color='ERROR')
2015-09-17 08:15:27 +02:00
return
2015-08-30 03:28:43 +02:00
2015-09-17 08:15:27 +02:00
# output
2015-08-30 03:28:43 +02:00
2015-09-17 08:15:27 +02:00
if args.output == 'save_magnet_files':
printer.print('Saving selected magnets...')
2015-09-17 08:15:27 +02:00
pirate.torrent.save_magnets(choices, results, args.save_directory)
return
2015-08-30 03:28:43 +02:00
2015-09-17 08:15:27 +02:00
if args.output == 'save_torrent_files':
printer.print('Saving selected torrents...')
2015-09-17 08:15:27 +02:00
pirate.torrent.save_torrents(choices, results, args.save_directory)
2015-08-30 03:28:43 +02:00
return
for choice in choices:
2015-09-17 08:15:27 +02:00
url = results[choice]['magnet']
2015-08-30 03:28:43 +02:00
2015-09-17 08:15:27 +02:00
if args.output == 'transmission':
subprocess.call(args.transmission_command + ['--add', url])
elif args.output == 'open_command':
2015-11-05 07:42:53 +01:00
cmd = parse_cmd(args.open_command, url)
printer.print(" ".join(cmd))
subprocess.call(cmd)
2015-09-17 08:15:27 +02:00
elif args.output == 'browser_open':
2015-08-30 03:28:43 +02:00
webbrowser.open(url)
2015-09-17 08:15:27 +02:00
if args.output == 'transmission':
subprocess.call(args.transmission_command + ['-l'])
2015-08-30 03:28:43 +02:00
2015-09-21 00:32:22 +02:00
def main():
args = combine_configs(load_config(), parse_args(sys.argv[1:]))
pirate_main(args)
2015-08-30 03:28:43 +02:00
if __name__ == '__main__':
main()