Curious Cat to Mastodon crossposter
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

run.py 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python3
  2. #Curious Greg - Curious Cat to Mastodon crossposter
  3. # This Source Code Form is subject to the terms of the Mozilla Public
  4. # License, v. 2.0. If a copy of the MPL was not distributed with this
  5. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. import json, time
  7. from mastodon import Mastodon
  8. import requests
  9. import mysql.connector
  10. cfg = json.load(open('meta.json'))
  11. db = mysql.connector.connect(user=cfg['dbuser'], password=cfg['dbpass'], database=cfg['dbname'])
  12. c = db.cursor()
  13. dc = db.cursor(dictionary=True)
  14. dc.execute("SELECT * FROM data")
  15. for row in dc.fetchall():
  16. print(row['cc'])
  17. settings = json.loads(row['settings'])
  18. t = int(time.time())
  19. next_check = row['last_check'] + row['time_between_checks'] * 60
  20. print("current time: {} waiting for: {}".format(t, next_check))
  21. if next_check <= t:
  22. row['time_between_checks'] = cfg['min_time_between_checks']
  23. #time to check
  24. if row['cc'] != "None" and row['cc'] != None:
  25. r = requests.get("https://curiouscat.me/api/v2/profile?username={}&count=100&min_timestamp={}".format(row['cc'], row['latest_post']))
  26. j = r.json()
  27. if 'error' in j:
  28. continue
  29. posted = False
  30. if len(j['posts']) > 0 and not (len(j['posts']) == 1 and int(j['posts'][0]['timestamp']) == int(row['latest_post'])):
  31. #they've made some new posts, log in to masto
  32. try:
  33. client = Mastodon(client_id=row['client_id'], client_secret=row['client_secret'], access_token=row['secret'], api_base_url=row['instance'])
  34. except:
  35. continue
  36. for post in j['posts']:
  37. if post['senderData']['id'] == False:
  38. sender = "(anonymous)"
  39. else:
  40. sender = post['senderData']['username']
  41. if int(post['timestamp']) <= int(row['latest_post']):
  42. #this is the one we've already seen
  43. continue
  44. #if we get to this point, they've definitely made a post
  45. posted = True
  46. toot = "Curious Cat user {} asks: {}\n\nMy answer: {}\n\nhttps://curiouscat.me/{}/post/{}".format(sender, post['comment'], post['reply'], row['cc'], post['id']) #TODO: what if this is over 500 characters?
  47. try:
  48. if settings['cw']:
  49. client.status_post(toot, spoiler_text="Curious Cat post")
  50. else:
  51. client.status_post(toot)
  52. except:
  53. continue
  54. c.execute("UPDATE data SET last_check = %s, time_between_checks = %s, latest_post = %s", (t, cfg['min_time_between_checks'], j['posts'][0]['timestamp']))
  55. if not posted:
  56. #we checked, and they haven't made a post
  57. tbc = int(row['time_between_checks']) + 1
  58. if tbc > int(cfg['max_time_between_checks']):
  59. tbc = cfg['max_time_between_checks']
  60. c.execute("UPDATE data SET last_check = %s, time_between_checks = %s", (t, tbc))
  61. db.commit()
  62. db.commit()