From e8e1c03b084d43c5feaa88f64385e7cf70a34062 Mon Sep 17 00:00:00 2001 From: Roboron3042 Date: Mon, 24 Apr 2023 02:20:18 +0200 Subject: [PATCH] ADD: describot --- common.py | 20 +++++++++++++++++++- describot.py | 42 ++++++++++++++++++++++++++++++++++++++++++ federabot.py | 21 +++++++++++++++------ siguebot.py | 22 ++++++++++++++++++++++ 4 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 describot.py create mode 100644 siguebot.py diff --git a/common.py b/common.py index 319f617..f99297f 100644 --- a/common.py +++ b/common.py @@ -6,7 +6,8 @@ def get_api(url, token_name = ""): try: file = open('token/' + token_name, 'r') except FileNotFoundError: - sys.exit() + print('Token not found for ' + token_name) + exit() else: token = file.read().splitlines()[0] file.close() @@ -37,3 +38,20 @@ def list_append(name, value): file.write(value + '\n') file.close() +# It is not safe to get notifications from "last_id" because some may have been deleted +def get_new_notifications(api, bot_name, types=None): + last_notifications=list_read(bot_name + '_last_notifications') + notifications = api.notifications(types=types) + new_notifications = [] + new_notifications_ids = [] + + for i in range(0, len(notifications) // 2): + if str(notifications[i]['id']) not in last_notifications: + new_notifications.append(notifications[i]) + + for n in notifications: + new_notifications_ids.append(n['id']) + + list_write(bot_name + "_last_notifications", new_notifications_ids) + return new_notifications + diff --git a/describot.py b/describot.py new file mode 100644 index 0000000..1c4edbe --- /dev/null +++ b/describot.py @@ -0,0 +1,42 @@ +from common import get_api +from common import list_append +from common import list_read +from common import list_write +from common import get_new_notifications + +message = "¡Hola! He detectado que has publicado imágenes o vídeo sin texto alternativo. Añadir una descripción de texto alternativa a tus vídeos e imágenes es esencial para que las personas con alguna discapacidad visual puedan disfrutar de nuestras publicaciones. \n\n Por favor, considera añadir texto alternativo a tus publicaciones la próxima vez (o edita esta publicación para añadírselo). Si necesitas ayuda para saber cómo hacerlo, consulta la publicación fijada en mi perfil: https://masto.es/@TeLoDescribot/110249937862873987 \n\n ¡Gracias por hacer de este espacio un lugar más accesible para todos! \n\n Bip bop. Esta es una cuenta automatizada, si no quieres que te mencione más, eres libre de bloquearme." + + +bot_name = 'describot' +api_mastoes = get_api('masto.es', bot_name) +max_posts=20 +warned=[] + +def check_timeline(domain, api_external, timeline_name = 'local'): + last_ids = list_read(bot_name + "_" + domain + "_last_ids") + warned.extend(list_read(bot_name + "_" + domain)) + timeline = api_external.timeline(timeline=timeline_name, limit=max_posts) + new_last_ids=[] + for post in timeline: + new_last_ids.append(post['id']) + for i in range(0, len(timeline) - 2): + post = timeline[i] + if str(post['id']) not in last_ids and (str(post['account']['acct']) not in warned or timeline_name == 'home'): + for media in post['media_attachments']: + if media['description'] is None: + print('Warning ' + post['account']['acct']) + api_mastoes.status_reply(post, message, visibility="unlisted") + warned.append(post['account']['acct']) + if domain != 'home': + list_append(bot_name + "_" + domain, post['account']['acct']) + break + list_write(bot_name + "_" + domain + "_last_ids", new_last_ids) + +notifications = get_new_notifications(api_mastoes, bot_name, types=['follow']) +for n in notifications: + print("Following: " + n['account']['acct']) + api_mastoes.account_follow(n['account']['id']) + + +check_timeline('masto.es', api_mastoes) +check_timeline('home', api_mastoes, timeline_name='home') diff --git a/federabot.py b/federabot.py index c0e09b9..6b9bff9 100644 --- a/federabot.py +++ b/federabot.py @@ -49,7 +49,9 @@ excluded_domains = [ 'loa.masto.host', 'bizkaia.social', #'mstdn.mx', - 'federa.social' + 'federa.social', + # Non-spanish accounts >:( + 'sportsbots.xyz' ] bot_name = 'federabot' @@ -57,8 +59,9 @@ api_mastoes = get_api('masto.es', 'rober') following = list_read(bot_name) date_recent = datetime.today() - timedelta(days=7) -def check_timeline(domain, timeline_name = 'public'): - api_external = get_api(domain) +def check_timeline(domain, timeline_name = 'public', api_external=None): + if api_external is None: + api_external = get_api(domain) last_id = list_read(bot_name + "_last_id_" + domain)[0] timeline = api_external.timeline( timeline=timeline_name, @@ -80,7 +83,7 @@ def check_timeline(domain, timeline_name = 'public'): and username not in following ): date_created = post['account']['created_at'].replace(tzinfo=None) - if date_created > date_recent and user_domain == 'mastodon.social': + if date_created > date_recent and timeline_name == 'local' and user_domain == 'mastodon.social': print("New user: " + username) api_mastoes.status_post("@" + username + " " + get_message(user_domain), visibility="direct") print("Following: " + username) @@ -94,5 +97,11 @@ def check_timeline(domain, timeline_name = 'public'): if len(timeline) > 0: list_write(bot_name + "_last_id_" + domain, [timeline[0]['id']]) -check_timeline('masto.es') -check_timeline('mastodon.social', 'local') +api=get_api('masto.es', bot_name) +check_timeline('masto.es', api_external=api) + +api=get_api('mastodon.social', bot_name + "_mastodon_social") +check_timeline('mastodon.social', 'local', api_external=api) +check_timeline('mastodon.social', 'public', api_external=api) + + diff --git a/siguebot.py b/siguebot.py new file mode 100644 index 0000000..7e051c7 --- /dev/null +++ b/siguebot.py @@ -0,0 +1,22 @@ +import time +from common import get_api +from common import list_read + +api = get_api('masto.es', 'temp') +mi_id = api.me()['id'] +following = list_read('federabot') + +accounts = api.account_followers(mi_id) +size = len(accounts) + +while(size == 40): + for account in accounts: + if account['acct'] not in following: + print('Siguiendo a ' + account['acct']) + try: + api.account_follow(account['id']) + except Exception: + pass + accounts = api.fetch_next(accounts) + size = len(accounts) + time.sleep(10)