Add `tags info` command

This commit is contained in:
Ivan Habunek 2023-12-13 08:37:43 +01:00
parent 381e3583ef
commit 01f3370b89
No known key found for this signature in database
GPG Key ID: F5F0623FF5EBCB3D
3 changed files with 39 additions and 0 deletions

View File

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

View File

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

View File

@ -3,6 +3,7 @@ import json as pyjson
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
@ -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