followers/followers.py

118 lines
2.3 KiB
Python

from app.libraries.database import Database
from app.libraries.setup import Setup
from mastodon import Mastodon
import pdb
def followers_list(my_id):
followers_list = []
temp_followers_list = {}
i = 0
while True:
if i == 0:
temp_followers_list[i] = mastodon.account_followers(my_id, limit=80)
if temp_followers_list[i] == []:
return followers_list
else:
temp_followers_list[i] = mastodon.fetch_next(temp_followers_list[i-1]._pagination_next)
dict_len = len(temp_followers_list[i])
ii = 0
while ii < dict_len:
followers_list.append(temp_followers_list[i][ii])
ii += 1
if len(temp_followers_list[i]) < 80:
return followers_list
i += 1
if __name__ == '__main__':
db = Database()
setup = Setup()
mastodon = Mastodon(
access_token = setup.mastodon_app_token,
api_base_url= setup.mastodon_hostname
)
my_id = mastodon.me()
followers = followers_list(my_id)
db.reset()
for follower in followers:
db.find_follower(follower.acct)
last_local, last_remote = db.last_total()
local_followers, remote_followers = db.totals()
if last_local != 0:
local_incr = local_followers - last_local
else:
local_incr = local_followers
if last_remote != 0:
remote_incr = remote_followers - last_remote
else:
remote_incr = remote_followers
db.save_total(local_followers, local_incr, remote_followers, remote_incr)
unfollowers = db.find_unfollowers()
post_text = f"@{mastodon.me().acct}\nseguidors: {len(followers)}\n"
post_text += f"locals: {local_followers} ({local_incr})\nremots: {remote_followers} ({remote_incr})\n"
if unfollowers != []:
post_text += f'\nJa no et segueix:\n'
for unfollow in unfollowers:
post_text += f'- {unfollow}'
unfollow_id = mastodon.account_lookup(unfollow).id
try:
mastodon.account_unfollow(unfollow_id)
post_text += f' (has deixat de seguir-lo)\n'
except:
post_text += f' (ja no existeix)\n'
pass
mastodon.status_post(post_text, in_reply_to_id=None, visibility='direct')