masto.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. mostRecentMentionRepliedTo = ''
  23. while True:
  24. mostRecentMention = mastodon.notifications(types = "mention")[0].status.content
  25. if mostRecentMention == mostRecentMentionRepliedTo:
  26. time.sleep(15)
  27. continue
  28. mostRecentMentionRepliedTo = mostRecentMention
  29. cleanTags = re.compile('<.*?>')
  30. tweetWithoutTags = re.sub(cleanTags, '', mostRecentMention)
  31. #put your bot's name and domain on the next line
  32. cleanMention = re.compile('@developer(@noc\.social)*')
  33. tweetWithoutMention = re.sub(cleanMention, '', tweetWithoutTags)
  34. cleanTweet = re.sub(r'[^a-zA-Z0-9 ]', '', tweetWithoutMention)
  35. rawInput = cleanTweet.strip()[:45]
  36. print(rawInput)
  37. os.system('python stable.py "' + rawInput + '"')
  38. cleanSpaces = re.compile(' ')
  39. folderName = re.sub(cleanSpaces, '_', rawInput)
  40. print(folderName)
  41. mentionerUrl = mastodon.notifications(types = "mention")[0].status.account.url
  42. cleanProtocol = re.compile('https*:\/\/')
  43. cleanedUrl = re.sub(cleanProtocol, '', mentionerUrl)
  44. urlParts = cleanedUrl.split("/")
  45. mentionTag = urlParts[1] + '@' + urlParts[0]
  46. print(mentionTag)
  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 = mentionTag + ' mentioned me and said: "' + tweetWithoutMention.strip() + '". Here\'s my dream about that!'
  50. mastodon.status_post(status = message, media_ids = mediaId)