200 líneas
6,6 KiB
Python
200 líneas
6,6 KiB
Python
from mastodonbot import Mastodonbot
|
|
from mastodon import MastodonNetworkError
|
|
from database import Database
|
|
from datetime import datetime, time
|
|
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':
|
|
|
|
mention = bot.get_data(notif)
|
|
|
|
if mention.reply and '@' not in mention.acct:
|
|
|
|
if mention.question == 'alta':
|
|
|
|
found_it, pending = db.check_user(mention.acct)
|
|
|
|
if not found_it:
|
|
|
|
post = f"@{mention.acct} confirmes que ets major d'edat, que cedeixes els teus tuts públics que escriguis a partir d'ara al projecte Corpus "
|
|
|
|
post += "(https://git.mastodont.cat/spla/corpus) amb llicència CCO i que ets l'autor dels tuts que escrius (no els copies)?\n"
|
|
|
|
post += "Per a confirmar-ho tot, respon aquest tut amb:\n"
|
|
|
|
post += "@corpus confirmo"
|
|
|
|
post_id = bot.mastodon.status_post(post, in_reply_to_id=mention.status_id, visibility='direct').id
|
|
|
|
is_added = db.add_user(mention.acct, post_id)
|
|
|
|
if not is_added:
|
|
|
|
bot.mastodon.status_post(f'@{mention.acct}, error al desar.', in_reply_to_id=post_id, visibility='direct')
|
|
|
|
else:
|
|
|
|
if not pending:
|
|
|
|
bot.mastodon.status_post(f"@{mention.acct} ja has confirmat l'alta", in_reply_to_id=mention.status_id, visibility='direct')
|
|
|
|
else:
|
|
|
|
bot.mastodon.status_post(f"@{mention.acct} encara no has confirmat l'alta. Confirma-la responen sí a les preguntes de confirmació.", in_reply_to_id=mention.status_id, visibility='direct')
|
|
|
|
if mention.question == 'confirmo':
|
|
|
|
found_it, pending = db.check_user(mention.acct)
|
|
|
|
if found_it and pending:
|
|
|
|
is_confirmed = db.confirm_user(mention.acct)
|
|
|
|
if is_confirmed:
|
|
|
|
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')
|
|
|
|
elif mention.question == "esborra":
|
|
|
|
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:
|
|
|
|
are_deleted = db.del_user_posts(mention.acct)
|
|
|
|
if are_deleted:
|
|
|
|
post = f"@{mention.acct} tots els teus tus esborrats amb èxit!\n\n"
|
|
|
|
bot.mastodon.status_post(post, in_reply_to_id=mention.status_id, visibility='direct')
|
|
|
|
else:
|
|
|
|
bot.mastodon.status_post(f'@{mention.acct}, error al esborrar tots els tuts.', in_reply_to_id=mention.status_id, visibility='direct')
|
|
|
|
elif notif.type == 'status':
|
|
|
|
if notif.status != None:
|
|
|
|
if 'visibility' in notif.status:
|
|
|
|
if notif.status.visibility == 'public' and notif.status.language == 'ca':
|
|
|
|
found_it, pending = db.check_user(notif.account.acct)
|
|
|
|
if found_it and not pending 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)
|
|
|
|
now = datetime.now()
|
|
|
|
print(now.hour, now.minute)
|
|
|
|
if now.hour == 18 or now.hour == 0 or now.hour == 6 or now.hour == 12:
|
|
|
|
if now.minute == 0:
|
|
|
|
post = 'Vols ajudar fàcilment a desenvolupar i millorar les tecnologies de llengua per el #català?\n'
|
|
|
|
post += "Només cal demanar-me l'alta i començaré a enregistrar tots els teus tuts públics. "
|
|
|
|
post += "Els teus tuts públics podran alimentar projectes lingüistics com per exemple el de Common Voice de #Mozilla.\n"
|
|
|
|
post += "Com donar-te d'alta?\n\n@corpus alta"
|
|
|
|
print(post)
|
|
|
|
bot.mastodon.status_post(post, in_reply_to_id=None, visibility='public')
|
|
|
|
elif now.hour == 20:
|
|
|
|
if now.minute == 0:
|
|
|
|
total_users = db.total_users()
|
|
|
|
total_posts = db.total_posts()
|
|
|
|
post = f"\nUsuaris donats d'alta: {total_users}\n"
|
|
|
|
post += f'Missatges públics cedits: {total_posts}\n'
|
|
|
|
print(post)
|
|
|
|
bot.mastodon.status_post(post, in_reply_to_id=None, visibility='public')
|
|
|