From 4df0c7882d9f60bc3b5dfeae4f3ecd41eeb32d33 Mon Sep 17 00:00:00 2001 From: ksunden Date: Wed, 2 Jan 2019 22:36:40 -0600 Subject: [PATCH] ENH: Add local and tag timeline support to curses Closes #61 --- toot/api.py | 21 ++++++++++++++------- toot/commands.py | 11 ++++++++++- toot/console.py | 11 ++++++++++- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/toot/api.py b/toot/api.py index e0cca77..8846d69 100644 --- a/toot/api.py +++ b/toot/api.py @@ -173,17 +173,17 @@ def get_next_path(headers): return "?".join([parsed.path, parsed.query]) -def _timeline_generator(app, user, path, limit=20): +def _timeline_generator(app, user, path, params=None): while path: - response = http.get(app, user, path) + response = http.get(app, user, path, params) yield response.json() path = get_next_path(response.headers) -def _anon_timeline_generator(instance, path, limit=20): +def _anon_timeline_generator(instance, path, params=None): while path: url = "https://{}{}".format(instance, path) - response = http.anon_get(url, path) + response = http.anon_get(url, params) yield response.json() path = get_next_path(response.headers) @@ -193,9 +193,16 @@ def home_timeline_generator(app, user, limit=20): return _timeline_generator(app, user, path) -def public_timeline_generator(instance, limit=20): - path = '/api/v1/timelines/public?limit={}'.format(limit) - return _anon_timeline_generator(instance, path) +def public_timeline_generator(instance, local=False, limit=20): + path = '/api/v1/timelines/public' + params = {'local': 'true' if local else 'false', 'limit': limit} + return _anon_timeline_generator(instance, path, params) + + +def tag_timeline_generator(app, user, hashtag, local=False, limit=20): + path = '/api/v1/timelines/tag/{}'.format(hashtag) + params = {'local': 'true' if local else 'false', 'limit': limit} + return _timeline_generator(app, user, path, params) def upload_media(app, user, file): diff --git a/toot/commands.py b/toot/commands.py index 4d9342b..184793c 100644 --- a/toot/commands.py +++ b/toot/commands.py @@ -33,12 +33,21 @@ def timeline(app, user, args): def curses(app, user, args): from toot.ui.app import TimelineApp + # Make sure tag, list and public are not used simultaneously + if len([arg for arg in [args.tag, args.public] if arg]) > 1: + raise ConsoleError("Only one of --public or --tag can be used at one time.") + + if args.local and not (args.public or args.tag): + raise ConsoleError("The --local option is only valid alongside --public or --tag.") + if not args.public and (not app or not user): raise ConsoleError("You must be logged in to view the home timeline.") if args.public: instance = args.instance or app.instance - generator = api.public_timeline_generator(instance) + generator = api.public_timeline_generator(instance, local=args.local) + elif args.tag: + generator = api.tag_timeline_generator(app, user, args.tag, local=args.local) else: generator = api.home_timeline_generator(app, user) diff --git a/toot/console.py b/toot/console.py index c6e5363..3709371 100644 --- a/toot/console.py +++ b/toot/console.py @@ -193,10 +193,19 @@ READ_COMMANDS = [ "default": False, "help": "Resolve non-local accounts", }), + (["-t", "--tag"], { + "type": str, + "help": "Show timeline for given hashtag.", + }), + (["-l", "--local"], { + "action": "store_true", + "default": False, + "help": "Show only statuses from local instance (public and tag timelines only).", + }), (["-i", "--instance"], { "type": str, "help": 'instance from which to read (for public timeline only)', - }) + }), ], require_auth=False, ),