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 # 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 # 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,)) c.execute("UPDATE bots SET enabled = FALSE WHERE handle = %s", (handle,))
except:
print("Failed to create post for {}".format(handle))
if id == None: if id == None:
# this wasn't a reply, it was a regular post, so update the last post date # 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,)) c.execute("UPDATE bots SET last_post = CURRENT_TIMESTAMP() WHERE handle = %s", (handle,))
db.commit() db.commit()
c.close()

View file

@ -2,33 +2,55 @@
import MySQLdb import MySQLdb
from mastodon import Mastodon from mastodon import Mastodon
from multiprocessing import Pool from multiprocessing import Pool
import requests
import json import json
import functions import functions
cfg = json.load(open('config.json')) cfg = json.load(open('config.json'))
def update_icon(bot): def update_icon(bot):
db = MySQLdb.connect( try:
host = cfg['db_host'], db = MySQLdb.connect(
user=cfg['db_user'], host = cfg['db_host'],
passwd=cfg['db_pass'], user=cfg['db_user'],
db=cfg['db_name'], passwd=cfg['db_pass'],
use_unicode=True, db=cfg['db_name'],
charset="utf8mb4" 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 = Mastodon(
client_id = bot['client_id'], client_id = bot['client_id'],
client_secret = bot['client_secret'], client_secret = bot['client_secret'],
access_token = bot['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() 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'])) c.execute("UPDATE bots SET icon = %s, icon_update_time = CURRENT_TIMESTAMP() WHERE handle = %s", (avatar, bot['handle']))
db.commit() db.commit()
c.close()
print("Establishing DB connection") print("Establishing DB connection")
db = MySQLdb.connect( db = MySQLdb.connect(