curious-greg/run.py

39 lines
1.5 KiB
Python
Executable file

#!/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/.
import json, time
from mastodon import Mastodon
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():
print(row)
t = int(time.time())
next_check = row['last_check'] + row['time_between_checks'] * 60
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()
if len(j['posts'] > 0):
#they've made some new posts, log in to masto
client = Mastodon(client_id=row['client_id'], client_secret=row['client_secret'], access_token=row['secret'], api_base_url=row['instance'])
for post in j['posts']:
if post['senderData']['id'] == False:
sender = "Anonymous user"
else:
sender = post['senderData']['username']
toot = "Curious cat question from {}: \"{}\"<br />My answer: {}\nView online at https://curiouscat.me/{}/post/{}".format(sender, post['comment'], post['reply'], row['cc'], post['id'])