Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from mastodon import Mastodon
- import requests
- from PIL import Image, ImageSequence
- from io import BytesIO
- import time
- import os
- from shutil import move
- from tempfile import TemporaryFile, NamedTemporaryFile
- mastodon = Mastodon(
- access_token = '(redacted)',
- api_base_url = 'https://botsin.space/'
- )
- def post_img(fname):
- if '|' in fname:
- alttext, fname = fname.split(sep='|')
- else:
- alttext = ''
- ext = fname[-4:]
- if ext == ".gif":
- im = Image.open(fname)
- if im.size[0] * im.size[1] >= 1280 * 720:
- tf = TemporaryFile()
- frame = im.copy()
- frame.save(tf, format="png")
- while True:
- try:
- tf.seek(0)
- m = mastodon.media_post(tf.read(), mime_type="png", description=alttext)
- time.sleep(15)
- mastodon.status_post("", media_ids = m)
- tf.close()
- im.close()
- return 0
- except:
- time.sleep(5)
- continue
- if im.is_animated:
- frames = []
- durations = []
- for frame in ImageSequence.Iterator(im):
- frames.append(frame.copy())
- durations.append(frame.info['duration'])
- k = len(frames)
- for _ in range(10):
- for i in range(k):
- frames.append(frames[i].copy())
- durations.append(durations[i])
- tf = TemporaryFile()
- frames[0].save(tf, format="gif", save_all=True, append_images=frames[1:], duration=durations)
- while True:
- try:
- tf.seek(0)
- m = mastodon.media_post(tf.read(), mime_type="gif", description=alttext)
- time.sleep(15)
- mastodon.status_post("", media_ids = m)
- tf.close()
- im.close()
- return 0
- except:
- time.sleep(5)
- continue
- while True:
- try:
- m = mastodon.media_post(fname, description=alttext)
- time.sleep(15)
- mastodon.status_post("", media_ids = m)
- return 0
- except:
- time.sleep(5)
- continue
- def post_text(s):
- time.sleep(15)
- if len(s) > 500: # max
- return -1
- while True:
- try:
- mastodon.status_post(s)
- return 0
- except:
- time.sleep(5)
- continue
- def job():
- file_path = "botdata.txt"
- # Fetch first line of file, then delete it
- f_in = open(file_path, 'r')
- f_out = NamedTemporaryFile(mode='w', delete=False)
- temp_path = f_out.name
- firstLine = f_in.readline().strip()
- if len(firstLine) >= 4 and firstLine[-4:] in [".gif", ".jpg", ".png"]:
- ret = post_img(firstLine)
- else:
- ret = post_text(firstLine)
- if ret != 0:
- err_msg = "@Paravellex@shrike.club I broke! Reboot me! Line was: " + firstLine
- if len(err_msg) > 500:
- err_msg = err_msg[:499]
- mastodon.status_post(err_msg, visibility="direct")
- raise RuntimeError
- for line in f_in:
- f_out.write(line)
- f_in.close()
- f_out.close()
- os.remove(file_path)
- move(temp_path, file_path)
- halfhours = (time.time() - 15) // 1800
- while True:
- time.sleep(1)
- if ((time.time() - 15) // 1800) > halfhours:
- job()
- halfhours += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement