Mastobot Class refactor

This commit is contained in:
spla 2022-08-06 10:49:37 +02:00
pare 4e2241d4ff
commit f1a8acd45e
S'han modificat 2 arxius amb 85 adicions i 49 eliminacions

22
info.py
Veure arxiu

@ -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)

Veure arxiu

@ -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"
post_text += f"Servidors federats: {peers}\n\n"
post_text += f"Versió Mastodon: v{version}\n"
post_text += f"Registre: {opened}"
post_text = (post_text[:400] + '... ') if len(post_text) > 400 else post_text
self.mastodon.status_post(post_text, in_reply_to_id=status_id,visibility=visibility)
print(f'Replied notification {notification_id}')
self.mastodon.notifications_dismiss(notification_id)
opened = 'obert'
else:
print(f'dismissing notification {notification_id}')
opened = 'tancat'
self.mastodon.notifications_dismiss(notification_id)
post_text = f"@{mention.username}, dades de {self.mastodon_hostname}:\n\n"
return
post_text += f"Usuaris registrats: {registers}\n"
post_text += f"Usuaris actius (mes): {mau}\n"
post_text += f"Apunts: {posts}\n"
post_text += f"Servidors federats: {peers}\n\n"
post_text += f"Versió Mastodon: v{version}\n"
post_text += f"Registre: {opened}"
post_text = (post_text[:400] + '... ') if len(post_text) > 400 else post_text
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):