2022-12-01 17:34:13 +01:00
from mastodonbot import Mastodonbot
from mastodon import MastodonNetworkError
from database import Database
2022-12-16 11:57:21 +01:00
from datetime import datetime , time
2022-12-01 17:34:13 +01:00
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 :
2022-12-18 12:56:48 +01:00
if notif . type == ' mention ' :
2022-12-01 17:34:13 +01:00
mention = bot . get_data ( notif )
if mention . reply and ' @ ' not in mention . acct :
if mention . question == ' alta ' :
2022-12-02 17:46:07 +01:00
found_it , pending = db . check_user ( mention . acct )
2022-12-01 17:34:13 +01:00
if not found_it :
2022-12-02 17:46:07 +01:00
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 )
2022-12-01 17:34:13 +01:00
2022-12-02 17:46:07 +01:00
if is_confirmed :
2022-12-01 17:34:13 +01:00
post = f " @ { mention . acct } afegit amb èxit! \n \n D ' 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 \n Els 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 ' )
2022-12-01 19:32:59 +01:00
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 ' )
2022-12-16 11:57:21 +01:00
elif notif . type == ' status ' :
2022-12-01 17:34:13 +01:00
2022-12-18 12:56:48 +01:00
if notif . status != None :
2022-12-16 11:57:21 +01:00
2022-12-18 12:56:48 +01:00
if ' visibility ' in notif . status :
2022-12-16 11:57:21 +01:00
2022-12-18 12:56:48 +01:00
if notif . status . visibility == ' public ' and notif . status . language == ' ca ' :
2022-12-16 11:57:21 +01:00
2022-12-18 12:56:48 +01:00
found_it , pending = db . check_user ( notif . account . acct )
2022-12-16 11:57:21 +01:00
2022-12-18 12:56:48 +01:00
if found_it and not pending and notif . status . in_reply_to_account_id == None :
2022-12-16 11:57:21 +01:00
2022-12-18 12:56:48 +01:00
is_saved = db . save_post ( notif . account . acct , unescape ( cleanhtml ( notif . status . content ) ) )
2022-12-16 11:57:21 +01:00
2022-12-18 12:56:48 +01:00
print ( f " Dismissing notification id { notif . id } " )
2022-12-01 17:34:13 +01:00
2022-12-18 12:56:48 +01:00
bot . mastodon . notifications_dismiss ( notif . id )
2022-12-16 11:57:21 +01:00
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 " \n Usuaris 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 ' )