1
0
Fork 0
mirror of https://github.com/Lynnesbian/FediBooks/ synced 2024-11-25 08:38:59 +00:00

more stable code with better error handling

This commit is contained in:
Lynne Megido 2020-01-20 15:21:46 +10:00
parent fe01416134
commit dc7787d296
Signed by: lynnesbian
GPG key ID: F0A184B5213D9F90
2 changed files with 36 additions and 11 deletions

View file

@ -165,8 +165,11 @@ def make_post(args):
# user has revoked the token given to the bot
# this needs to be dealt with properly later on, but for now, we'll just disable the bot
c.execute("UPDATE bots SET enabled = FALSE WHERE handle = %s", (handle,))
except:
print("Failed to create post for {}".format(handle))
if id == None:
# this wasn't a reply, it was a regular post, so update the last post date
c.execute("UPDATE bots SET last_post = CURRENT_TIMESTAMP() WHERE handle = %s", (handle,))
db.commit()
c.close()

View file

@ -2,12 +2,14 @@
import MySQLdb
from mastodon import Mastodon
from multiprocessing import Pool
import requests
import json
import functions
cfg = json.load(open('config.json'))
def update_icon(bot):
try:
db = MySQLdb.connect(
host = cfg['db_host'],
user=cfg['db_user'],
@ -16,19 +18,39 @@ def update_icon(bot):
use_unicode=True,
charset="utf8mb4"
)
except:
print("Failed to connect to database.")
return
url = "https://{}".format(bot['handle'].split("@")[2])
try:
r = requests.head(url, timeout=10, allow_redirects = True)
if r.status_code != 200:
raise
except:
print("{} is down.".format(url))
return
print("Updating cached icon for {}".format(bot['handle']))
client = Mastodon(
client_id = bot['client_id'],
client_secret = bot['client_secret'],
access_token = bot['secret'],
api_base_url = "https://{}".format(bot['handle'].split("@")[2])
api_base_url = url
)
avatar = client.account_verify_credentials()['avatar']
c = db.cursor()
try:
avatar = client.account_verify_credentials()['avatar']
except:
c.execute("UPDATE bots SET icon_update_time = CURRENT_TIMESTAMP() WHERE handle = %s", (bot['handle'],))
db.commit()
c.close()
return
c.execute("UPDATE bots SET icon = %s, icon_update_time = CURRENT_TIMESTAMP() WHERE handle = %s", (avatar, bot['handle']))
db.commit()
c.close()
print("Establishing DB connection")
db = MySQLdb.connect(