From 65c09345e4cc68fa616b0179d50e468c27d302d6 Mon Sep 17 00:00:00 2001 From: Paco Hope Date: Sun, 4 Dec 2022 11:28:05 -0500 Subject: [PATCH] Handle logging in better, added set_display_name --- tootapalooza/cli.py | 71 ++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 24 deletions(-) diff --git a/tootapalooza/cli.py b/tootapalooza/cli.py index 97951be..fc73225 100644 --- a/tootapalooza/cli.py +++ b/tootapalooza/cli.py @@ -8,9 +8,8 @@ __date__ = '25 November 2022' __version__ = '1.0' __copyright__ = 'Copyright © 2022 Paco Hope. See LICENSE for details.' -from mastodon import Mastodon +import mastodon import toml -import os import time import argparse import sys @@ -18,11 +17,12 @@ from pathlib import Path import random import re import logging +from tootapalooza.userdata.names import usernames args=None logger=None -class Tooter(Mastodon): +class Tooter(mastodon.Mastodon): credentials: dict = {} hostname : str = '' files : dict = {} @@ -37,14 +37,30 @@ class Tooter(Mastodon): self.displayname = cred_dict['name'] self.cred_file = f'.tootapalooza-usercred-{self.acct}.env' - super().__init__(client_id=self.client_id) + super().__init__(access_token = self.cred_file, + client_id=self.client_id, + user_agent = "tootapalooza") + + # Try a basic call to ensure we are logged in + try: + account = self.me() + except mastodon.errors.MastodonUnauthorizedError: + logger.warn(f"Warning {self.acct} not logged in. Logging in.") + self.revoke_access_token() + try: + self.log_in( + self.username, + self.password, + to_file=self.cred_file + ) + except Exception as e: + logger.critical( f"Failed to login as {self.acct}" ) + return None + # if logging in worked, this will work + account = self.me() + + self.id = account['id'] - self.log_in( - self.username, - self.password, - to_file=self.cred_file - ) - self.id = self.me()['id'] @classmethod def load_credentials(cls, file: str) -> None: as_dict = toml.load(file) @@ -280,23 +296,30 @@ class Tooter(Mastodon): logger.debug(f"{self.acct} report_random_local") return( 0 ) + def set_display_name(self) -> int: + newname = random.choice(usernames) + logger.debug(f"{self.acct} set_display_name to {newname}") + self.account_update_credentials(display_name=newname) + return( 0 ) + def random_interaction(self): """Choose one possible interaction according to the weights, and do it.""" interactions = { - self.reply_random_local: 4, - self.reply_random_home: 4, - self.reply_random_public: 4, - self.follow_random_local: 2, - self.unfollow_random: 1, - self.toot_plain_public: 1, - self.toot_tagged_public: 4, - self.toot_plain_unlisted: 1, - self.favourite_random_local: 2, - self.favourite_random_home: 2, - self.favourite_random_public: 2, - self.boost_random_local: 2, - self.boost_random_home: 2, - self.boost_random_public: 2, + self.reply_random_local: 0, + self.reply_random_home: 0, + self.reply_random_public: 0, + self.follow_random_local: 0, + self.unfollow_random: 0, + self.toot_plain_public: 0, + self.toot_tagged_public: 0, + self.toot_plain_unlisted: 0, + self.favourite_random_local: 0, + self.favourite_random_home: 0, + self.favourite_random_public: 0, + self.boost_random_local: 0, + self.boost_random_home: 0, + self.boost_random_public: 0, + self.set_display_name: 1, self.report_random_local: 0 } chosen = random.choices(population=list(interactions.keys()),