60 lines
1.7 KiB
Python
60 lines
1.7 KiB
Python
"""
|
|
Runtime for the bot :)
|
|
"""
|
|
|
|
from mastodon import Mastodon, StreamListener
|
|
|
|
from paperbot import Config
|
|
from paperbot.logger import init_logger
|
|
|
|
class UserListener(StreamListener):
|
|
def __init__(self, client:Mastodon, config:Config):
|
|
self.client = client
|
|
self.config = config
|
|
self.logger = init_logger(name="user-listener", basedir=config.LOGDIR)
|
|
|
|
|
|
def on_update(self, status):
|
|
"""On status..."""
|
|
pass
|
|
|
|
def on_notification(self, notif):
|
|
pass
|
|
|
|
def on_conversation(self, convo):
|
|
"""On dm..."""
|
|
pass
|
|
|
|
def on_unknown_event(self, event):
|
|
"""#TODO: log this.."""
|
|
pass
|
|
|
|
def follow_back(self, notification):
|
|
self.client.account_follow(notification['account']['id'])
|
|
self.logger.debug(f'account followed! {notification["account"]["url"]} with id {notification["account"]["id"]}')
|
|
|
|
class Paperbot:
|
|
def __init__(self, config:Optional[Config]=None, post_length=500):
|
|
self._me = None # type: Optional[Account]
|
|
|
|
if config is None:
|
|
config = Config()
|
|
|
|
self.config = config
|
|
self.config.LOGDIR.mkdir(exist_ok=True)
|
|
self.post_length = post_length
|
|
self.logger = init_logger('paperbot', basedir=self.config.LOGDIR)
|
|
|
|
self.client = Mastodon(
|
|
access_token=self.config.MASTO_TOKEN,
|
|
api_base_url=self.config.MASTO_URL
|
|
)
|
|
|
|
def init_stream(self, run_async:bool=True):
|
|
# Listen to a stream consisting of just us.
|
|
listener = UserListener(self.client, self.config)
|
|
self.logger.info('Initializing streaming')
|
|
self.client.stream_user(
|
|
listener = listener,
|
|
run_async=run_async
|
|
) |