From 28f365410d27a496f0539a0a48f211132b831fc1 Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Thu, 30 Nov 2023 20:26:35 +0100 Subject: [PATCH] wip --- tests/integration/test_timelines.py | 0 toot/cli/__init__.py | 3 +- toot/cli/timelines.py | 87 +++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 tests/integration/test_timelines.py create mode 100644 toot/cli/timelines.py diff --git a/tests/integration/test_timelines.py b/tests/integration/test_timelines.py new file mode 100644 index 00000000..e69de29b diff --git a/toot/cli/__init__.py b/toot/cli/__init__.py index 2e6451c4..29c57701 100644 --- a/toot/cli/__init__.py +++ b/toot/cli/__init__.py @@ -1,9 +1,10 @@ from toot.cli.base import cli, Context # noqa -from toot.cli.auth import * from toot.cli.accounts import * +from toot.cli.auth import * from toot.cli.lists import * from toot.cli.post import * from toot.cli.read import * from toot.cli.statuses import * from toot.cli.tags import * +from toot.cli.timelines import * diff --git a/toot/cli/timelines.py b/toot/cli/timelines.py new file mode 100644 index 00000000..957de9b6 --- /dev/null +++ b/toot/cli/timelines.py @@ -0,0 +1,87 @@ +import click + +from toot import api, App, User +from toot.cli.base import cli, pass_context, Context +from typing import Optional + +from toot.entities import Status, from_dict +from toot.output import print_timeline + + +@cli.command() +@click.option("--account", "-a", help="Show account timeline") +@click.option("--instance", "-i", help="Instance from which to read (public and tag timelines only)") +@click.option("--list", help="Show list timeline") +@click.option("--local", "-l", is_flag=True, help="Show only statuses from the local instance (public and tag timelines only)") +@click.option("--public", "-p", is_flag=True, help="Show public timeline") +@click.option("--tag", "-t", help="Show hashtag timeline") +@pass_context +def timeline( + ctx: Context, + account: Optional[str], + instance: Optional[str], + list: Optional[str], + local: bool, + public: bool, + tag: Optional[str], +): + """Show recent statuses in a timeline""" + generator = get_timeline_generator(ctx.app, ctx.user, account, instance, list, local, public, tag) + + while True: + try: + items = next(generator) + except StopIteration: + click.echo("That's all folks.") + return + + # if args.reverse: + # items = reversed(items) + + statuses = [from_dict(Status, item) for item in items] + print_timeline(statuses) + + # if args.once or not sys.stdout.isatty(): + # break + + char = input("\nContinue? [Y/n] ") + if char.lower() == "n": + break + + +def get_timeline_generator( + app: App, + user: User, + account: Optional[str], + instance: Optional[str], + list: Optional[str], + local: bool, + public: bool, + tag: Optional[str], + limit=20, # TODO +): + if len([arg for arg in [tag, list, public, account] if arg]) > 1: + raise click.ClickException("Only one of --public, --tag, --account, or --list can be used at one time.") + + if local and not (public or tag): + raise click.ClickException("The --local option is only valid alongside --public or --tag.") + + if instance and not (public or tag): + raise click.ClickException("The --instance option is only valid alongside --public or --tag.") + + if public: + if instance: + return api.anon_public_timeline_generator(instance, local=local, limit=limit) + else: + return api.public_timeline_generator(app, user, local=local, limit=limit) + elif tag: + if instance: + return api.anon_tag_timeline_generator(instance, tag, limit=limit) + else: + return api.tag_timeline_generator(app, user, tag, local=local, limit=limit) + elif account: + return api.account_timeline_generator(app, user, account, limit=limit) + elif list: + return api.timeline_list_generator(app, user, list, limit=limit) + else: + return api.home_timeline_generator(app, user, limit=limit)