masto.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from mastodon import Mastodon
  2. import re
  3. import os
  4. import time
  5. # Register your app! This only needs to be done once (per server, or when
  6. # distributing rather than hosting an application, most likely per device and server).
  7. # Uncomment the code and substitute in your information:
  8. '''
  9. Mastodon.create_app(
  10. 'grapeapp',
  11. api_base_url = 'https://YOUR_MASTODON_BASE_URL/',
  12. to_file = 'grapeapp_clientcred.secret'
  13. )
  14. '''
  15. mastodon = Mastodon(client_id = 'grapeapp_clientcred.secret',)
  16. mastodon.log_in(
  17. 'ENTER_YOUR_MASTODON_USERNAME',
  18. 'ENTER_YOUR_MASTODON_PASSWORD',
  19. to_file = 'grapeapp_usercred.secret'
  20. )
  21. mastodon = Mastodon(access_token = 'grapeapp_usercred.secret')
  22. mostRecentlyHandledMention = ''
  23. while True:
  24. mostRecentMention = mastodon.notifications(types = "mention")[0]
  25. if mostRecentMention.status.content == mostRecentlyHandledMention:
  26. time.sleep(15)
  27. continue
  28. mostRecentlyHandledMention = mostRecentMention.status.content
  29. if mostRecentMention.status.in_reply_to_id != None:
  30. time.sleep(15)
  31. continue
  32. mostRecentMentionContent = mostRecentMention.status.content
  33. cleanTags = re.compile('<.*?>')
  34. tweetWithoutTags = re.sub(cleanTags, '', mostRecentMentionContent)
  35. #put your bot's name and domain on the next line
  36. cleanMention = re.compile('@developer(@noc\.social)*')
  37. tweetWithoutMention = re.sub(cleanMention, '', tweetWithoutTags)
  38. cleanTweet = re.sub(r'[^a-zA-Z0-9 ]', '', tweetWithoutMention)
  39. rawInput = cleanTweet.strip()[:45]
  40. print(rawInput)
  41. os.system('python stable.py "' + rawInput + '"')
  42. cleanSpaces = re.compile(' ')
  43. folderName = re.sub(cleanSpaces, '_', rawInput)
  44. print(folderName)
  45. mentionerTag = '@' + mostRecentMention.status.account.acct
  46. print(mentionerTag)
  47. #put the file path for your stable diffusion output on the next line
  48. mediaId = mastodon.media_post(media_file = 'C:\\Users\\bigd\\Downloads\\stable-diffusion-main\\stable-diffusion-main\\outputs\\txt2img-samples\\' + folderName + '\\seed_28_00000.png', mime_type = 'image/png')
  49. message = mentionerTag + ' mentioned me and said: "' + tweetWithoutMention.strip() + '". Here\'s my dream about that!'
  50. mastodon.status_post(in_reply_to_id = mostRecentMention.status, status = message, media_ids = mediaId)