1
0
mirror of https://github.com/vikstrous/pirate-get synced 2025-01-09 09:59:51 +01:00

implement JSON output for results

Note: pirate-get now outputs to stderr by default.
This makes piping pirate-get (for example to jq) easier as
a normal pipe will selectively pick only the JSON, not the UI.
This commit is contained in:
Michele Guerini Rocco 2020-05-22 10:35:42 +02:00
parent 03c5a396e1
commit 496b0f09b0
Signed by: rnhmjoj
GPG Key ID: BFBAF4C975F76450
3 changed files with 21 additions and 8 deletions

View File

@ -8,7 +8,7 @@ import socket
import urllib.request as request
import urllib.error
import builtins
import json
import webbrowser
import pirate.data
@ -138,8 +138,8 @@ def parse_args(args_in):
parser.add_argument('-l', dest='list_categories',
action='store_true',
help='list categories')
parser.add_argument('--list_sorts', dest='list_sorts',
action='store_true',
parser.add_argument('--list-sorts', '--list_sorts',
dest='list_sorts', action='store_true',
help='list Sortable Types')
parser.add_argument('-L', '--local', dest='database',
help='a csv file containing the Pirate Bay database '
@ -185,6 +185,9 @@ def parse_args(args_in):
parser.add_argument('-v', '--version',
action='store_true',
help='print pirate-get version number')
parser.add_argument('-j', '--json',
action='store_true',
help='print results in JSON format to stdout')
args = parser.parse_args(args_in)
return args
@ -369,7 +372,11 @@ def pirate_main(args):
printer.print('No results')
return
printer.search_results(results, local=args.source == 'local_tpb')
if args.json:
print(json.dumps(results))
return
else:
printer.search_results(results, local=args.source == 'local_tpb')
# number of results to pick
if args.first:

View File

@ -4,6 +4,7 @@ import gzip
import urllib.request as request
import shutil
import json
import sys
import pirate.data
import pirate.torrent
@ -33,10 +34,10 @@ class Printer:
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)
return builtins.print(*args, file=sys.stderr, **kwargs)
else:
kwargs.pop('color', None)
return builtins.print(*args, **kwargs)
return builtins.print(*args, file=sys.stderr, **kwargs)
# TODO: extract the name from the search results
# instead of from the magnet link when possible

View File

@ -2,6 +2,7 @@
import os
import unittest
import json
import sys
from unittest.mock import patch, call, MagicMock
from pirate.print import Printer
@ -64,11 +65,15 @@ class TestPrint(unittest.TestCase):
printer = Printer(False)
with patch('pirate.print.builtins.print') as mock_print:
printer.print('abc', color='zebra_1')
mock_print.assert_called_once_with('abc')
mock_print.assert_called_once_with(
'abc',
file=sys.stderr)
printer = Printer(True)
with patch('pirate.print.builtins.print') as mock_print:
printer.print('abc', color='zebra_1')
mock_print.assert_called_once_with('\x1b[34mabc', '\x1b[0m')
mock_print.assert_called_once_with(
'\x1b[34mabc', '\x1b[0m',
file=sys.stderr)
def test_print_results_local2(self):
class MockTable: