From 6149c9bedaec6fa7bf3261906a5dce146f349085 Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Sat, 6 Jan 2018 11:25:05 +0100 Subject: [PATCH] Show public or home timeline in curses --- toot/api.py | 34 ++++++++++++++++++++++++++-------- toot/commands.py | 11 ++++++++++- toot/console.py | 14 ++++++++++++-- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/toot/api.py b/toot/api.py index e59954f..f0c8f77 100644 --- a/toot/api.py +++ b/toot/api.py @@ -88,22 +88,40 @@ def timeline_home(app, user): return http.get(app, user, '/api/v1/timelines/home').json() -def timeline_generator(app, user, limit=20): - path = '/api/v1/timelines/home?limit={}'.format(limit) +def get_next_path(headers): + """Given timeline response headers, returns the path to the next batch""" + links = headers.get('Link', '') + matches = re.match('<([^>]+)>; rel="next"', links) + if matches: + parsed = urlparse(matches.group(1)) + return "?".join([parsed.path, parsed.query]) - def get_next_path(headers): - links = headers.get('Link', '') - matches = re.match('<([^>]+)>; rel="next"', links) - if matches: - parsed = urlparse(matches.group(1)) - return "?".join([parsed.path, parsed.query]) +def _timeline_generator(app, user, path, limit=20): while path: response = http.get(app, user, path) yield response.json() path = get_next_path(response.headers) +def _anon_timeline_generator(instance, path, limit=20): + while path: + url = "https://{}{}".format(instance, path) + response = http.anon_get(url, path) + yield response.json() + path = get_next_path(response.headers) + + +def home_timeline_generator(app, user, limit=20): + path = '/api/v1/timelines/home?limit={}'.format(limit) + 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 upload_media(app, user, file): return http.post(app, user, '/api/v1/media', files={ 'file': file diff --git a/toot/commands.py b/toot/commands.py index 084cfed..c9b3674 100644 --- a/toot/commands.py +++ b/toot/commands.py @@ -65,7 +65,16 @@ def timeline(app, user, args): def curses(app, user, args): from toot.app import TimelineApp - generator = api.timeline_generator(app, user) + + 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) + else: + generator = api.home_timeline_generator(app, user) + TimelineApp(generator).run() diff --git a/toot/console.py b/toot/console.py index 0db79cd..20edce1 100644 --- a/toot/console.py +++ b/toot/console.py @@ -138,8 +138,18 @@ READ_COMMANDS = [ Command( name="curses", description="An experimental timeline app (doesn't work on Windows)", - arguments=[], - require_auth=True, + arguments=[ + (["-p", "--public"], { + "action": 'store_true', + "default": False, + "help": "Resolve non-local accounts", + }), + (["-i", "--instance"], { + "type": str, + "help": 'instance from which to read (for public timeline only)', + }) + ], + require_auth=False, ), ]