Advertisement
Guest User

Homestuck Bot Script

a guest
Apr 11th, 2024
3,588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. from mastodon import Mastodon
  2. import requests
  3. from PIL import Image, ImageSequence
  4. from io import BytesIO
  5. import time
  6. import os
  7. from shutil import move
  8. from tempfile import TemporaryFile, NamedTemporaryFile
  9.  
  10. mastodon = Mastodon(
  11. access_token = '(redacted)',
  12. api_base_url = 'https://botsin.space/'
  13. )
  14.  
  15. def post_img(fname):
  16. if '|' in fname:
  17. alttext, fname = fname.split(sep='|')
  18. else:
  19. alttext = ''
  20. ext = fname[-4:]
  21. if ext == ".gif":
  22. im = Image.open(fname)
  23. if im.size[0] * im.size[1] >= 1280 * 720:
  24. tf = TemporaryFile()
  25. frame = im.copy()
  26. frame.save(tf, format="png")
  27. while True:
  28. try:
  29. tf.seek(0)
  30. m = mastodon.media_post(tf.read(), mime_type="png", description=alttext)
  31. time.sleep(15)
  32. mastodon.status_post("", media_ids = m)
  33. tf.close()
  34. im.close()
  35. return 0
  36. except:
  37. time.sleep(5)
  38. continue
  39. if im.is_animated:
  40. frames = []
  41. durations = []
  42. for frame in ImageSequence.Iterator(im):
  43. frames.append(frame.copy())
  44. durations.append(frame.info['duration'])
  45. k = len(frames)
  46. for _ in range(10):
  47. for i in range(k):
  48. frames.append(frames[i].copy())
  49. durations.append(durations[i])
  50. tf = TemporaryFile()
  51. frames[0].save(tf, format="gif", save_all=True, append_images=frames[1:], duration=durations)
  52. while True:
  53. try:
  54. tf.seek(0)
  55. m = mastodon.media_post(tf.read(), mime_type="gif", description=alttext)
  56. time.sleep(15)
  57. mastodon.status_post("", media_ids = m)
  58. tf.close()
  59. im.close()
  60. return 0
  61. except:
  62. time.sleep(5)
  63. continue
  64. while True:
  65. try:
  66. m = mastodon.media_post(fname, description=alttext)
  67. time.sleep(15)
  68. mastodon.status_post("", media_ids = m)
  69. return 0
  70. except:
  71. time.sleep(5)
  72. continue
  73.  
  74. def post_text(s):
  75. time.sleep(15)
  76. if len(s) > 500: # max
  77. return -1
  78. while True:
  79. try:
  80. mastodon.status_post(s)
  81. return 0
  82. except:
  83. time.sleep(5)
  84. continue
  85.  
  86. def job():
  87. file_path = "botdata.txt"
  88. # Fetch first line of file, then delete it
  89. f_in = open(file_path, 'r')
  90. f_out = NamedTemporaryFile(mode='w', delete=False)
  91. temp_path = f_out.name
  92. firstLine = f_in.readline().strip()
  93. if len(firstLine) >= 4 and firstLine[-4:] in [".gif", ".jpg", ".png"]:
  94. ret = post_img(firstLine)
  95. else:
  96. ret = post_text(firstLine)
  97. if ret != 0:
  98. err_msg = "@Paravellex@shrike.club I broke! Reboot me! Line was: " + firstLine
  99. if len(err_msg) > 500:
  100. err_msg = err_msg[:499]
  101. mastodon.status_post(err_msg, visibility="direct")
  102. raise RuntimeError
  103. for line in f_in:
  104. f_out.write(line)
  105. f_in.close()
  106. f_out.close()
  107. os.remove(file_path)
  108. move(temp_path, file_path)
  109.  
  110. halfhours = (time.time() - 15) // 1800
  111. while True:
  112. time.sleep(1)
  113. if ((time.time() - 15) // 1800) > halfhours:
  114. job()
  115. halfhours += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement