Mastobot Class refactor
This commit is contained in:
pare
4e2241d4ff
commit
f1a8acd45e
S'han modificat 2 arxius amb 85 adicions i 49 eliminacions
22
info.py
22
info.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)
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
112
mastobot.py
112
mastobot.py
|
@ -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(',','.')
|
||||
return mention
|
||||
|
||||
registers = self.mastodon.instance().stats.user_count
|
||||
def post(self, mention):
|
||||
|
||||
registers = '{:,}'.format(registers).replace(',','.')
|
||||
mau = self.mastodon.instance_nodeinfo().usage.users.activeMonth
|
||||
|
||||
posts = self.mastodon.instance().stats.status_count
|
||||
mau = '{:,}'.format(mau).replace(',','.')
|
||||
|
||||
posts = '{:,}'.format(posts).replace(',','.')
|
||||
registers = self.mastodon.instance().stats.user_count
|
||||
|
||||
peers = self.mastodon.instance().stats.domain_count
|
||||
registers = '{:,}'.format(registers).replace(',','.')
|
||||
|
||||
peers = '{:,}'.format(peers).replace(',','.')
|
||||
posts = self.mastodon.instance().stats.status_count
|
||||
|
||||
version = self.mastodon.instance().version
|
||||
posts = '{:,}'.format(posts).replace(',','.')
|
||||
|
||||
reg_open = self.mastodon.instance_nodeinfo().openRegistrations
|
||||
peers = self.mastodon.instance().stats.domain_count
|
||||
|
||||
if reg_open:
|
||||
peers = '{:,}'.format(peers).replace(',','.')
|
||||
|
||||
opened = 'obert'
|
||||
version = self.mastodon.instance().version
|
||||
|
||||
else:
|
||||
reg_open = self.mastodon.instance_nodeinfo().openRegistrations
|
||||
|
||||
opened = 'tancat'
|
||||
if reg_open:
|
||||
|
||||
post_text = f"@{username}, dades de {self.mastodon_hostname}:\n\n"
|
||||
|
||||
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=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):
|
||||
|
|
Loading…
Referencia en una nova incidència