Last active
November 16, 2022 13:14
-
-
Save axwax/d0427b82afb30859d63cc93d5411a584 to your computer and use it in GitHub Desktop.
Add (already followed) users to a Mastodon list using Mastodon.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Very quickly thrown together script to add (already followed) Mastodon users to a list. | |
# YMMV - it probably needs some tweaking to work for you | |
# relies on Mastodon.py and needs a "credentials.py" file with the variables access_token and api_base_url | |
# I created my access token following the docs at https://docs.joinmastodon.org/client/token/ and https://docs.joinmastodon.org/client/authorized/ | |
# but you might be able to get the access token by going to Settings|Development|New Application (I haven't tried though). | |
# As there is a rate limit for usage of the API I wouldn't try to import lots of users at once - I imported 132 users and it worked though... | |
# @axwax@fosstodon.org | |
import signal | |
from urllib.request import urlopen | |
from mastodon import Mastodon | |
from credentials import access_token, api_base_url | |
# name of the text file to import | |
# one username in the format @user@instance per line | |
list_filename = "makers.txt" | |
# Initialise Mastodon.py | |
mastodon = Mastodon( | |
access_token = str(access_token), | |
api_base_url = str(api_base_url) | |
) | |
# grab all lists and print their IDs | |
my_lists = mastodon.lists() | |
print (my_lists) | |
# let user enter a list's id | |
list_id = int(input ("please enter list id:")) | |
print(list_id) | |
# get all account ids that are currently in the list | |
the_list = mastodon.list_accounts(list_id) | |
account_ids_in_list = [user['id'] for user in the_list] | |
# remove the new line characters | |
not_added = [] | |
not_found = [] | |
account_ids_to_add = [] | |
# open the text file and loop through each entry | |
with open(list_filename) as f: | |
lines = [line.rstrip() for line in f] | |
for line in lines: | |
# for each entry search for the corresponding account, only searching for accounts you already follow | |
account = mastodon.account_search(q=line, following = True) | |
# if the account is found and not already in the list, add it to "account_ids_to_add", otherwise add it to "not_found" or "not_added" respectively | |
if (len(account) == 1): | |
account = account[0] | |
print(account['acct'] + ", " + account['display_name'] + ", " + str(account['id'])) | |
if(account['id'] not in account_ids_in_list): | |
account_ids_to_add.append(account['id']) | |
else: | |
not_added.append(account['id']) | |
else: | |
not_found.append(line) | |
# print out any possible errors / warnings | |
if (len(not_found) > 0): | |
print("error: the following accounts could not be found: ") | |
print(*not_found,sep='\n') | |
if (len(not_added) > 0): | |
print("warning: the following accounts were already in the list: ") | |
print(*not_added,sep='\n') | |
# finally, try to add the accouts to the list | |
try: | |
makers = mastodon.list_accounts_add(list_id, account_ids_to_add) | |
print(makers) | |
except: | |
print("error: can't add the following accounts: ") | |
print(*account_ids_to_add,sep='\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment