Mastobot Class refactor

master
spla 4 months ago
parent 4e2241d4ff
commit f1a8acd45e
  1. 22
      info.py
  2. 100
      mastobot.py

@ -10,6 +10,26 @@ if __name__ == '__main__':
for notif in notifications:
bot.get_data(notif)
if notif.type != 'mention':
print(f"Dismissing notification id {notif.id}")
bot.mastodon.notifications_dismiss(notif.id)
else:
mention = bot.get_data(notif)
if mention.reply:
bot.post(mention)
else:
print(f"Dismissing notification id {notif.id}")
bot.mastodon.notifications_dismiss(notif.id)

@ -7,6 +7,22 @@ import os
import sys
import os.path
###
# Dict helper class.
# Defined at top level so it can be pickled.
###
class AttribAccessDict(dict):
def __getattr__(self, attr):
if attr in self:
return self[attr]
else:
raise AttributeError("Attribute not found: " + str(attr))
def __setattr__(self, attr, val):
if attr in self:
raise AttributeError("Attribute-style access is read only")
super(AttribAccessDict, self).__setattr__(attr, val)
class Mastobot:
name = 'Mastobot'
@ -41,15 +57,7 @@ class Mastobot:
def get_data(self, notif):
notification_id = notif.id
if notif.type != 'mention':
print(f'dismissing notification {notification_id}')
self.mastodon.notifications_dismiss(notification_id)
return
id = notif.id
account_id = notif.account.id
@ -63,65 +71,73 @@ class Mastobot:
reply, question = self.get_question(self, text)
if reply:
mention_dict = {'reply': reply, 'question': question, 'id': id, 'account_id': account_id, 'username': username, 'status_id': status_id, 'text': text, 'visibility': visibility}
mau = self.mastodon.instance_nodeinfo().usage.users.activeMonth
mention = self.__json_allow_dict_attrs(mention_dict)
mau = '{:,}'.format(mau).replace(',','.')
registers = self.mastodon.instance().stats.user_count
return mention
registers = '{:,}'.format(registers).replace(',','.')
def post(self, mention):
posts = self.mastodon.instance().stats.status_count
mau = self.mastodon.instance_nodeinfo().usage.users.activeMonth
posts = '{:,}'.format(posts).replace(',','.')
mau = '{:,}'.format(mau).replace(',','.')
peers = self.mastodon.instance().stats.domain_count
peers = '{:,}'.format(peers).replace(',','.')
registers = self.mastodon.instance().stats.user_count
version = self.mastodon.instance().version
reg_open = self.mastodon.instance_nodeinfo().openRegistrations
registers = '{:,}'.format(registers).replace(',','.')
if reg_open:
posts = self.mastodon.instance().stats.status_count
opened = 'obert'
posts = '{:,}'.format(posts).replace(',','.')
else:
peers = self.mastodon.instance().stats.domain_count
opened = 'tancat'
peers = '{:,}'.format(peers).replace(',','.')
post_text = f"@{username}, dades de {self.mastodon_hostname}:\n\n"
version = self.mastodon.instance().version
post_text += f"Usuaris registrats: {registers}\n"
reg_open = self.mastodon.instance_nodeinfo().openRegistrations
post_text += f"Usuaris actius (mes): {mau}\n"
if reg_open:
post_text += f"Apunts: {posts}\n"
opened = 'obert'
post_text += f"Servidors federats: {peers}\n\n"
else:
post_text += f"Versió Mastodon: v{version}\n"
opened = 'tancat'
post_text += f"Registre: {opened}"
post_text = f"@{mention.username}, dades de {self.mastodon_hostname}:\n\n"
post_text = (post_text[:400] + '... ') if len(post_text) > 400 else post_text
post_text += f"Usuaris registrats: {registers}\n"
self.mastodon.status_post(post_text, in_reply_to_id=status_id,visibility=visibility)
post_text += f"Usuaris actius (mes): {mau}\n"
print(f'Replied notification {notification_id}')
post_text += f"Apunts: {posts}\n"
self.mastodon.notifications_dismiss(notification_id)
post_text += f"Servidors federats: {peers}\n\n"
else:
post_text += f"Versió Mastodon: v{version}\n"
print(f'dismissing notification {notification_id}')
post_text += f"Registre: {opened}"
self.mastodon.notifications_dismiss(notification_id)
post_text = (post_text[:400] + '... ') if len(post_text) > 400 else post_text
return
self.mastodon.status_post(post_text, in_reply_to_id=mention.status_id,visibility=mention.visibility)
print(f'Replied notification {mention.id}')
self.mastodon.notifications_dismiss(mention.id)
@staticmethod
def __json_allow_dict_attrs(json_object):
"""
Makes it possible to use attribute notation to access a dicts
elements, while still allowing the dict to act as a dict.
"""
if isinstance(json_object, dict):
return AttribAccessDict(json_object)
return json_object
@staticmethod
def get_question(self, text):

Loading…
Cancel
Save