Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add list set (union, difference) commands #221

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions src/tootstream/toot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import shutil
from collections import OrderedDict
import webbrowser

# Get the version of Tootstream
import pkg_resources # part of setuptools
import click
Expand Down Expand Up @@ -1978,6 +1979,82 @@ def listaccounts(mastodon, rest):
printUser(user)


@command("<list> <list>", "List")
def listdifference(mastodon, rest):
"""Remove users in the second list from the first list.
ex: listdifference list_to_remove_from list_of_what_to_remove"""
if not (list_support(mastodon)):
return
if not rest:
cprint("Argument required.", fg("red"))
return
items = rest.split(" ")
if len(items) < 2:
cprint("Not enough arguments.", fg("red"))
return
list_id = get_list_id(mastodon, items[0])
diff_list_id = get_list_id(mastodon, items[1])

list_accounts = mastodon.fetch_remaining(mastodon.list_accounts(diff_list_id))
existing_ids = [
x["id"] for x in mastodon.fetch_remaining(mastodon.list_accounts(list_id))
]

for user in list_accounts:
if user["id"] in existing_ids:
cprint(
"User {} already missing from list {}.".format(
format_username(user), items[0]
),
fg("green"),
)
else:
mastodon.list_accounts_delete(list_id, user["id"])
cprint(
"Removed {} from list {}.".format(format_username(user), items[0]),
fg("green"),
)


@command("<list> <list>", "List")
def listunion(mastodon, rest):
"""Add users in the second list to the first list.
ex: listunion list_to_add_to list_of_what_to_add"""
if not (list_support(mastodon)):
return
if not rest:
cprint("Argument required.", fg("red"))
return
items = rest.split(" ")
if len(items) < 2:
cprint("Not enough arguments.", fg("red"))
return
list_id = get_list_id(mastodon, items[0])
if items[1] == "[following]":
user = mastodon.account_verify_credentials()
list_accounts = mastodon.fetch_remaining(mastodon.account_following(user["id"]))
else:
union_list_id = get_list_id(mastodon, items[1])
list_accounts = mastodon.fetch_remaining(mastodon.list_accounts(union_list_id))

existing_ids = [
x["id"] for x in mastodon.fetch_remaining(mastodon.list_accounts(list_id))
]

for user in list_accounts:
if user["id"] in existing_ids:
cprint(
"User {} already on list {}.".format(format_username(user), items[0]),
fg("green"),
)
else:
mastodon.list_accounts_add(list_id, user["id"])
cprint(
"Added {} to list {}.".format(format_username(user), items[0]),
fg("green"),
)


@command("<list> <user>", "List")
def listadd(mastodon, rest):
"""Add user to list.
Expand Down