1
0
mirror of https://github.com/ihabunek/toot.git synced 2024-12-04 14:46:33 -05:00

Show public or home timeline in curses

This commit is contained in:
Ivan Habunek 2018-01-06 11:25:05 +01:00
parent 521c329db9
commit 6149c9beda
No known key found for this signature in database
GPG Key ID: CDBD63C43A30BB95
3 changed files with 48 additions and 11 deletions

View File

@ -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

View File

@ -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()

View File

@ -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,
),
]