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:
parent
fe01416134
commit
dc7787d296
2 changed files with 36 additions and 11 deletions
|
@ -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()
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in a new issue