CHG: Improve error handling

This commit is contained in:
Roboron3042 2023-08-18 22:21:03 +02:00
parent b2ab6c6355
commit c84a76b270
3 changed files with 92 additions and 53 deletions

View File

@ -1,5 +1,6 @@
from common import get_api from common import get_api
from common import list_read from common import list_read
from common import list_append
from common import list_write from common import list_write
# Messages # Messages
@ -9,28 +10,25 @@ message = "¡Hola! Soy Roberto, el administrador de este servidor de Mastodon :m
# Initialization # Initialization
bot_name = 'bienvenibot' bot_name = 'bienvenibot'
api = get_api('masto.es', 'rober') api = get_api('masto.es', 'rober')
last_ids = list_read(bot_name) users = list_read(bot_name)
new_last_ids=[] users_limited = list_read(bot_name + "_limited")
max_notifications=5 users_limited_new = []
notifications = api.notifications(limit=max_notifications) notifications = api.notifications(types=["admin.sign_up"])
#notifications = mastodon.notifications(types=["admin.sign_up"], limit=5)
def try_dm(user):
try:
api.status_post("@" + user + " " + message, visibility="direct")
except:
users_limited_new.append(user)
for user in users_limited:
try_dm(user)
for n in notifications: for n in notifications:
new_last_ids.append(n['id'])
# Some notifications may have been deleted since last fetch
# Therefore, it is better to check less than the maximum number of notifications
for i in range(0, max_notifications - 1):
n = notifications[i]
if str(n['id']) not in last_ids:
# Message new users # Message new users
username = n['account']['acct'] user = n['account']['acct']
if n['type'] == "admin.sign_up": if n['type'] == "admin.sign_up" and user not in users:
api.status_post("@" + username + " " + message, visibility="direct") list_append(bot_name, user)
# Follow any user who interacted with our account try_dm(user)
#elif not "@" in username:
elif n['type'] == "follow":
api.account_follow(n['account']['id'])
elif n['type'] == "admin.report":
api.status_post("@Roboron@im-in.space Informe recibido de " + username, visibility="direct")
list_write(bot_name, new_last_ids) list_write(bot_name + "_limited", users_limited_new)

View File

@ -14,7 +14,7 @@ def get_api(url, token_name = ""):
else: else:
token = "" token = ""
return Mastodon(access_token = token, api_base_url = url) return Mastodon(access_token = token, api_base_url = url, ratelimit_method='throw')
def list_read(name): def list_read(name):
try: try:
@ -22,11 +22,12 @@ def list_read(name):
except FileNotFoundError: except FileNotFoundError:
file = open('list/' + name, 'x') file = open('list/' + name, 'x')
file.close() file.close()
return [""] return []
else: else:
list = file.read().splitlines() list = file.read().splitlines()
file.close() file.close()
return list return list
def list_write(name, values): def list_write(name, values):
file = open('list/' + name, 'w') file = open('list/' + name, 'w')
for value in values: for value in values:

View File

@ -21,47 +21,73 @@ excluded_domains = [
'mastorol.es', 'mastorol.es',
'shrimply.social', 'shrimply.social',
'mstdn.jmiguel.eu', 'mstdn.jmiguel.eu',
# Relay chocoflan 'mastorock.com',
'mk.mistli.net', 'frikiverse.zone',
'izta.mistli.net',
'novoa.nagoya',
'quey.la',
'social.hispabot.freemyip.com',
'mastodon.uy',
'mstdn.mx',
'el-spot.xyz',
'mastodonperu.xyz',
'41020.social', '41020.social',
#'mast.lat', 'tuiter.rocks',
'acc4e.com', 'mastorock.com',
#'mastodon.com.py', 'meetiko.org',
#'shrimply.social', 'mastodon.cr',
'nonomastodon.redirectme.net',
'arguos.com',
'social.wikimedia.es',
'tarugo.ddns.net',
'pleroma.lyricaltokarev.fun',
# Relay nobigtech.es # Relay nobigtech.es
'sindicato.social', 'sindicato.social',
#'mastodon.uy', 'mastodon.uy',
'red.niboe.info', 'red.niboe.info',
'nobigtech.es', 'nobigtech.es',
'loa.masto.host', 'loa.masto.host',
'bizkaia.social', 'bizkaia.social',
#'mstdn.mx', 'mstdn.mx',
'federa.social', 'federa.social',
# Non-spanish accounts >:( # Non-spanish accounts >:(
'sportsbots.xyz' 'sportsbots.xyz',
'press.coop'
] ]
bot_name = 'federabot' bot_name = 'federabot'
api_mastoes = get_api('masto.es', 'rober') api_mastoes = get_api('masto.es', 'rober')
following = list_read(bot_name) following = list_read(bot_name)
date_recent = datetime.today() - timedelta(days=7) 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)
def check_timeline(domain, timeline_name = 'public', api_external=None): def check_timeline(domain, timeline_name = 'public', api_external=None):
if api_external is None: if api_external is None:
api_external = get_api(domain) api_external = get_api(domain)
last_id = list_read(bot_name + "_last_id_" + domain)[0] last_id = list_read(bot_name + "_last_id_" + domain)[0]
timeline = api_external.timeline( timeline = api_external.timeline(
timeline=timeline_name, timeline=timeline_name,
@ -84,24 +110,38 @@ def check_timeline(domain, timeline_name = 'public', api_external=None):
): ):
date_created = post['account']['created_at'].replace(tzinfo=None) date_created = post['account']['created_at'].replace(tzinfo=None)
if date_created > date_recent and timeline_name == 'local' and user_domain == 'mastodon.social': if date_created > date_recent and timeline_name == 'local' and user_domain == 'mastodon.social':
print("New user: " + username) try_dm(username, user_domain)
api_mastoes.status_post("@" + username + " " + get_message(user_domain), visibility="direct")
print("Following: " + username) print("Following: " + username)
user = api_mastoes.search_v2("@" + username + " ", result_type="accounts")["accounts"][0] user = api_mastoes.search_v2("@" + username + " ", result_type="accounts")["accounts"][0]
# Retrieve the post, it could be the first # Retrieve the post, it could be the first
api_mastoes.search_v2(post['url'], result_type="posts") api_mastoes.search_v2(post['url'], result_type="posts")
api_mastoes.account_follow(user['id'])
following.append(username) following.append(username)
list_append(bot_name, username) list_append(bot_name, username)
try_follow(user['id'])
if len(timeline) > 0: if len(timeline) > 0:
list_write(bot_name + "_last_id_" + domain, [timeline[0]['id']]) list_write(bot_name + "_last_id_" + domain, [timeline[0]['id']])
api=get_api('masto.es', bot_name)
check_timeline('masto.es', api_external=api) print('\nChecking 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('\nChecking follows...')
check_follows()
api=get_api('mastodon.social', bot_name + "_mastodon_social") api=get_api('mastodon.social', bot_name + "_mastodon_social")
print('\nChecking mastodon.social local TL')
check_timeline('mastodon.social', 'local', api_external=api) check_timeline('mastodon.social', 'local', api_external=api)
print('\nChecking mastodon.social federated TL')
check_timeline('mastodon.social', 'public', api_external=api) check_timeline('mastodon.social', 'public', api_external=api)
print('\nChecking masto.es federated TL')
api=get_api('masto.es', bot_name)
check_timeline('masto.es', api_external=api)