From 47225c686b895d1ff6d0293ed030884436b6a674 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sat, 14 Sep 2019 18:12:03 +0100 Subject: [PATCH] Storing avatars in cache --- cache.py | 2 +- daemon.py | 3 +++ posts.py | 2 +- session.py | 1 - webinterface.py | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 39 insertions(+), 3 deletions(-) diff --git a/cache.py b/cache.py index 5b9dbe42b..10e6341b0 100644 --- a/cache.py +++ b/cache.py @@ -10,7 +10,7 @@ import os import time import datetime import commentjson - + def storePersonInCache(baseDir: str,personUrl: str,personJson: {},personCache: {}) -> None: """Store an actor in the cache """ diff --git a/daemon.py b/daemon.py index 306986217..28a360f6c 100644 --- a/daemon.py +++ b/daemon.py @@ -3671,6 +3671,9 @@ def runDaemon(projectVersion, \ if not os.path.isdir(baseDir+'/cache/announce'): print('Creating announce cache') 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' if not os.path.isdir(archiveDir): diff --git a/posts.py b/posts.py index 1c257de4b..bd94f0fe3 100644 --- a/posts.py +++ b/posts.py @@ -1931,7 +1931,7 @@ def archivePostsForPerson(httpPrefix: str,nickname: str,domain: str,baseDir: str return # sort the list in ascending order of date postsInBox=OrderedDict(sorted(postsInBoxDict.items(),reverse=False)) - + for statusNumber,postFilename in postsInBox.items(): filePath = os.path.join(boxDir, postFilename) if os.path.isfile(filePath): diff --git a/session.py b/session.py index 89572a213..1ac6a825b 100644 --- a/session.py +++ b/session.py @@ -37,7 +37,6 @@ def getJson(session,url: str,headers: {},params: {}, \ sessionHeaders=headers if params: sessionParams=params - pythonVersion=str(sys.version_info[0])+'.'+str(sys.version_info[1])+'.'+str(sys.version_info[2]) sessionHeaders['User-Agent']='Epicyon/'+version if domain: sessionHeaders['User-Agent']+='; +'+httpPrefix+'://'+domain+'/' diff --git a/webinterface.py b/webinterface.py index 40edddb0d..85f3ae453 100644 --- a/webinterface.py +++ b/webinterface.py @@ -13,6 +13,7 @@ import commentjson from datetime import datetime from dateutil.parser import parse from shutil import copyfile +from shutil import copyfileobj from pprint import pprint from person import personBoxJson from utils import getNicknameFromActor @@ -41,6 +42,38 @@ from config import getConfigParam from skills import getSkills 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: """Returns the avatar url for the person """ @@ -1708,6 +1741,7 @@ def individualPostAsHtml(iconsDir: str,translate: {}, \ if not avatarUrl: avatarUrl=getPersonAvatarUrl(baseDir,postJsonObject['actor'],personCache) + avatarUrl=updateAvatarImageCache(session,baseDir,httpPrefix,postJsonObject['actor'],avatarUrl,personCache) if not avatarUrl: avatarUrl=postJsonObject['actor']+'/avatar.png'