MLP Trixie Lulamoon bot for Mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

123 lines
4.4 KiB

import sys
import random
import datetime
import os.path as op
from pprint import pprint
import requests
from mastodon import Mastodon
# To avoid obvious repetitivity --------------------
def get_rand_int(l_int, rrang) -> int:
tmp = random.randint(0, rrang)
while tmp == l_int:
tmp = random.randint(0, rrang)
return tmp
# Compare time -------------------------------------
def is_actual(time_date_now, time_date_posted) -> bool:
time_date_posted = time_date_posted.replace('T', ' ')
# If not the same day:
if time_date_now.split()[0] != time_date_posted.split()[0]:
return False
time_grades_now = time_date_now.split()[1].split(':')
time_grades_posted = time_date_posted.split()[1].split(':')
# If different hours:
if time_grades_now[0] != time_grades_posted[0]:
return (time_grades_now[1] == '00' or time_grades_now[1] == '01'
and time_grades_posted[1] == '58' or time_grades_posted[1] == '59')
# If same hours, difference shouldn't be more than 3 minutes
return abs(int(time_grades_now[1]) - int(time_grades_posted[1])) < 3
# --------------------------------------------------
def main():
mastodon = Mastodon(
access_token = 'token.dat',
api_base_url = 'https://social.inex.rocks/'
)
# Quotes for mentions
if op.isfile('quotes.dat'):
with open('quotes.dat', 'r', encoding='utf-8') as file:
quotes = file.readlines()
else:
time = str(datetime.datetime.now().time())
print(time, ': Quotes disappeared!')
quotes = [ 'All my quotes are gone! Please, contact @NaiJi!' ]
# Emojis for each of her toot
if op.isfile('emojis.dat'):
with open('emojis.dat', 'r', encoding='utf-8') as file:
emojis = file.readlines()
else:
ttime = str(datetime.datetime.now().time())
print(ttime, ': Emojis disappeared!')
emojis = [ ':trixiehappy:' ]
# The latest mentioned user
if op.isfile('lastid.dat'):
with open('lastid.dat', 'r', encoding='utf-8') as file:
last_id = int(file.readline())
else:
ttime = str(datetime.datetime.now().time())
print(ttime, ': lastid.dat is missing. Creating.')
with open('lastid.dat', 'w', encoding='utf-8') as file:
last_id = -1
is_first = True
newest_id = last_id
last_emoji = ':?:'
last_quote = -1
# Iterating the last 5 mentions and answering on them until Trixie finds already mentioned id
try:
for entry in mastodon.notifications(limit=5):
if entry.get('type') == 'mention':
# if the toot is too old, ignore it!
if is_actual(str(datetime.datetime.now()), entry.get('created_at')):
continue
current_id = int(entry.get('id'))
visibility = entry.get('visibility')
if current_id != last_id:
username = entry.get('account').get('username')
last_emoji = get_rand_int(last_emoji, len(emojis) - 1)
last_quote = get_rand_int(last_quote, len(quotes) - 1)
mastodon.status_reply(entry.get('status'),
f'{quotes[last_quote][1:-2]} {emojis[last_emoji]}',
media_ids=None, visibility=visibility)
else:
if (is_first):
newest_id = current_id
break
# The first examined mention is the last written mention, so we must save its id
if (is_first):
is_first = False
newest_id = current_id
except KeyboardInterrupt:
sys.exit(1)
except:
ttime = str(datetime.datetime.now().time())
print(ttime, ': Error while mentioning!')
# The latest mentioned user. If it disappeared after posting, it's super weird!
if op.isfile('lastid.dat'):
with open('lastid.dat', 'w', encoding='utf-8') as file:
print(newest_id, end='', file=file)
else:
ttime = str(datetime.datetime.now().time())
print(ttime, ': lastid.dat is missing AFTER answering! Creating the file. Be aware of next Trixie\'s double mentioning.')
with open('lastid.dat', 'w', encoding='utf-8') as file:
last_id = -1
if __name__ == '__main__':
sys.exit(main())