Skip to content

Commit

Permalink
Avoid adding/removing unncessarily
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseWeinstein committed Dec 27, 2022
1 parent 6a541c7 commit 531009c
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions src/tootstream/toot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2107,15 +2107,21 @@ def listdifference(mastodon, rest):
cprint("List {} is not found".format(items[1]), fg('red'))
return

list_accounts = mastodon.list_accounts(diff_list_id)
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:
try:
mastodon.list_accounts_delete(list_id, user['id'])
cprint("Removed {} from list {}.".format(
if user['id'] in existing_ids:
cprint("User {} already missing from list {}.".format(
format_username(user), items[0]), fg('green'))
except Exception as e:
cprint("error while deleting from list: {}".format(
type(e).__name__), fg('red'))
else:
try:
mastodon.list_accounts_delete(list_id, user['id'])
cprint("Removed {} from list {}.".format(
format_username(user), items[0]), fg('green'))
except Exception as e:
cprint("error while deleting from list: {}".format(
type(e).__name__), fg('red'))


listdifference.__argstr__ = '<list> <list>'
Expand Down Expand Up @@ -2147,16 +2153,22 @@ def listunion(mastodon, rest):
cprint("List {} is not found".format(items[1]), fg('red'))
return

list_accounts = mastodon.list_accounts(union_list_id)
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:
try:
mastodon.list_accounts_add(list_id, user['id'])
cprint("Added {} to list {}.".format(
if user['id'] in existing_ids:
cprint("User {} already on list {}.".format(
format_username(user), items[0]), fg('green'))
except Exception as e:
cprint("error while adding to list: {}".format(
type(e).__name__), fg('red'))
else:
try:
mastodon.list_accounts_add(list_id, user['id'])
cprint("Added {} to list {}.".format(
format_username(user), items[0]), fg('green'))
except Exception as e:
cprint("error while adding to list: {}".format(
type(e).__name__), fg('red'))


listunion.__argstr__ = '<list> <list>'
Expand Down

0 comments on commit 531009c

Please sign in to comment.