mastoes-bots/common.py

84 lines
2.4 KiB
Python
Raw Normal View History

2023-04-23 11:47:16 +00:00
from mastodon import Mastodon
from sys import exit
def get_api(url, token_name = ""):
if token_name:
try:
file = open('token/' + token_name, 'r')
except FileNotFoundError:
2023-04-24 00:20:18 +00:00
print('Token not found for ' + token_name)
exit()
2023-04-23 11:47:16 +00:00
else:
token = file.read().splitlines()[0]
file.close()
else:
token = ""
2023-08-18 20:21:03 +00:00
return Mastodon(access_token = token, api_base_url = url, ratelimit_method='throw')
2023-04-23 11:47:16 +00:00
def list_read(name):
try:
file = open('list/' + name, 'r')
except FileNotFoundError:
file = open('list/' + name, 'x')
file.close()
2023-08-18 20:21:03 +00:00
return []
2023-04-23 11:47:16 +00:00
else:
list = file.read().splitlines()
file.close()
return list
2023-08-18 20:21:03 +00:00
2023-04-23 11:47:16 +00:00
def list_write(name, values):
file = open('list/' + name, 'w')
for value in values:
file.write(str(value) + '\n')
file.close()
def list_append(name, value):
file = open('list/' + name, 'a')
file.write(value + '\n')
file.close()
2023-04-24 00:20:18 +00:00
# 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 = []
max_notification = 0
if len(notifications) < 2:
max_notification = len(notifications)
else:
max_notification = len(notifications) // 2
2023-04-24 00:20:18 +00:00
for i in range(0, max_notification):
2023-04-24 00:20:18 +00:00
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
2024-12-03 23:33:00 +00:00
women_pronouns = ["she","her","ella","illa"]
nb_pronouns = ["they","them","elle", "ille"]
def is_gender(pronouns, account):
for pronoun in pronouns:
2024-12-05 21:10:39 +00:00
if(pronoun in account.display_name.lower()):
2024-12-03 23:33:00 +00:00
return True
2024-12-05 21:10:39 +00:00
for field in account.fields:
if(pronoun in field.value.lower()):
2024-12-03 23:33:00 +00:00
return True
2024-12-05 21:10:39 +00:00
if(pronoun in account.note.lower()):
return True
2024-12-03 23:33:00 +00:00
return False
def get_gender(account):
if(is_gender(nb_pronouns, account)):
return 2
2024-12-05 21:10:39 +00:00
if(is_gender(women_pronouns,account)):
return 1
return 0