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)
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
58
mastobot.py
58
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,7 +71,13 @@ 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}
|
||||
|
||||
mention = self.__json_allow_dict_attrs(mention_dict)
|
||||
|
||||
return mention
|
||||
|
||||
def post(self, mention):
|
||||
|
||||
mau = self.mastodon.instance_nodeinfo().usage.users.activeMonth
|
||||
|
||||
|
@ -93,7 +107,7 @@ class Mastobot:
|
|||
|
||||
opened = 'tancat'
|
||||
|
||||
post_text = f"@{username}, dades de {self.mastodon_hostname}:\n\n"
|
||||
post_text = f"@{mention.username}, dades de {self.mastodon_hostname}:\n\n"
|
||||
|
||||
post_text += f"Usuaris registrats: {registers}\n"
|
||||
|
||||
|
@ -109,19 +123,21 @@ class Mastobot:
|
|||
|
||||
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)
|
||||
self.mastodon.status_post(post_text, in_reply_to_id=mention.status_id,visibility=mention.visibility)
|
||||
|
||||
print(f'Replied notification {notification_id}')
|
||||
print(f'Replied notification {mention.id}')
|
||||
|
||||
self.mastodon.notifications_dismiss(notification_id)
|
||||
self.mastodon.notifications_dismiss(mention.id)
|
||||
|
||||
else:
|
||||
|
||||
print(f'dismissing notification {notification_id}')
|
||||
|
||||
self.mastodon.notifications_dismiss(notification_id)
|
||||
|
||||
return
|
||||
@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