database.py 1.07 KB
Newer Older
kaniini's avatar
kaniini committed
1 2
import asyncio
import logging
3
import urllib.parse
kaniini's avatar
kaniini committed
4
import simplejson as json
5
from sys import exit
kaniini's avatar
kaniini committed
6 7 8


from . import CONFIG
9
AP_CONFIG = CONFIG['ap']
kaniini's avatar
kaniini committed
10 11 12 13

try:
    with open(CONFIG['db']) as f:
        DATABASE = json.load(f)
14 15

except FileNotFoundError:
kaniini's avatar
kaniini committed
16 17 18
    logging.info('No database was found, making a new one.')
    DATABASE = {}

19 20 21 22
except json.decoder.JSONDecodeError:
    logging.info('Invalid JSON in db. Exiting...')
    exit(1)

gled's avatar
gled committed
23 24
following = DATABASE.get('relay-list', [])
for inbox in following:
Izalia Mae's avatar
Izalia Mae committed
25 26 27 28 29
    if urllib.parse.urlsplit(inbox).hostname in AP_CONFIG['blocked_instances']:
        following.remove(inbox)
        DATABASE['relay-list'] = following

    elif AP_CONFIG['whitelist_enabled'] is True and urllib.parse.urlsplit(inbox).hostname not in AP_CONFIG['whitelist']:
gled's avatar
gled committed
30 31
        following.remove(inbox)
        DATABASE['relay-list'] = following
kaniini's avatar
kaniini committed
32

33 34 35
if 'actors' in DATABASE:
    DATABASE.pop('actors')

kaniini's avatar
kaniini committed
36 37 38 39 40 41 42 43
async def database_save():
    while True:
        with open(CONFIG['db'], 'w') as f:
            json.dump(DATABASE, f)
        await asyncio.sleep(30)


asyncio.ensure_future(database_save())