mastrep.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/python3
  2. from randstrip import createStrip
  3. from mastodon import Mastodon, StreamListener
  4. from mastodon_main import publishStrip
  5. import json
  6. import os
  7. import sys
  8. import time
  9. fileDir = os.path.dirname(os.path.abspath(__file__))
  10. fileDir = fileDir + "/"
  11. API_URL = "https://botsin.space"
  12. class stripListener(StreamListener):
  13. def on_notification(self, notification):
  14. try:
  15. account = "@"+notification["account"]["acct"]
  16. content = notification["status"]["content"]
  17. if content.find("help") != -1:
  18. mastodon.status_post("Hello "+account+" just send me a message with 'new strip' and the desired profile. Try with 'oloturia' (Italian), 'oloeng' (broken English) or 'olofra' (French). If not specified, Italian will be selected as default.",visibility="direct")
  19. elif content.find("new strip") != -1:
  20. if content.find("oloeng") != -1:
  21. profile = "oloeng"
  22. elif content.find("olofra") != -1:
  23. profile = "olofra"
  24. else:
  25. profile = "oloturia"
  26. publishStrip([profile],account)
  27. except KeyError:
  28. return
  29. if __name__ == "__main__":
  30. with open(fileDir+"/config.json") as f:
  31. config = json.load(f)
  32. with open(fileDir+config["mastodonListenerToken"]) as f:
  33. createapp = f.readlines()
  34. createapp = [x.strip() for x in createapp]
  35. TOKEN = createapp[0]
  36. mastodon = Mastodon(access_token = TOKEN,api_base_url = API_URL)
  37. listener = stripListener()
  38. while True:
  39. try:
  40. mastodon.stream_user(listener)
  41. except:
  42. time.sleep(10)