2022-03-04 21:36:26 +01:00
from six . moves import urllib
from datetime import datetime , timedelta
from mastodon import Mastodon
import unidecode
import time
import re
import os
import time
import sys
import os . path
import wikipedia
import pdb
def cleanhtml ( raw_html ) :
cleanr = re . compile ( ' <.*?> ' )
cleantext = re . sub ( cleanr , ' ' , raw_html )
return cleantext
def unescape ( s ) :
s = s . replace ( " ' " , " ' " )
return s
def get_question ( text ) :
reply = False
keyword = ' '
content = cleanhtml ( text )
content = unescape ( content )
try :
start = content . index ( " @ " )
end = content . index ( " " )
if len ( content ) > end :
content = content [ 0 : start : ] + content [ end + 1 : : ]
cleanit = content . count ( ' @ ' )
i = 0
while i < cleanit :
start = content . rfind ( " @ " )
end = len ( content )
content = content [ 0 : start : ] + content [ end + 1 : : ]
i + = 1
content = content . lower ( )
question = content
wiki_lang = ' ca '
if wiki_lang == ' ca ' :
keyword_length = 8
keyword = ' consulta '
if unidecode . unidecode ( question ) [ 0 : keyword_length ] == keyword :
reply = True
wikipedia . set_lang ( wiki_lang )
start = 0
end = unidecode . unidecode ( question ) . index ( keyword , 0 )
if len ( question ) > end :
question = question [ 0 : start : ] + question [ end + keyword_length + 1 : : ]
except :
pass
return ( reply , question )
def log_in ( ) :
# Load secrets from secrets file
secrets_filepath = " secrets/secrets.txt "
uc_client_id = get_parameter ( " uc_client_id " , secrets_filepath )
uc_client_secret = get_parameter ( " uc_client_secret " , secrets_filepath )
uc_access_token = get_parameter ( " uc_access_token " , secrets_filepath )
# Load configuration from config file
config_filepath = " config/config.txt "
mastodon_hostname = get_parameter ( " mastodon_hostname " , config_filepath )
# Initialise Mastodon API
mastodon = Mastodon (
client_id = uc_client_id ,
client_secret = uc_client_secret ,
access_token = uc_access_token ,
api_base_url = ' https:// ' + mastodon_hostname ,
)
# Initialise access headers
headers = { ' Authorization ' : ' Bearer %s ' % uc_access_token }
return mastodon
def get_parameter ( parameter , file_path ) :
# Check if secrets file exists
if not os . path . isfile ( file_path ) :
print ( " File %s not found, exiting. " % file_path )
sys . exit ( 0 )
# Find parameter in file
with open ( file_path ) as f :
for line in f :
if line . startswith ( parameter ) :
return line . replace ( parameter + " : " , " " ) . strip ( )
# Cannot find parameter, exit
print ( file_path + " Missing parameter %s " % parameter )
sys . exit ( 0 )
###############################################################################
# main
if __name__ == ' __main__ ' :
mastodon = log_in ( )
bot_notifications = mastodon . notifications ( )
if len ( bot_notifications ) == 0 :
print ( ' No mentions ' )
sys . exit ( 0 )
i = 0
while i < len ( bot_notifications ) :
notification_id = bot_notifications [ i ] . id
if bot_notifications [ i ] . type != ' mention ' :
i + = 1
print ( f ' dismissing notification { notification_id } ' )
mastodon . notifications_dismiss ( notification_id )
continue
account_id = bot_notifications [ i ]
2022-03-08 11:32:23 +01:00
username = bot_notifications [ i ] . account . acct
2022-03-04 21:36:26 +01:00
status_id = bot_notifications [ i ] . status . id
text = bot_notifications [ i ] . status . content
visibility = bot_notifications [ i ] . status . visibility
reply , question = get_question ( text )
if reply :
try :
wiki_result = wikipedia . page ( question )
except wikipedia . exceptions . DisambiguationError as error :
wiki_result = wikipedia . search ( question , results = 10 )
ambiguous_reply = " \n - " . join ( str ( x ) for x in wiki_result )
2022-03-08 11:30:11 +01:00
toot_text = f " @ { username } ' { question } ' és una consulta ambigua. Potser et referies a: \n \n { ambiguous_reply } "
2022-03-04 21:36:26 +01:00
toot_text = ( toot_text [ : 400 ] + ' ... ' ) if len ( toot_text ) > 400 else toot_text
except wikipedia . exceptions . PageError as page_error :
2022-03-09 14:10:59 +01:00
toot_text = f " @ { username } l ' article ' { question } ' no existeix. Varia una mica la consulta o si comprobes que l ' article no existeix t ' animem a crear-lo. Som l ' enciclopèdia lliure que tu també pots editar. \n "
2022-03-04 21:36:26 +01:00
toot_text + = " :viqui: "
else :
2022-03-08 11:30:11 +01:00
toot_text = f ' @ { username } \n '
2022-03-04 21:36:26 +01:00
toot_text + = wikipedia . summary ( question , sentences = 2 ) + " \n "
toot_text = ( toot_text [ : 400 ] + ' ... ' ) if len ( toot_text ) > 400 else toot_text
toot_text + = f ' { str ( wiki_result . url ) } \n '
toot_text + = " :viqui: "
mastodon . status_post ( toot_text , in_reply_to_id = status_id , visibility = visibility )
print ( f ' Replied notification { notification_id } ' )
mastodon . notifications_dismiss ( notification_id )
else :
print ( f ' dismissing notification { notification_id } ' )
mastodon . notifications_dismiss ( notification_id )
i + = 1