Advertisement
GanWeaving

postToBS

May 8th, 2023 (edited)
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.81 KB | None | 0 0
  1. import sys
  2. import os
  3. import io
  4. import curses
  5. from PIL import Image
  6. from atproto import Client
  7. from atproto import models
  8. from datetime import datetime
  9.  
  10. import curses
  11. import curses.ascii
  12. import sys
  13.  
  14. import curses
  15. import curses.ascii
  16. import sys
  17.  
  18. import curses
  19. import curses.ascii
  20. import sys
  21.  
  22. def edit_text_window(stdscr, prompt, initial_text):
  23.     curses.echo()
  24.     stdscr.addstr(0, 0, prompt)
  25.     stdscr.refresh()
  26.  
  27.     text = initial_text
  28.     while True:
  29.         stdscr.addstr(1, 0, f"Current text length: {len(text)} characters")
  30.         stdscr.addstr(2, 0, "Edit the text below to 300 characters or less (Press ENTER to finish):")
  31.         stdscr.addstr(3, 0, text)
  32.         stdscr.refresh()
  33.  
  34.         ch = stdscr.getch()
  35.         if ch == curses.ascii.CR:
  36.             break
  37.         elif ch == curses.ascii.BS or ch == 127:
  38.             if len(text) > 0:
  39.                 text = text[:-1]
  40.                 stdscr.addstr(3, 0, " " * (len(text) + 1))
  41.                 stdscr.move(3, 0)
  42.                 stdscr.addstr(3, 0, text)
  43.         else:
  44.             text += chr(ch)
  45.  
  46.     return text
  47.  
  48.  
  49. def edit_text(text):
  50.     return curses.wrapper(edit_text_window, "Warning: Text is too long. Please edit the text to 300 characters or less.", text)
  51.  
  52. def main():
  53.     client = Client()
  54.     client.login('username', 'password')
  55.  
  56.     while True:
  57.         post_type = input("Choose an option:\n(1) Post images\n(2) Post text\n(3) Exit\nYour choice: ")
  58.  
  59.         if post_type == '1':
  60.             images = []
  61.  
  62.             image_extensions = ('.jpg', '.jpeg', '.png')
  63.             image_files = [f for f in os.listdir('.') if f.endswith(image_extensions)][:4]
  64.  
  65.             max_size_kb = 976.56
  66.  
  67.             for image_file in image_files:
  68.                 img = Image.open(image_file)
  69.                 img_format = img.format
  70.  
  71.                 while True:
  72.                     img_data = io.BytesIO()
  73.                     img.save(img_data, format=img_format)
  74.                     img_data.seek(0)
  75.  
  76.                     if len(img_data.getvalue()) / 1024 <= max_size_kb:
  77.                         break
  78.  
  79.                     img.thumbnail((img.width * 0.9, img.height * 0.9), Image.LANCZOS)
  80.  
  81.                 img_data = img_data.getvalue()
  82.  
  83.                 alt_text = input(f"Enter the alt text for {image_file}: ")
  84.  
  85.                 upload = client.com.atproto.repo.upload_blob(img_data)
  86.                 images.append(models.AppBskyEmbedImages.Image(alt=alt_text, image=upload.blob))
  87.                 os.remove(image_file)
  88.  
  89.             if not images:
  90.                 print("No images found. Exiting.")
  91.                 return
  92.  
  93.             text = input("Enter the text for the post: ")
  94.  
  95.             while len(text) > 300:
  96.                 text = edit_text(text)
  97.  
  98.             embed = models.AppBskyEmbedImages.Main(images=images)
  99.  
  100.             client.com.atproto.repo.create_record(
  101.                 models.ComAtprotoRepoCreateRecord.Data(
  102.                     repo=client.me.did,
  103.                     collection='app.bsky.feed.post',
  104.                     record=models.AppBskyFeedPost.Main(
  105.                         createdAt=datetime.now().isoformat(), text=text, embed=embed
  106.                     ),
  107.                 )
  108.             )
  109.             print("Images posted on Bluesky! What else do you want to do?")
  110.         elif post_type == '2':
  111.             text = input("Enter the text for the post: ")
  112.             while len(text) > 300:
  113.                 text = edit_text(text)
  114.  
  115.             client.send_post(text=text)
  116.             print("Text posted on Bluesky! What else do you want to do?")
  117.         elif post_type == '3':
  118.             print("Exiting...")
  119.             sys.exit()
  120.         else:
  121.             print("Invalid input. Please try again.")
  122.  
  123. if __name__ == '__main__':
  124.     os.system('cls' if os.name == 'nt' else 'clear')
  125.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement