Consolidate timeline selection arguments

toot timeline and toot curses now use the same logic.
This commit is contained in:
Ivan Habunek 2019-02-15 14:06:47 +01:00
parent abb1b436ca
commit d224375da4
No known key found for this signature in database
GPG Key ID: CDBD63C43A30BB95
3 changed files with 57 additions and 71 deletions

View File

@ -192,10 +192,10 @@ def public_timeline_generator(instance, local=False, limit=20):
return _anon_timeline_generator(instance, path, params)
def tag_timeline_generator(app, user, hashtag, local=False, limit=20):
def tag_timeline_generator(instance, hashtag, local=False, limit=20):
path = '/api/v1/timelines/tag/{}'.format(hashtag)
params = {'local': str_bool(local), 'limit': limit}
return _timeline_generator(app, user, path, params)
return _anon_timeline_generator(instance, path, params)
def timeline_list_generator(app, user, list_id, limit=20):

View File

@ -7,7 +7,7 @@ from toot.output import print_out, print_instance, print_account, print_search_r
from toot.utils import assert_domain_exists, multiline_input, EOF_KEY
def timeline(app, user, args):
def get_timeline_generator(app, user, args):
# Make sure tag, list and public are not used simultaneously
if len([arg for arg in [args.tag, args.list, args.public] if arg]) > 1:
raise ConsoleError("Only one of --public, --tag, or --list can be used at one time.")
@ -15,17 +15,30 @@ def timeline(app, user, args):
if args.local and not (args.public or args.tag):
raise ConsoleError("The --local option is only valid alongside --public or --tag.")
if args.instance and not (args.public or args.tag):
raise ConsoleError("The --instance option is only valid alongside --public or --tag.")
if args.public:
gen = api.public_timeline_generator(app.instance, local=args.local, limit=args.count)
instance = args.instance or app.instance
return api.public_timeline_generator(instance, local=args.local, limit=args.count)
elif args.tag:
gen = api.tag_timeline_generator(app, user, args.tag, local=args.local, limit=args.count)
instance = args.instance or app.instance
return api.tag_timeline_generator(instance, args.tag, local=args.local, limit=args.count)
elif args.list:
gen = api.timeline_list_generator(app, user, args.list)
return api.timeline_list_generator(app, user, args.list, limit=args.count)
else:
gen = api.home_timeline_generator(app, user, limit=args.count)
return api.home_timeline_generator(app, user, limit=args.count)
def timeline(app, user, args):
generator = get_timeline_generator(app, user, args)
while(True):
items = next(gen)
try:
items = next(generator)
except StopIteration:
print_out("That's all folks.")
return
if args.reverse:
items = reversed(items)
@ -56,26 +69,8 @@ def thread(app, user, args):
def curses(app, user, args):
generator = get_timeline_generator(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, 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)
TimelineApp(app, user, generator).run()

View File

@ -84,6 +84,38 @@ status_id_arg = (["status_id"], {
"type": int,
})
# Arguments for selecting a timeline (see `toot.commands.get_timeline_generator`)
timeline_args = [
(["-p", "--public"], {
"action": "store_true",
"default": False,
"help": "show public timeline (does not require auth)",
}),
(["-t", "--tag"], {
"type": str,
"help": "show hashtag timeline (does not require auth)",
}),
(["-l", "--local"], {
"action": "store_true",
"default": False,
"help": "show only statuses from local instance (public and tag timelines only)",
}),
(["-i", "--instance"], {
"type": str,
"help": "mastodon instance from which to read (public and tag timelines only)",
}),
(["--list"], {
"type": int,
"help": "show timeline for given list.",
}),
(["-c", "--count"], {
"type": timeline_count,
"help": "number of toots to show per page (1-20, default 10).",
"default": 10,
}),
]
AUTH_COMMANDS = [
Command(
name="login",
@ -117,6 +149,7 @@ AUTH_COMMANDS = [
),
]
READ_COMMANDS = [
Command(
name="whoami",
@ -174,35 +207,12 @@ READ_COMMANDS = [
Command(
name="timeline",
description="Show recent items in a timeline (home by default)",
arguments=[
(["-p", "--public"], {
"action": "store_true",
"default": False,
"help": "Show public timeline.",
}),
(["-t", "--tag"], {
"type": str,
"help": "Show timeline for given hashtag.",
}),
(["-i", "--list"], {
"type": int,
"help": "Show timeline for given list ID.",
}),
(["-l", "--local"], {
"action": "store_true",
"default": False,
"help": "Show only statuses from local instance (public and tag timelines only).",
}),
arguments=timeline_args + [
(["-r", "--reverse"], {
"action": "store_true",
"default": False,
"help": "Reverse the order of the shown timeline (to new posts at the bottom)",
}),
(["-c", "--count"], {
"type": timeline_count,
"help": "Number of toots to show per page (1-20, default 10).",
"default": 10,
}),
(["-1", "--once"], {
"action": "store_true",
"default": False,
@ -214,26 +224,7 @@ READ_COMMANDS = [
Command(
name="curses",
description="An experimental timeline app (doesn't work on Windows)",
arguments=[
(["-p", "--public"], {
"action": 'store_true',
"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)',
}),
],
arguments=timeline_args,
require_auth=False,
),
]