Do some OOP Stuff
This commit is contained in:
parent
f43877fc86
commit
c9eb4f072c
2 changed files with 89 additions and 76 deletions
|
@ -2,9 +2,10 @@
|
|||
|
||||
import json
|
||||
from mastodon import Mastodon
|
||||
import datetime
|
||||
import sys
|
||||
from InactiveUserManager import InactiveUserManager
|
||||
|
||||
if __name__ == "__main__":
|
||||
CONFIG_FILE = "config.json"
|
||||
|
||||
try:
|
||||
|
@ -20,66 +21,10 @@ try:
|
|||
access_token=ACCESS_TOKEN,
|
||||
api_base_url=API_BASE_URL
|
||||
)
|
||||
|
||||
my_account = mastodon.me()
|
||||
except Exception as e:
|
||||
sys.exit(f"Fehler beim Verbinden mit der Instanz: {e}")
|
||||
|
||||
my_id = my_account['id']
|
||||
|
||||
print("Lade alle Accounts, denen Du folgst...\n")
|
||||
following = []
|
||||
accounts = mastodon.account_following(my_id)
|
||||
following.extend(accounts)
|
||||
|
||||
while accounts:
|
||||
accounts = mastodon.fetch_next(accounts)
|
||||
if accounts:
|
||||
following.extend(accounts)
|
||||
|
||||
print(f"Du folgst insgesamt {len(following)} Accounts.\n")
|
||||
|
||||
now = datetime.datetime.now(datetime.timezone.utc)
|
||||
threshold_date = now - datetime.timedelta(days=180)
|
||||
|
||||
unfollowed_users = []
|
||||
|
||||
for account in following:
|
||||
account_id = account['id']
|
||||
|
||||
full_acct = account.get('acct', account.get('username', 'Unbekannt'))
|
||||
|
||||
|
||||
last_activity = None
|
||||
if account.get('statuses_count', 0) > 0:
|
||||
try:
|
||||
statuses = mastodon.account_statuses(account_id, limit=1)
|
||||
except Exception as e:
|
||||
print(f"Fehler beim Abruf der Status für {full_acct}: {e}")
|
||||
continue
|
||||
if statuses:
|
||||
last_activity = statuses[0]['created_at']
|
||||
|
||||
if last_activity is None:
|
||||
last_activity = account.get('created_at', None)
|
||||
|
||||
if last_activity is None:
|
||||
print(f"Keine Aktivitätsdaten für {full_acct}. Überspringe diesen Account.")
|
||||
continue
|
||||
|
||||
if last_activity < threshold_date:
|
||||
try:
|
||||
mastodon.account_unfollow(account_id)
|
||||
unfollowed_users.append((full_acct, last_activity))
|
||||
print(f"Entfolgt: {full_acct} – Letzte Aktivität: {last_activity.date()}")
|
||||
except Exception as e:
|
||||
print(f"Fehler beim Entfolgen von {full_acct}: {e}")
|
||||
|
||||
if unfollowed_users:
|
||||
print("\nZusammenfassung der entfolgten Accounts:")
|
||||
for acct, last in unfollowed_users:
|
||||
print(f"- {acct}: Letzte Aktivität am {last.date()}")
|
||||
else:
|
||||
print("Es wurden keine Accounts gefunden, die seit mehr als 6 Monaten inaktiv sind.")
|
||||
|
||||
manager = InactiveUserManager(mastodon, 180)
|
||||
manager.run()
|
||||
print("\nUnd wie der legendäre Giovanni Trapatoni einst sagte: 'Ich habe fertig!'\n")
|
||||
|
|
68
InactiveUserManager.py
Normal file
68
InactiveUserManager.py
Normal file
|
@ -0,0 +1,68 @@
|
|||
import datetime
|
||||
|
||||
class InactiveUserManager:
|
||||
def __init__(self, mastodon_client, grace_period_days: int):
|
||||
self.mastodon_client = mastodon_client
|
||||
self.grace_period_days = grace_period_days
|
||||
self.my_id = None
|
||||
self.following = []
|
||||
self.unfollowed_users = []
|
||||
|
||||
def fetch_account_id(self):
|
||||
account = self.mastodon_client.me()
|
||||
self.my_id = account['id']
|
||||
|
||||
def fetch_following_accounts(self):
|
||||
print("Lade alle Accounts, denen Du folgst...\n")
|
||||
accounts = self.mastodon_client.account_following(self.my_id)
|
||||
self.following.extend(accounts)
|
||||
while accounts:
|
||||
accounts = self.mastodon_client.fetch_next(accounts)
|
||||
if accounts:
|
||||
self.following.extend(accounts)
|
||||
print(f"Du folgst insgesamt {len(self.following)} Accounts.\n")
|
||||
|
||||
def unfollow_inactive_users(self):
|
||||
now = datetime.datetime.now(datetime.timezone.utc)
|
||||
threshold_date = now - datetime.timedelta(days=self.grace_period_days)
|
||||
|
||||
for account in self.following:
|
||||
account_id = account['id']
|
||||
full_acct = account.get('acct', account.get('username', 'Unbekannt'))
|
||||
last_activity = None
|
||||
|
||||
if account.get('statuses_count', 0) > 0:
|
||||
try:
|
||||
statuses = self.mastodon_client.account_statuses(account_id, limit=1)
|
||||
if statuses:
|
||||
last_activity = statuses[0]['created_at']
|
||||
except Exception as e:
|
||||
print(f"Fehler beim Abruf der Status für {full_acct}: {e}")
|
||||
continue
|
||||
|
||||
if last_activity is None:
|
||||
last_activity = account.get('created_at', None)
|
||||
|
||||
if last_activity is None:
|
||||
print(f"Keine Aktivitätsdaten für {full_acct}. Überspringe diesen Account.")
|
||||
continue
|
||||
|
||||
if last_activity < threshold_date:
|
||||
try:
|
||||
self.mastodon_client.account_unfollow(account_id)
|
||||
self.unfollowed_users.append((full_acct, last_activity))
|
||||
print(f"Entfolgt: {full_acct} – Letzte Aktivität: {last_activity.date()}")
|
||||
except Exception as e:
|
||||
print(f"Fehler beim Entfolgen von {full_acct}: {e}")
|
||||
|
||||
def run(self):
|
||||
self.fetch_account_id()
|
||||
self.fetch_following_accounts()
|
||||
self.unfollow_inactive_users()
|
||||
if self.unfollowed_users:
|
||||
print("\nZusammenfassung der entfolgten Accounts:")
|
||||
for acct, last in self.unfollowed_users:
|
||||
print(f"- {acct}: Letzte Aktivität am {last.date()}")
|
||||
else:
|
||||
print("Es wurden keine Accounts gefunden, die seit mehr als "
|
||||
f"{self.grace_period_days} Tagen inaktiv sind.")
|
Loading…
Add table
Reference in a new issue