Skip to content

Commit

Permalink
Add tags info command
Browse files Browse the repository at this point in the history
  • Loading branch information
ihabunek committed Dec 13, 2023
1 parent 381e358 commit 01f3370
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- "Migrate to `click` for commandline arguments. BC should be mostly preserved, please report any issues."
- "Add shell completion, see: https://toot.bezdomni.net/shell_completion.html"
- "Add `--json` option to tag commands"
- "Add `tags info`, `tags featured`, `tags feature`, and `tags unfeature` commands"

0.39.0:
date: 2023-11-23
Expand Down
14 changes: 14 additions & 0 deletions toot/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,20 @@ def unfeature_tag(app, user, tag_id: str) -> Response:
return http.delete(app, user, f"/api/v1/featured_tags/{tag_id}")


def find_tag(app, user, tag) -> Optional[dict]:
"""Find a hashtag by tag name or ID"""
tag = tag.lstrip("#")
results = search(app, user, tag, type="hashtags").json()

return next(
(
t for t in results["hashtags"]
if t["name"].lower() == tag.lstrip("#").lower() or t["id"] == tag
),
None
)


def find_featured_tag(app, user, tag) -> Optional[dict]:
"""Find a featured tag by tag name or ID"""
return next(
Expand Down
24 changes: 24 additions & 0 deletions toot/cli/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from toot import api
from toot.cli.base import cli, pass_context, json_option, Context
from toot.entities import Tag, from_dict
from toot.output import print_tag_list, print_warning


Expand All @@ -11,6 +12,29 @@ def tags():
"""List, follow, and unfollow tags"""


@tags.command()
@click.argument("tag")
@json_option
@pass_context
def info(ctx: Context, tag, json: bool):
"""Show a hashtag and its associated information"""
tag = api.find_tag(ctx.app, ctx.user, tag)

if not tag:
raise click.ClickException("Tag not found")

if json:
click.echo(pyjson.dumps(tag))
else:
tag = from_dict(Tag, tag)
click.secho(f"#{tag.name}", fg="yellow")
click.secho(tag.url, italic=True)
if tag.following:
click.echo("Followed")
else:
click.echo("Not followed")


@tags.command()
@json_option
@pass_context
Expand Down

0 comments on commit 01f3370

Please sign in to comment.