Skip to content

Commit

Permalink
Use authorized get to fetch public and tag timelines
Browse files Browse the repository at this point in the history
Some servers require authorization for viewing "public" timelines, and
since currently toot always requires auth for this it's not required to
support anon access to timelines.

fixes #168
  • Loading branch information
ihabunek committed May 11, 2020
1 parent ad96143 commit 3b5769a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ Changelog

<!-- Do not edit. This file is automatically generated from changelog.yaml.-->

**0.27.0 (TBD)**

* Fix access to public and tag timelines when on private mastodon instances
(#168)

**0.26.0 (2020-04-15)**

* Fix datetime parsing on Python 3.5 (#162)
Expand Down
5 changes: 5 additions & 0 deletions changelog.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.27.0:
date: "TBD"
changes:
- "Fix access to public and tag timelines when on private mastodon instances (#168)"

0.26.0:
date: 2020-04-15
changes:
Expand Down
36 changes: 24 additions & 12 deletions toot/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,36 +173,48 @@ def _timeline_generator(app, user, path, params=None):
path = _get_next_path(response.headers)


def _anon_timeline_generator(instance, path, params=None):
while path:
url = "https://{}{}".format(instance, path)
response = http.anon_get(url, params)
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, local=False, limit=20):
def public_timeline_generator(app, user, local=False, limit=20):
path = '/api/v1/timelines/public'
params = {'local': str_bool(local), 'limit': limit}
return _anon_timeline_generator(instance, path, params)
return _timeline_generator(app, user, path, params)


def tag_timeline_generator(instance, hashtag, local=False, limit=20):
def tag_timeline_generator(app, user, hashtag, local=False, limit=20):
path = '/api/v1/timelines/tag/{}'.format(quote(hashtag))
params = {'local': str_bool(local), 'limit': limit}
return _anon_timeline_generator(instance, path, params)
return _timeline_generator(app, user, path, params)


def timeline_list_generator(app, user, list_id, limit=20):
path = '/api/v1/timelines/list/{}'.format(list_id)
return _timeline_generator(app, user, path, {'limit': limit})


def _anon_timeline_generator(instance, path, params=None):
while path:
url = "https://{}{}".format(instance, path)
response = http.anon_get(url, params)
yield response.json()
path = _get_next_path(response.headers)


def anon_public_timeline_generator(instance, local=False, limit=20):
path = '/api/v1/timelines/public'
params = {'local': str_bool(local), 'limit': limit}
return _anon_timeline_generator(instance, path, params)


def anon_tag_timeline_generator(instance, hashtag, local=False, limit=20):
path = '/api/v1/timelines/tag/{}'.format(quote(hashtag))
params = {'local': str_bool(local), 'limit': limit}
return _anon_timeline_generator(instance, path, params)


def upload_media(app, user, file):
return http.post(app, user, '/api/v1/media', files={
'file': file
Expand Down
6 changes: 2 additions & 4 deletions toot/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ def get_timeline_generator(app, user, args):
raise ConsoleError("The --instance option is only valid alongside --public or --tag.")

if args.public:
instance = args.instance or app.instance
return api.public_timeline_generator(instance, local=args.local, limit=args.count)
return api.public_timeline_generator(app, user, local=args.local, limit=args.count)
elif args.tag:
instance = args.instance or app.instance
return api.tag_timeline_generator(instance, args.tag, local=args.local, limit=args.count)
return api.tag_timeline_generator(app, user, args.tag, local=args.local, limit=args.count)
elif args.list:
return api.timeline_list_generator(app, user, args.list, limit=args.count)
else:
Expand Down
4 changes: 2 additions & 2 deletions toot/tui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,13 @@ def goto_home_timeline(self):

def goto_public_timeline(self, local):
self.timeline_generator = api.public_timeline_generator(
self.app.instance, local=local, limit=40)
self.app, self.user, local=local, limit=40)
promise = self.async_load_timeline(is_initial=True, timeline_name="public")
promise.add_done_callback(lambda *args: self.close_overlay())

def goto_tag_timeline(self, tag, local):
self.timeline_generator = api.tag_timeline_generator(
self.app.instance, tag, local=local, limit=40)
self.app, self.user, tag, local=local, limit=40)
promise = self.async_load_timeline(is_initial=True, timeline_name="#{}".format(tag))
promise.add_done_callback(lambda *args: self.close_overlay())

Expand Down

0 comments on commit 3b5769a

Please sign in to comment.