curious-greg/run.py

72 lines
2.7 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
#Curious Greg - Curious Cat to Mastodon crossposter
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
2018-11-13 02:49:23 +00:00
import json, time
from mastodon import Mastodon
2018-11-13 02:49:23 +00:00
import requests
import mysql.connector
cfg = json.load(open('meta.json'))
db = mysql.connector.connect(user=cfg['dbuser'], password=cfg['dbpass'], database=cfg['dbname'])
c = db.cursor()
dc = db.cursor(dictionary=True)
dc.execute("SELECT * FROM data")
for row in dc.fetchall():
2018-11-13 10:58:07 +00:00
print(row['cc'])
2018-11-13 11:52:19 +00:00
settings = json.loads(row['settings'])
t = int(time.time())
next_check = row['last_check'] + row['time_between_checks'] * 60
2018-11-13 14:31:10 +00:00
print("current time: {} waiting for: {}".format(t, next_check))
2018-11-13 10:58:07 +00:00
if next_check <= t:
row['time_between_checks'] = cfg['min_time_between_checks']
#time to check
if row['cc'] != "None" and row['cc'] != None:
r = requests.get("https://curiouscat.me/api/v2/profile?username={}&count=100&min_timestamp={}".format(row['cc'], row['latest_post']))
j = r.json()
2018-11-15 01:09:47 +00:00
if 'error' in j:
continue
posted = False
2018-11-13 11:52:19 +00:00
if len(j['posts']) > 0 and not (len(j['posts']) == 1 and int(j['posts'][0]['timestamp']) == int(row['latest_post'])):
#they've made some new posts, log in to masto
2018-11-17 05:34:52 +00:00
try:
client = Mastodon(client_id=row['client_id'], client_secret=row['client_secret'], access_token=row['secret'], api_base_url=row['instance'])
except:
continue
for post in j['posts']:
if post['senderData']['id'] == False:
2018-11-13 12:03:57 +00:00
sender = "(anonymous)"
else:
sender = post['senderData']['username']
2018-11-15 01:06:01 +00:00
if int(post['timestamp']) <= int(row['latest_post']):
2018-11-13 11:52:19 +00:00
#this is the one we've already seen
continue
2018-11-13 12:03:57 +00:00
#if we get to this point, they've definitely made a post
posted = True
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?
2018-11-17 05:37:57 +00:00
try:
if settings['cw']:
client.status_post(toot, spoiler_text="Curious Cat post")
else:
client.status_post(toot)
except:
continue
2018-11-13 10:58:07 +00:00
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']))
if not posted:
2018-11-13 10:58:07 +00:00
#we checked, and they haven't made a post
tbc = int(row['time_between_checks']) + 1
if tbc > int(cfg['max_time_between_checks']):
tbc = cfg['max_time_between_checks']
c.execute("UPDATE data SET last_check = %s, time_between_checks = %s", (t, tbc))
2018-11-17 05:16:28 +00:00
db.commit()
2018-11-13 13:57:23 +00:00
db.commit()
2018-11-13 10:58:07 +00:00