mirror of
https://github.com/Lynnesbian/FediBooks/
synced 2024-11-25 08:38:59 +00:00
update cached icons periodically
This commit is contained in:
parent
fc8be5db40
commit
f46e954f5a
1 changed files with 37 additions and 2 deletions
39
service.py
39
service.py
|
@ -1,11 +1,35 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import MySQLdb
|
import MySQLdb
|
||||||
|
from mastodon import Mastodon
|
||||||
from multiprocessing import Pool
|
from multiprocessing import Pool
|
||||||
import json
|
import json
|
||||||
import functions
|
import functions
|
||||||
|
|
||||||
cfg = json.load(open('config.json'))
|
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"
|
||||||
|
)
|
||||||
|
|
||||||
|
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])
|
||||||
|
)
|
||||||
|
|
||||||
|
avatar = client.account_verify_credentials()['avatar']
|
||||||
|
c = db.cursor()
|
||||||
|
c.execute("UPDATE bots SET icon = %s, icon_update_time = CURRENT_TIMESTAMP() WHERE handle = %s", (avatar, bot['handle']))
|
||||||
|
db.commit()
|
||||||
|
|
||||||
print("Establishing DB connection")
|
print("Establishing DB connection")
|
||||||
db = MySQLdb.connect(
|
db = MySQLdb.connect(
|
||||||
host = cfg['db_host'],
|
host = cfg['db_host'],
|
||||||
|
@ -23,12 +47,23 @@ cursor.execute("DELETE FROM fedi_accounts WHERE handle NOT IN (SELECT fedi_id FR
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
print("Generating posts")
|
print("Generating posts")
|
||||||
cursor.execute("SELECT handle FROM bots WHERE enabled = TRUE AND TIMESTAMPDIFF(MINUTE, last_post, CURRENT_TIMESTAMP()) > post_frequency")
|
cursor.execute("SELECT handle FROM bots WHERE enabled = TRUE AND TIMESTAMPDIFF(MINUTE, last_post, CURRENT_TIMESTAMP()) >= post_frequency")
|
||||||
bots = cursor.fetchall()
|
bots = cursor.fetchall()
|
||||||
|
|
||||||
with Pool(cfg['service_threads']) as p:
|
with Pool(cfg['service_threads']) as p:
|
||||||
p.map(functions.make_post, bots)
|
p.map(functions.make_post, bots)
|
||||||
|
|
||||||
#TODO: other cron tasks should be done here, like updating profile pictures
|
print("Updating cached icons")
|
||||||
|
dc = db.cursor(MySQLdb.cursors.DictCursor)
|
||||||
|
dc.execute("""
|
||||||
|
SELECT handle, instance_type, client_id, client_secret, secret
|
||||||
|
FROM bots
|
||||||
|
INNER JOIN credentials
|
||||||
|
ON bots.credentials_id = credentials.id
|
||||||
|
WHERE TIMESTAMPDIFF(HOUR, icon_update_time, CURRENT_TIMESTAMP()) > 2""")
|
||||||
|
bots = dc.fetchall()
|
||||||
|
|
||||||
|
with Pool(cfg['service_threads']) as p:
|
||||||
|
p.map(update_icon, bots)
|
||||||
|
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
Loading…
Reference in a new issue