2022-12-01 17:34:13 +01:00
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 ' :
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-01 17:34:13 +01:00
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 ' :
2022-12-02 17:46:07 +01:00
found_it , pending = db . check_user ( notif . account . acct )
2022-12-01 17:34:13 +01:00
2022-12-02 17:46:07 +01:00
if found_it and not pending and notif . status . in_reply_to_account_id == None :
2022-12-01 17:34:13 +01:00
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 )