From 496b0f09b084191d18d5b5451bb44e1e5f0b8f0a Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Fri, 22 May 2020 10:35:42 +0200 Subject: [PATCH] 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. --- pirate/pirate.py | 15 +++++++++++---- pirate/print.py | 5 +++-- tests/test_print.py | 9 +++++++-- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/pirate/pirate.py b/pirate/pirate.py index ca70c3b..53d44af 100755 --- a/pirate/pirate.py +++ b/pirate/pirate.py @@ -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: diff --git a/pirate/print.py b/pirate/print.py index 8b67c9b..adc3614 100644 --- a/pirate/print.py +++ b/pirate/print.py @@ -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 diff --git a/tests/test_print.py b/tests/test_print.py index c1e00af..ab13920 100755 --- a/tests/test_print.py +++ b/tests/test_print.py @@ -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: