ADD: describot

This commit is contained in:
Roboron3042 2023-04-24 02:20:18 +02:00
parent 62f165d487
commit e8e1c03b08
4 changed files with 98 additions and 7 deletions

View File

@ -6,7 +6,8 @@ def get_api(url, token_name = ""):
try: try:
file = open('token/' + token_name, 'r') file = open('token/' + token_name, 'r')
except FileNotFoundError: except FileNotFoundError:
sys.exit() print('Token not found for ' + token_name)
exit()
else: else:
token = file.read().splitlines()[0] token = file.read().splitlines()[0]
file.close() file.close()
@ -37,3 +38,20 @@ def list_append(name, value):
file.write(value + '\n') file.write(value + '\n')
file.close() 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

42
describot.py Normal file
View File

@ -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')

View File

@ -49,7 +49,9 @@ excluded_domains = [
'loa.masto.host', 'loa.masto.host',
'bizkaia.social', 'bizkaia.social',
#'mstdn.mx', #'mstdn.mx',
'federa.social' 'federa.social',
# Non-spanish accounts >:(
'sportsbots.xyz'
] ]
bot_name = 'federabot' bot_name = 'federabot'
@ -57,8 +59,9 @@ 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=7)
def check_timeline(domain, timeline_name = 'public'): def check_timeline(domain, timeline_name = 'public', api_external=None):
api_external = get_api(domain) if api_external is None:
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,
@ -80,7 +83,7 @@ def check_timeline(domain, timeline_name = 'public'):
and username not in following and username not in following
): ):
date_created = post['account']['created_at'].replace(tzinfo=None) 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) print("New user: " + username)
api_mastoes.status_post("@" + username + " " + get_message(user_domain), visibility="direct") api_mastoes.status_post("@" + username + " " + get_message(user_domain), visibility="direct")
print("Following: " + username) print("Following: " + username)
@ -94,5 +97,11 @@ def check_timeline(domain, timeline_name = 'public'):
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']])
check_timeline('masto.es') api=get_api('masto.es', bot_name)
check_timeline('mastodon.social', 'local') 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)

22
siguebot.py Normal file
View File

@ -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)