mastodon-cthulhusay.py 797 B

123456789101112131415161718192021222324252627282930
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import sys
  5. import random
  6. import cthulhusay
  7. from mastodon import Mastodon
  8. API_URL = 'https://botsin.space/'
  9. sys.stdout = open(sys.stdout.fileno(), mode='w', encoding='utf8', buffering=1)
  10. def get_words_of_cthulhu():
  11. return cthulhusay.cthulhu_say(words=random.randint(1, 12))
  12. def serve(token):
  13. words_of_cthulhu = get_words_of_cthulhu()
  14. print('serving:\n%s' % words_of_cthulhu)
  15. mastodon = Mastodon(access_token=token, api_base_url=API_URL)
  16. mastodon.status_post(words_of_cthulhu)
  17. if __name__ == '__main__':
  18. if 'CTHULHUBOT_TOKEN' not in os.environ:
  19. print("Please specify the Mastodon token in the CTHULHUBOT_TOKEN environment variable")
  20. sys.exit(1)
  21. serve(token=os.environ['CTHULHUBOT_TOKEN'])