2018-11-18 22:07:12 +00:00
|
|
|
import logging
|
2018-08-10 21:14:22 +00:00
|
|
|
import aiohttp
|
2018-11-18 14:25:04 +00:00
|
|
|
from . import CONFIG
|
2018-11-18 00:07:36 +00:00
|
|
|
from .http_debug import http_debug
|
2018-08-10 21:14:22 +00:00
|
|
|
|
2018-11-18 14:25:04 +00:00
|
|
|
from cachetools import TTLCache
|
|
|
|
|
|
|
|
|
|
|
|
CACHE_SIZE = CONFIG.get('cache-size', 16384)
|
|
|
|
CACHE_TTL = CONFIG.get('cache-ttl', 3600)
|
|
|
|
|
|
|
|
ACTORS = TTLCache(CACHE_SIZE, CACHE_TTL)
|
|
|
|
|
2018-08-10 21:14:22 +00:00
|
|
|
|
|
|
|
async def fetch_actor(uri, force=False):
|
|
|
|
if uri in ACTORS and not force:
|
|
|
|
return ACTORS[uri]
|
|
|
|
|
2018-11-18 22:07:12 +00:00
|
|
|
try:
|
|
|
|
async with aiohttp.ClientSession(trace_configs=[http_debug()]) as session:
|
|
|
|
async with session.get(uri, headers={'Accept': 'application/activity+json'}) as resp:
|
2018-11-18 22:09:08 +00:00
|
|
|
if resp.status != 200:
|
2018-11-18 22:07:12 +00:00
|
|
|
return None
|
|
|
|
ACTORS[uri] = (await resp.json(encoding='utf-8', content_type=None))
|
|
|
|
return ACTORS[uri]
|
|
|
|
except Exception as e:
|
|
|
|
logging.info('Caught %r while fetching actor %r.', e, uri)
|
|
|
|
return None
|