Storing avatars in cache

main2
Bob Mottram 2019-09-14 18:12:03 +01:00
parent 35531bda86
commit 47225c686b
5 changed files with 39 additions and 3 deletions

View File

@ -3671,6 +3671,9 @@ def runDaemon(projectVersion, \
if not os.path.isdir(baseDir+'/cache/announce'): if not os.path.isdir(baseDir+'/cache/announce'):
print('Creating announce cache') print('Creating announce cache')
os.mkdir(baseDir+'/cache/announce') os.mkdir(baseDir+'/cache/announce')
if not os.path.isdir(baseDir+'/cache/avatars'):
print('Creating avatars cache')
os.mkdir(baseDir+'/cache/avatars')
archiveDir=baseDir+'/archive' archiveDir=baseDir+'/archive'
if not os.path.isdir(archiveDir): if not os.path.isdir(archiveDir):

View File

@ -37,7 +37,6 @@ def getJson(session,url: str,headers: {},params: {}, \
sessionHeaders=headers sessionHeaders=headers
if params: if params:
sessionParams=params sessionParams=params
pythonVersion=str(sys.version_info[0])+'.'+str(sys.version_info[1])+'.'+str(sys.version_info[2])
sessionHeaders['User-Agent']='Epicyon/'+version sessionHeaders['User-Agent']='Epicyon/'+version
if domain: if domain:
sessionHeaders['User-Agent']+='; +'+httpPrefix+'://'+domain+'/' sessionHeaders['User-Agent']+='; +'+httpPrefix+'://'+domain+'/'

View File

@ -13,6 +13,7 @@ import commentjson
from datetime import datetime from datetime import datetime
from dateutil.parser import parse from dateutil.parser import parse
from shutil import copyfile from shutil import copyfile
from shutil import copyfileobj
from pprint import pprint from pprint import pprint
from person import personBoxJson from person import personBoxJson
from utils import getNicknameFromActor from utils import getNicknameFromActor
@ -41,6 +42,38 @@ from config import getConfigParam
from skills import getSkills from skills import getSkills
from cache import getPersonFromCache from cache import getPersonFromCache
def updateAvatarImageCache(session,baseDir: str,httpPrefix: str,actor: str,avatarUrl: str,personCache: {},force=False) -> str:
"""Updates the cached avatar for the given actor
"""
if not avatarUrl:
return None
avatarImageFilename=baseDir+'/cache/avatars/'+avatarUrl.replace('/','#')
if not os.path.isfile(avatarImageFilename) or force:
if avatarUrl.endswith('.png'):
sessionHeaders = {'Accept': 'image/png'}
elif avatarUrl.endswith('.jpg') or avatarUrl.endswith('.jpeg'):
sessionHeaders = {'Accept': 'image/jpeg'}
elif avatarUrl.endswith('.gif'):
sessionHeaders = {'Accept': 'image/gif'}
else:
return None
try:
result=session.get(avatarUrl, headers=sessionHeaders, params=None)
with open(avatarImageFilename, 'wb') as f:
result.raw.decode_content = True
copyfileobj(result.raw, f)
print('avatar image downloaded for '+actor)
return avatarUrl
except Exception as e:
print('Failed to download avatar image: '+str(avatarUrl))
print(e)
sessionHeaders = {'Accept': 'application/activity+json; profile="https://www.w3.org/ns/activitystreams"'}
personJson = getJson(session,actor,sessionHeaders,None,__version__,httpPrefix,None)
if personJson:
storePersonInCache(baseDir,actor,personJson,personCache)
return getPersonAvatarUrl(baseDir,actor,personCache)
return None
def getPersonAvatarUrl(baseDir: str,personUrl: str,personCache: {}) -> str: def getPersonAvatarUrl(baseDir: str,personUrl: str,personCache: {}) -> str:
"""Returns the avatar url for the person """Returns the avatar url for the person
""" """
@ -1708,6 +1741,7 @@ def individualPostAsHtml(iconsDir: str,translate: {}, \
if not avatarUrl: if not avatarUrl:
avatarUrl=getPersonAvatarUrl(baseDir,postJsonObject['actor'],personCache) avatarUrl=getPersonAvatarUrl(baseDir,postJsonObject['actor'],personCache)
avatarUrl=updateAvatarImageCache(session,baseDir,httpPrefix,postJsonObject['actor'],avatarUrl,personCache)
if not avatarUrl: if not avatarUrl:
avatarUrl=postJsonObject['actor']+'/avatar.png' avatarUrl=postJsonObject['actor']+'/avatar.png'