2023-04-23 11:47:16 +00:00
from common import get_api
from common import list_append
from common import list_read
from common import list_write
from datetime import datetime , timedelta
def get_message ( user_domain ) :
2023-11-13 17:05:28 +00:00
return " ¡Hola! Veo que es tu primera vez en Mastodon, ¡te doy la bienvenida! \n \n Te recomiendo que empieces escribiendo una publicación con la etiqueta #presentación y tus intereses para darte a conocer. \n \n ¡Espero que tengas un buen comienzo! Si necesitas ayuda, ¡cuenta conmigo! "
2023-04-23 11:47:16 +00:00
excluded_domains = [
' masto.es ' ,
# Relay tkz.one
' mst.universoalterno.es ' ,
' masto.friki.lol ' ,
' mastodon.com.py ' ,
' comunidad.nvda.es ' ,
' mast.lat ' ,
' viajes.social ' ,
' ferrocarril.net ' ,
' tkz.one ' ,
' mastorol.es ' ,
' shrimply.social ' ,
' mstdn.jmiguel.eu ' ,
2023-08-18 20:21:03 +00:00
' mastorock.com ' ,
' frikiverse.zone ' ,
2023-04-23 11:47:16 +00:00
' 41020.social ' ,
2023-08-18 20:21:03 +00:00
' tuiter.rocks ' ,
' mastorock.com ' ,
' meetiko.org ' ,
' mastodon.cr ' ,
2023-11-13 17:05:28 +00:00
' fedi.lat ' ,
2023-04-23 11:47:16 +00:00
# Relay nobigtech.es
' sindicato.social ' ,
2023-08-18 20:21:03 +00:00
' mastodon.uy ' ,
2023-04-23 11:47:16 +00:00
' red.niboe.info ' ,
' nobigtech.es ' ,
' loa.masto.host ' ,
' bizkaia.social ' ,
2023-08-18 20:21:03 +00:00
' mstdn.mx ' ,
2023-04-24 00:20:18 +00:00
' federa.social ' ,
# Non-spanish accounts >:(
2023-08-18 20:21:03 +00:00
' sportsbots.xyz ' ,
2024-12-03 23:35:21 +00:00
' press.coop ' ,
' fedibird.com ' ,
# Mirrors
' respublicae.eu ' ,
' bots.fedi.cr '
2023-04-23 11:47:16 +00:00
]
bot_name = ' federabot '
api_mastoes = get_api ( ' masto.es ' , ' rober ' )
2023-08-18 20:21:03 +00:00
2023-04-23 11:47:16 +00:00
following = list_read ( bot_name )
2023-08-18 20:21:03 +00:00
date_recent = datetime . today ( ) - timedelta ( days = 30 )
follows_limited = list_read ( bot_name + ' _follows_limited ' )
dms_limited = list_read ( bot_name + ' _messages_limited ' )
list_write ( bot_name + " _follows_limited " , [ ] )
list_write ( bot_name + " _messages_limited " , [ ] )
def try_follow ( user_id ) :
try :
api_mastoes . account_follow ( user_id )
except :
list_append ( bot_name + ' _follows_limited ' , str ( user_id ) )
print ( " Fail to follow. Will retry next time " )
def try_dm ( username , user_domain ) :
try :
api_mastoes . status_post ( " @ " + username + " " + get_message ( user_domain ) , visibility = " direct " )
print ( " Welcome new user: " + username )
except :
list_append ( bot_name + ' _messages_limited ' , username )
print ( " Fail to welcome new user: " + username + " . Will retry next time " )
def check_follows ( ) :
notifications = api_mastoes . notifications ( types = [ " follow " ] )
for n in notifications :
username = n [ ' account ' ] [ ' acct ' ]
user_domain = username . split ( " @ " ) [ 1 ] if " @ " in username else " masto.es "
date_created = n [ ' account ' ] [ ' created_at ' ] . replace ( tzinfo = None )
if username not in following :
print ( " Following: " + username )
try_follow ( n [ ' account ' ] [ ' id ' ] )
following . append ( username )
list_append ( bot_name , username )
if date_created > date_recent and user_domain == ' mastodon.social ' :
try_dm ( username , user_domain )
2023-04-23 11:47:16 +00:00
2023-04-24 00:20:18 +00:00
def check_timeline ( domain , timeline_name = ' public ' , api_external = None ) :
2023-08-18 20:21:03 +00:00
2023-04-24 00:20:18 +00:00
if api_external is None :
api_external = get_api ( domain )
2023-08-18 20:21:03 +00:00
2023-04-23 11:47:16 +00:00
last_id = list_read ( bot_name + " _last_id_ " + domain ) [ 0 ]
timeline = api_external . timeline (
timeline = timeline_name ,
since_id = last_id ,
remote = ( True if timeline_name == ' public ' else False )
)
for post in timeline :
if timeline_name == ' local ' :
username = post [ ' account ' ] [ ' acct ' ] + " @ " + domain
user_domain = domain
else :
username = post [ ' account ' ] [ ' acct ' ]
user_domain = username . split ( " @ " ) [ 1 ]
if (
post [ ' language ' ] == ' es '
and not " nobot " in post [ ' account ' ] [ ' note ' ]
and user_domain not in excluded_domains
and username not in following
) :
date_created = post [ ' account ' ] [ ' created_at ' ] . replace ( tzinfo = None )
2023-04-24 00:20:18 +00:00
if date_created > date_recent and timeline_name == ' local ' and user_domain == ' mastodon.social ' :
2023-08-18 20:21:03 +00:00
try_dm ( username , user_domain )
2023-04-23 11:47:16 +00:00
print ( " Following: " + username )
user = api_mastoes . search_v2 ( " @ " + username + " " , result_type = " accounts " ) [ " accounts " ] [ 0 ]
# Retrieve the post, it could be the first
api_mastoes . search_v2 ( post [ ' url ' ] , result_type = " posts " )
following . append ( username )
list_append ( bot_name , username )
2023-08-18 20:21:03 +00:00
try_follow ( user [ ' id ' ] )
2023-04-23 11:47:16 +00:00
if len ( timeline ) > 0 :
list_write ( bot_name + " _last_id_ " + domain , [ timeline [ 0 ] [ ' id ' ] ] )
2023-08-18 20:21:03 +00:00
print ( ' \n Checking previous attempts... ' )
for user_id in follows_limited :
try_follow ( user_id )
for username in dms_limited :
user_domain = username . split ( " @ " ) [ 1 ]
try_dm ( username , user_domain )
print ( ' \n Checking follows... ' )
check_follows ( )
2023-04-24 00:20:18 +00:00
api = get_api ( ' mastodon.social ' , bot_name + " _mastodon_social " )
2023-08-18 20:21:03 +00:00
print ( ' \n Checking mastodon.social local TL ' )
2023-04-24 00:20:18 +00:00
check_timeline ( ' mastodon.social ' , ' local ' , api_external = api )
2023-08-18 20:21:03 +00:00
print ( ' \n Checking mastodon.social federated TL ' )
2023-04-24 00:20:18 +00:00
check_timeline ( ' mastodon.social ' , ' public ' , api_external = api )
2023-08-18 20:21:03 +00:00
print ( ' \n Checking masto.es federated TL ' )
api = get_api ( ' masto.es ' , bot_name )
check_timeline ( ' masto.es ' , api_external = api )