diff --git a/app/functions.py b/app/functions.py index 00229e2..887a494 100644 --- a/app/functions.py +++ b/app/functions.py @@ -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() diff --git a/app/service.py b/app/service.py index cc105a0..2fd3a48 100755 --- a/app/service.py +++ b/app/service.py @@ -2,33 +2,55 @@ 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): - db = MySQLdb.connect( - host = cfg['db_host'], - user=cfg['db_user'], - passwd=cfg['db_pass'], - db=cfg['db_name'], - use_unicode=True, - charset="utf8mb4" - ) + try: + db = MySQLdb.connect( + host = cfg['db_host'], + user=cfg['db_user'], + passwd=cfg['db_pass'], + db=cfg['db_name'], + 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(