1
0
mirror of https://github.com/vikstrous/pirate-get synced 2025-01-10 10:04:21 +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.request as request
import urllib.error import urllib.error
import builtins import builtins
import json
import webbrowser import webbrowser
import pirate.data import pirate.data
@ -138,8 +138,8 @@ def parse_args(args_in):
parser.add_argument('-l', dest='list_categories', parser.add_argument('-l', dest='list_categories',
action='store_true', action='store_true',
help='list categories') help='list categories')
parser.add_argument('--list_sorts', dest='list_sorts', parser.add_argument('--list-sorts', '--list_sorts',
action='store_true', dest='list_sorts', action='store_true',
help='list Sortable Types') help='list Sortable Types')
parser.add_argument('-L', '--local', dest='database', parser.add_argument('-L', '--local', dest='database',
help='a csv file containing the Pirate Bay database ' help='a csv file containing the Pirate Bay database '
@ -185,6 +185,9 @@ def parse_args(args_in):
parser.add_argument('-v', '--version', parser.add_argument('-v', '--version',
action='store_true', action='store_true',
help='print pirate-get version number') 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) args = parser.parse_args(args_in)
return args return args
@ -369,6 +372,10 @@ def pirate_main(args):
printer.print('No results') printer.print('No results')
return return
if args.json:
print(json.dumps(results))
return
else:
printer.search_results(results, local=args.source == 'local_tpb') printer.search_results(results, local=args.source == 'local_tpb')
# number of results to pick # number of results to pick

View File

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

View File

@ -2,6 +2,7 @@
import os import os
import unittest import unittest
import json import json
import sys
from unittest.mock import patch, call, MagicMock from unittest.mock import patch, call, MagicMock
from pirate.print import Printer from pirate.print import Printer
@ -64,11 +65,15 @@ class TestPrint(unittest.TestCase):
printer = Printer(False) printer = Printer(False)
with patch('pirate.print.builtins.print') as mock_print: with patch('pirate.print.builtins.print') as mock_print:
printer.print('abc', color='zebra_1') 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) printer = Printer(True)
with patch('pirate.print.builtins.print') as mock_print: with patch('pirate.print.builtins.print') as mock_print:
printer.print('abc', color='zebra_1') 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): def test_print_results_local2(self):
class MockTable: class MockTable: