maston-publish.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import sys
  5. import subprocess
  6. from mastodon import Mastodon
  7. API_URL = 'https://botsin.space/'
  8. sys.stdout = open(sys.stdout.fileno(), mode='w', encoding='utf8', buffering=1)
  9. def getThoughtsAndPrayers():
  10. cmd = ['/cry-a-lot']
  11. process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  12. stdout, stderr = process.communicate()
  13. try:
  14. stdout = stdout.strip()
  15. stdout = stdout.decode('utf8')
  16. except:
  17. return 'uh-oh: something was wrong with the encoding of the prayer; try again'
  18. if process.returncode != 0:
  19. return 'something terrible is happening: exit code: %s, stderr: %s' % (
  20. process.returncode, stderr.decode('utf8'))
  21. if not stdout:
  22. return 'sadness: the Thoughts & Prayers bottle was empty; try again'
  23. return stdout
  24. def serve(token):
  25. tap = getThoughtsAndPrayers()
  26. print('my thoughts & prayers: %s' % tap)
  27. mastodon = Mastodon(access_token=token, api_base_url=API_URL)
  28. mastodon.status_post(tap)
  29. if __name__ == '__main__':
  30. if 'TAPBOT_TOKEN' not in os.environ:
  31. print("Please specify the Mastodon token in the TAPBOT_TOKEN environment variable")
  32. sys.exit(1)
  33. serve(token=os.environ['TAPBOT_TOKEN'])