From 87705878726762dd9e80a6e8ad8c06f535d60822 Mon Sep 17 00:00:00 2001 From: Felix Stupp Date: Sun, 26 Jun 2022 15:42:42 +0000 Subject: [PATCH] Add support for different output modes, deprecating --only-url Supported output modes are: - headlines - json - only-url (replacing --only-url) --- scripts/tinytinypy | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/scripts/tinytinypy b/scripts/tinytinypy index 08d4814..1273b53 100644 --- a/scripts/tinytinypy +++ b/scripts/tinytinypy @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import argparse +from enum import Enum from getpass import getpass import json import os @@ -13,14 +14,40 @@ from tinytinypy import Connection APP_NAME = "tinytinypy" # For default config directories + +class OutputMode(Enum): + # enum values + JSON = ('json', False) + ONLY_URL = ('only-url', False) + HEADLINES = ('headlines', False) + # helpers + @classmethod + def get_mode_names(cls): + return [e.mode_name for e in cls] + @classmethod + def parse_mode(cls, mode_name): + for e in cls: + if e.mode_name == mode_name: + return e + def __init__(self, mode_name, requires_content): + self.mode_name = mode_name + self.requires_content = requires_content + def __str__(self): + return self.mode_name + def func_articles(server, args): filtered = {key: value for key, value in args.__dict__.items() if key in ["feed_id", "cat_id", "limit", "skip", "view_mode", "since_id", "include_nested", "order_by"]} - headlines = server.getHeadlines(**filtered) - if args.only_url: + headlines = server.getHeadlines(show_content=args.output_mode.requires_content, **filtered) + if args.output_mode == OutputMode.ONLY_URL: for h in headlines: print(h.url) - else: + elif args.output_mode == OutputMode.HEADLINES: + for h in headlines: + print(h.title) + elif args.output_mode == OutputMode.JSON: print(json.dumps([h.toJson() for h in headlines])) + else: + raise Exception(f'Not implemented output mode "{args.output_mode}"') def configure_parser(): parser = argparse.ArgumentParser(description="Allows access to feeds and articles of a Tiny Tiny RSS instance using the API.") @@ -38,7 +65,7 @@ def configure_parser(): p.add_argument('--limit', dest='limit', type=int, help="Maximum count of articles") p.add_argument('--skip', '--offset', dest='skip', type=int, help="Skip this amount of feeds first") p.add_argument('--view-mode', '--filter', dest='view_mode', choices=['all_articles', 'unread', 'adaptive', 'marked', 'updated'], default='all_articles', help='Only show articles of certain type') - p.add_argument('--only-url', dest='only_url', action='store_true', help="Only output urls of articles instead of full article info") + p.add_argument('--output-mode', dest='output_mode', choices=OutputMode, type=OutputMode.parse_mode, default='json', help='Define how the received articles should be outputed, in most modes except json and *-full modes, one line equals a single article') return parser def parse(args):