mirror of
https://github.com/ihabunek/toot
synced 2025-02-09 08:38:39 +01:00
Command line support for following hashtags (Mastodon 4+)
This commit is contained in:
parent
a3fa7e1e3a
commit
67b52757a4
31
toot/api.py
31
toot/api.py
@ -24,6 +24,12 @@ def _status_action(app, user, status_id, action, data=None):
|
|||||||
return http.post(app, user, url, data=data).json()
|
return http.post(app, user, url, data=data).json()
|
||||||
|
|
||||||
|
|
||||||
|
def _tag_action(app, user, tag_name, action):
|
||||||
|
url = '/api/v1/tags/{}/{}'.format(tag_name, action)
|
||||||
|
|
||||||
|
return http.post(app, user, url).json()
|
||||||
|
|
||||||
|
|
||||||
def create_app(domain, scheme='https'):
|
def create_app(domain, scheme='https'):
|
||||||
url = '{}://{}/api/v1/apps'.format(scheme, domain)
|
url = '{}://{}/api/v1/apps'.format(scheme, domain)
|
||||||
|
|
||||||
@ -318,23 +324,36 @@ def unfollow(app, user, account):
|
|||||||
return _account_action(app, user, account, 'unfollow')
|
return _account_action(app, user, account, 'unfollow')
|
||||||
|
|
||||||
|
|
||||||
def _get_account_list(app, user, path):
|
def follow_tag(app, user, tag_name):
|
||||||
accounts = []
|
return _tag_action(app, user, tag_name, 'follow')
|
||||||
|
|
||||||
|
|
||||||
|
def unfollow_tag(app, user, tag_name):
|
||||||
|
return _tag_action(app, user, tag_name, 'unfollow')
|
||||||
|
|
||||||
|
|
||||||
|
def _get_response_list(app, user, path):
|
||||||
|
items = []
|
||||||
while path:
|
while path:
|
||||||
response = http.get(app, user, path)
|
response = http.get(app, user, path)
|
||||||
accounts += response.json()
|
items += response.json()
|
||||||
path = _get_next_path(response.headers)
|
path = _get_next_path(response.headers)
|
||||||
return accounts
|
return items
|
||||||
|
|
||||||
|
|
||||||
def following(app, user, account):
|
def following(app, user, account):
|
||||||
path = '/api/v1/accounts/{}/{}'.format(account, 'following')
|
path = '/api/v1/accounts/{}/{}'.format(account, 'following')
|
||||||
return _get_account_list(app, user, path)
|
return _get_response_list(app, user, path)
|
||||||
|
|
||||||
|
|
||||||
def followers(app, user, account):
|
def followers(app, user, account):
|
||||||
path = '/api/v1/accounts/{}/{}'.format(account, 'followers')
|
path = '/api/v1/accounts/{}/{}'.format(account, 'followers')
|
||||||
return _get_account_list(app, user, path)
|
return _get_response_list(app, user, path)
|
||||||
|
|
||||||
|
|
||||||
|
def followed_tags(app, user):
|
||||||
|
path = '/api/v1/followed_tags'
|
||||||
|
return _get_response_list(app, user, path)
|
||||||
|
|
||||||
|
|
||||||
def mute(app, user, account):
|
def mute(app, user, account):
|
||||||
|
@ -8,7 +8,8 @@ from toot import api, config, __version__
|
|||||||
from toot.auth import login_interactive, login_browser_interactive, create_app_interactive
|
from toot.auth import login_interactive, login_browser_interactive, create_app_interactive
|
||||||
from toot.exceptions import ApiError, ConsoleError
|
from toot.exceptions import ApiError, ConsoleError
|
||||||
from toot.output import (print_out, print_instance, print_account, print_acct_list,
|
from toot.output import (print_out, print_instance, print_account, print_acct_list,
|
||||||
print_search_results, print_timeline, print_notifications)
|
print_search_results, print_timeline, print_notifications,
|
||||||
|
print_tag_list)
|
||||||
from toot.tui.utils import parse_datetime
|
from toot.tui.utils import parse_datetime
|
||||||
from toot.utils import editor_input, multiline_input, EOF_KEY
|
from toot.utils import editor_input, multiline_input, EOF_KEY
|
||||||
|
|
||||||
@ -323,6 +324,23 @@ def followers(app, user, args):
|
|||||||
print_acct_list(response)
|
print_acct_list(response)
|
||||||
|
|
||||||
|
|
||||||
|
def follow_tag(app, user, args):
|
||||||
|
tn = args.tag_name if not args.tag_name.startswith("#") else args.tag_name[1:]
|
||||||
|
api.follow_tag(app, user, tn)
|
||||||
|
print_out("<green>✓ You are now following #{}</green>".format(tn))
|
||||||
|
|
||||||
|
|
||||||
|
def unfollow_tag(app, user, args):
|
||||||
|
tn = args.tag_name if not args.tag_name.startswith("#") else args.tag_name[1:]
|
||||||
|
api.unfollow_tag(app, user, tn)
|
||||||
|
print_out("<green>✓ You are no longer following #{}</green>".format(tn))
|
||||||
|
|
||||||
|
|
||||||
|
def followed_tags(app, user, args):
|
||||||
|
response = api.followed_tags(app, user)
|
||||||
|
print_tag_list(response)
|
||||||
|
|
||||||
|
|
||||||
def mute(app, user, args):
|
def mute(app, user, args):
|
||||||
account = _find_account(app, user, args.account)
|
account = _find_account(app, user, args.account)
|
||||||
api.mute(app, user, account['id'])
|
api.mute(app, user, account['id'])
|
||||||
|
@ -161,6 +161,10 @@ visibility_arg = (["-v", "--visibility"], {
|
|||||||
"the TOOT_POST_VISIBILITY environment variable",
|
"the TOOT_POST_VISIBILITY environment variable",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
tag_arg = (["tag_name"], {
|
||||||
|
"type": str,
|
||||||
|
"help": "tag name, e.g. Caturday, or \"#Caturday\"",
|
||||||
|
})
|
||||||
|
|
||||||
# Arguments for selecting a timeline (see `toot.commands.get_timeline_generator`)
|
# Arguments for selecting a timeline (see `toot.commands.get_timeline_generator`)
|
||||||
common_timeline_args = [
|
common_timeline_args = [
|
||||||
@ -552,7 +556,28 @@ ACCOUNTS_COMMANDS = [
|
|||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
COMMANDS = AUTH_COMMANDS + READ_COMMANDS + TUI_COMMANDS + POST_COMMANDS + STATUS_COMMANDS + ACCOUNTS_COMMANDS
|
TAG_COMMANDS = [
|
||||||
|
Command(
|
||||||
|
name="follow_tag",
|
||||||
|
description="Follow a hashtag",
|
||||||
|
arguments=[tag_arg],
|
||||||
|
require_auth=True,
|
||||||
|
),
|
||||||
|
Command(
|
||||||
|
name="unfollow_tag",
|
||||||
|
description="Unfollow a hashtag",
|
||||||
|
arguments=[tag_arg],
|
||||||
|
require_auth=True,
|
||||||
|
),
|
||||||
|
Command(
|
||||||
|
name="followed_tags",
|
||||||
|
description="List hashtags you follow",
|
||||||
|
arguments=[],
|
||||||
|
require_auth=True,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
COMMANDS = AUTH_COMMANDS + READ_COMMANDS + TUI_COMMANDS + POST_COMMANDS + STATUS_COMMANDS + ACCOUNTS_COMMANDS + TAG_COMMANDS
|
||||||
|
|
||||||
|
|
||||||
def print_usage():
|
def print_usage():
|
||||||
@ -565,6 +590,7 @@ def print_usage():
|
|||||||
("Post", POST_COMMANDS),
|
("Post", POST_COMMANDS),
|
||||||
("Status", STATUS_COMMANDS),
|
("Status", STATUS_COMMANDS),
|
||||||
("Accounts", ACCOUNTS_COMMANDS),
|
("Accounts", ACCOUNTS_COMMANDS),
|
||||||
|
("Hashtags", TAG_COMMANDS),
|
||||||
]
|
]
|
||||||
|
|
||||||
print_out("<green>{}</green>".format(CLIENT_NAME))
|
print_out("<green>{}</green>".format(CLIENT_NAME))
|
||||||
|
@ -198,6 +198,11 @@ def print_acct_list(accounts):
|
|||||||
print_out(f"* <green>@{account['acct']}</green> {account['display_name']}")
|
print_out(f"* <green>@{account['acct']}</green> {account['display_name']}")
|
||||||
|
|
||||||
|
|
||||||
|
def print_tag_list(tags):
|
||||||
|
for tag in tags:
|
||||||
|
print_out(f"* <green>#{tag['name']}\t</green> {tag['url']}")
|
||||||
|
|
||||||
|
|
||||||
def print_search_results(results):
|
def print_search_results(results):
|
||||||
accounts = results['accounts']
|
accounts = results['accounts']
|
||||||
hashtags = results['hashtags']
|
hashtags = results['hashtags']
|
||||||
|
Loading…
x
Reference in New Issue
Block a user