-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmastodon_bot.py
84 lines (61 loc) · 3 KB
/
mastodon_bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# EVERY FRAME IN ORDER BOT FOR TWITTER
# Original twitter bot by Pigeonburger, 2022
# https://github.com/pigeonburger
#
# Modified Mastodon bot by cfultz, 2023
# https://github.com/cfultz
#
import sqlite3 as db
import os
from mastodon import Mastodon
# Set up Mastodon
mastodon = Mastodon(
access_token = 'token.secret',
api_base_url = 'https://botsin.space'
)
# Connect to the SQLite database that was created in setupbot.py
connection = db.connect("framebot.db")
cursor = connection.cursor()
# Enter the name of the show you're posting frames for here:
show_name = ""
# Put the number of frames the bot should post each time this script is run here.
iters = 5
while iters > 0:
# Get the current season & episode the bot is currently on.
current_ep = cursor.execute("SELECT current_episode FROM bot").fetchone()[0]
# Get season and episode number
ep_season, ep_num = current_ep.split('x')
# Get the total frames from that episode.
total_frames = cursor.execute(f"SELECT frames FROM show WHERE ep = \"{current_ep}\"").fetchone()[0]
# Get the number of the last frame that was uploaded, then add one to that number to get the number of the next frame to post.
next_frame = cursor.execute("SELECT last_frame FROM bot").fetchone()[0] + 1
# If the next_frame number is bigger than the number of total frames in the episode, then the current_episode db value needs to be updated.
if next_frame > total_frames:
next_ep = str(int(ep_num)+1).zfill(2)
# If another episode in the same season is found, update the database to reflect that.
if os.path.isfile(f"./frames/S{ep_season}/{next_ep}x1.jpg"):
cursor.execute(f'UPDATE bot SET current_episode = "{ep_season}x{next_ep}"')
cursor.execute(f'UPDATE bot SET last_frame = 0')
connection.commit()
continue # var iters does not change, loop again
# Otherwise, we need to go to a new season.
else:
next_season = str(int(ep_season) + 1).zfill(2)
cursor.execute(f'UPDATE bot SET current_episode = "{next_season}x01"')
cursor.execute(f'UPDATE bot SET last_frame = 0')
connection.commit()
continue # var iters does not change, loop again
# Need to add a check here to see if the entire series is finished or not....
# Get the file path for the next frame to upload.
frame_path = f"./frames/S{ep_season}/{ep_num}x{next_frame}.jpg"
# The message to attach to the toot
msg = {show_name} - Season {ep_season} Episode {ep_num} - Frame {next_frame} of {total_frames}
# Send the toot
media = mastodon.media_post(frame_path)
mastodon.status_post(msg,media_ids=media)
# Update the database to reflect the most recently uploaded frame.
cursor.execute(f"UPDATE bot SET last_frame = {next_frame}")
connection.commit()
# That's one complete loop of the total number to upload finished.
iters -= 1
# Automatically exit the script when finished.