118 líneas
3,5 KiB
Python
118 líneas
3,5 KiB
Python
from mastodonbot import Mastodonbot
|
|
from mastodon import MastodonNetworkError
|
|
from database import Database
|
|
import sys
|
|
import re
|
|
import pdb
|
|
|
|
def cleanhtml(raw_html):
|
|
|
|
cleanr = re.compile('<.*?>')
|
|
cleantext = re.sub(cleanr, '', raw_html)
|
|
return cleantext
|
|
|
|
def unescape(s):
|
|
|
|
s = s.replace("'", "'")
|
|
s = s.replace("'", "'")
|
|
return s
|
|
# main
|
|
|
|
if __name__ == '__main__':
|
|
|
|
bot = Mastodonbot()
|
|
|
|
db = Database()
|
|
|
|
try:
|
|
|
|
notifications = bot.mastodon.notifications()
|
|
|
|
except MastodonNetworkError as net_error:
|
|
|
|
sys.exit(net_error)
|
|
|
|
for notif in notifications:
|
|
|
|
if notif.type != 'mention' and notif.type != 'status':
|
|
|
|
print(f"Dismissing notification id {notif.id}")
|
|
|
|
bot.mastodon.notifications_dismiss(notif.id)
|
|
|
|
elif notif.type == 'mention':
|
|
|
|
mention = bot.get_data(notif)
|
|
|
|
if mention.reply and '@' not in mention.acct:
|
|
|
|
if mention.question == 'alta':
|
|
|
|
found_it = db.check_user(mention.acct)
|
|
|
|
if not found_it:
|
|
|
|
is_added = db.add_user(mention.acct)
|
|
|
|
if is_added:
|
|
|
|
post = f"@{mention.acct} afegit amb èxit!\n\nD'ara endavant tots els teus tuts públics seràn desats en la base de dades.\n"
|
|
|
|
bot.mastodon.status_post(post, in_reply_to_id=mention.status_id, visibility='direct')
|
|
|
|
bot.mastodon.account_follow(notif.account.id, reblogs=False, notify=True)
|
|
|
|
else:
|
|
|
|
bot.mastodon.status_post(f'@{mention.acct}, error al desar.', in_reply_to_id=mention.status_id, visibility='direct')
|
|
|
|
else:
|
|
|
|
bot.mastodon.status_post(f"@{mention.acct} ja estàs donat d'alta", in_reply_to_id=mention.status_id, visibility='direct')
|
|
|
|
elif mention.question == "baixa":
|
|
|
|
found_it = db.check_user(mention.acct)
|
|
|
|
if not found_it:
|
|
|
|
bot.mastodon.status_post(f"@{mention.acct} no estàs donat d'alta", in_reply_to_id=mention.status_id, visibility='direct')
|
|
|
|
else:
|
|
|
|
is_deleted = db.del_user(mention.acct)
|
|
|
|
if is_deleted:
|
|
|
|
post = f"@{mention.acct} esborrat amb èxit!\n\nEls teus tuts públics ja no seràn desats en la base de dades.\n"
|
|
|
|
bot.mastodon.status_post(post, in_reply_to_id=mention.status_id, visibility='direct')
|
|
|
|
bot.mastodon.account_unfollow(notif.account.id)
|
|
|
|
else:
|
|
|
|
bot.mastodon.status_post(f'@{mention.acct}, error al esborrar.', in_reply_to_id=mention.status_id, visibility='direct')
|
|
|
|
print(f"Dismissing notification id {mention.id}")
|
|
|
|
bot.mastodon.notifications_dismiss(mention.id)
|
|
|
|
else:
|
|
|
|
print(f"Dismissing notification id {mention.id}")
|
|
|
|
bot.mastodon.notifications_dismiss(mention.id)
|
|
|
|
elif notif.type == 'status' and notif.status.visibility == 'public' and notif.status.language == 'ca':
|
|
|
|
found_it = db.check_user(notif.account.acct)
|
|
|
|
if found_it and notif.status.in_reply_to_account_id == None:
|
|
|
|
is_saved = db.save_post(notif.account.acct, unescape(cleanhtml(notif.status.content)))
|
|
|
|
print(f"Dismissing notification id {notif.id}")
|
|
|
|
bot.mastodon.notifications_dismiss(notif.id)
|