You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.5 KiB
Python
54 lines
1.5 KiB
Python
#!/usr/bin/python3
|
|
|
|
from mastodon import Mastodon
|
|
import configparser
|
|
import xdg.BaseDirectory
|
|
|
|
config_file_path = '%s/masto-mutuals/config.txt' % xdg.BaseDirectory.xdg_config_home
|
|
config = configparser.ConfigParser()
|
|
config.read(config_file_path)
|
|
# TODO: if config file is empty, this should write defaults into a file.
|
|
|
|
mastodon = Mastodon(
|
|
access_token = config['ACCOUNT_DATA']['access_token']
|
|
api_base_url = config['ACCOUNT_DATA']['instance_url']
|
|
)
|
|
|
|
user_id = mastodon.me()['id']
|
|
|
|
def fetch_user_ids(func, idval=user_id):
|
|
user_ids = []
|
|
initial = True
|
|
while True:
|
|
users = func(idval) if initial else mastodon.fetch_next(users)
|
|
initial = False
|
|
if users is None or len(users)==0:
|
|
break
|
|
for user in users:
|
|
user_ids.append(user['id'])
|
|
return user_ids
|
|
|
|
followed_ids = fetch_user_ids(mastodon.account_following)
|
|
follower_ids = fetch_user_ids(mastodon.account_followers)
|
|
mutual_ids = [i for i in followed_ids if i in followed_ids and i in follower_ids]
|
|
|
|
list_id = None
|
|
for l in mastodon.lists():
|
|
if l['title'] == 'mutuals':
|
|
list_id = l['id']
|
|
if list_id is not None:
|
|
break
|
|
if list_id is None:
|
|
l = mastodon.list_create('mutuals')
|
|
list_id = l['id']
|
|
|
|
list_ids = fetch_user_ids(mastodon.list_accounts, idval=list_id)
|
|
toadd_ids = [i for i in mutual_ids if i not in list_ids]
|
|
torm_ids = [i for i in list_ids if i not in mutual_ids]
|
|
|
|
if len(toadd_ids) > 0:
|
|
mastodon.list_accounts_add(list_id, toadd_ids)
|
|
|
|
if len(torm_ids) > 0:
|
|
mastodon.list_accounts_delete(list_id, torm_ids)
|