forked from indymedia/epicyon
Storing avatars in cache
parent
35531bda86
commit
47225c686b
2
cache.py
2
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
|
||||
"""
|
||||
|
|
|
@ -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):
|
||||
|
|
2
posts.py
2
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):
|
||||
|
|
|
@ -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+'/'
|
||||
|
|
|
@ -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'
|
||||
|
||||
|
|
Loading…
Reference in New Issue