Tidying the loading and saving of json

main2
Bob Mottram 2019-10-22 12:55:06 +01:00
parent baaf4c9e27
commit 077d34ba7a
18 changed files with 204 additions and 1102 deletions

View File

@ -18,6 +18,8 @@ from utils import getNicknameFromActor
from utils import getDomainFromActor from utils import getDomainFromActor
from utils import locatePost from utils import locatePost
from utils import getCachedPostFilename from utils import getCachedPostFilename
from utils import loadJson
from utils import saveJson
from posts import sendSignedJson from posts import sendSignedJson
from posts import getPersonBox from posts import getPersonBox
from session import postJson from session import postJson
@ -70,17 +72,7 @@ def undoAnnounceCollectionEntry(baseDir: str,postFilename: str,actor: str,domain
collection within a post. Note that the "shares" collection has no relation collection within a post. Note that the "shares" collection has no relation
to shared items in shares.py. It's shares of posts, not shares of physical objects. to shared items in shares.py. It's shares of posts, not shares of physical objects.
""" """
postJsonObject=None postJsonObject=loadJson(postFilename)
tries=0
while tries<5:
try:
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception undoAnnounceCollectionEntry - '+str(e))
time.sleep(2)
tries+=1
if postJsonObject: if postJsonObject:
# remove any cached version of this announce so that the like icon is changed # remove any cached version of this announce so that the like icon is changed
nickname=getNicknameFromActor(actor) nickname=getNicknameFromActor(actor)
@ -122,33 +114,14 @@ def undoAnnounceCollectionEntry(baseDir: str,postFilename: str,actor: str,domain
del postJsonObject['object']['shares'] del postJsonObject['object']['shares']
else: else:
postJsonObject['object']['shares']['totalItems']=len(postJsonObject['object']['shares']['items']) postJsonObject['object']['shares']['totalItems']=len(postJsonObject['object']['shares']['items'])
tries=0 saveJson(postJsonObject,postFilename)
while tries<5:
try:
with open(postFilename, 'w') as fp:
commentjson.dump(postJsonObject, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
def updateAnnounceCollection(baseDir: str,postFilename: str,actor: str,domain: str,debug: bool) -> None: def updateAnnounceCollection(baseDir: str,postFilename: str,actor: str,domain: str,debug: bool) -> None:
"""Updates the announcements collection within a post """Updates the announcements collection within a post
Confusingly this is known as "shares", but isn't the same as shared items within shares.py Confusingly this is known as "shares", but isn't the same as shared items within shares.py
It's shares of posts, not shares of physical objects. It's shares of posts, not shares of physical objects.
""" """
postJsonObject=None postJsonObject=loadJson(postFilename)
tries=0
while tries<5:
try:
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception updateAnnounceCollection - '+str(e))
time.sleep(1)
tries+=1
if postJsonObject: if postJsonObject:
# remove any cached version of this announce so that the like icon is changed # remove any cached version of this announce so that the like icon is changed
nickname=getNicknameFromActor(actor) nickname=getNicknameFromActor(actor)
@ -197,16 +170,7 @@ def updateAnnounceCollection(baseDir: str,postFilename: str,actor: str,domain: s
if debug: if debug:
print('DEBUG: saving post with shares (announcements) added') print('DEBUG: saving post with shares (announcements) added')
pprint(postJsonObject) pprint(postJsonObject)
tries=0 saveJson(postJsonObject,postFilename)
while tries<5:
try:
with open(postFilename, 'w') as fp:
commentjson.dump(postJsonObject, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
def announcedByPerson(postJsonObject: {}, nickname: str,domain: str) -> bool: def announcedByPerson(postJsonObject: {}, nickname: str,domain: str) -> bool:
"""Returns True if the given post is announced by the given person """Returns True if the given post is announced by the given person
@ -269,16 +233,7 @@ def createAnnounce(session,baseDir: str,federationList: [], \
if saveToFile: if saveToFile:
outboxDir = createOutboxDir(nickname,domain,baseDir) outboxDir = createOutboxDir(nickname,domain,baseDir)
filename=outboxDir+'/'+newAnnounceId.replace('/','#')+'.json' filename=outboxDir+'/'+newAnnounceId.replace('/','#')+'.json'
tries=0 saveJson(newAnnounce,filename)
while tries<5:
try:
with open(filename, 'w') as fp:
commentjson.dump(newAnnounce, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
announceNickname=None announceNickname=None
announceDomain=None announceDomain=None

View File

@ -16,6 +16,8 @@ from posts import getPersonBox
from session import postJson from session import postJson
from utils import getNicknameFromActor from utils import getNicknameFromActor
from utils import getDomainFromActor from utils import getDomainFromActor
from utils import loadJson
from utils import saveJson
def setAvailability(baseDir: str,nickname: str,domain: str, \ def setAvailability(baseDir: str,nickname: str,domain: str, \
status: str) -> bool: status: str) -> bool:
@ -27,29 +29,10 @@ def setAvailability(baseDir: str,nickname: str,domain: str, \
actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json' actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json'
if not os.path.isfile(actorFilename): if not os.path.isfile(actorFilename):
return False return False
actorJson=None actorJson=loadJson(actorFilename)
tries=0
while tries<5:
try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception setAvailability - '+str(e))
time.sleep(1)
tries+=1
if actorJson: if actorJson:
actorJson['availability']=status actorJson['availability']=status
tries=0 saveJson(actorJson,actorFilename)
while tries<5:
try:
with open(actorFilename, 'w') as fp:
commentjson.dump(actorJson, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
return True return True
def getAvailability(baseDir: str,nickname: str,domain: str) -> str: def getAvailability(baseDir: str,nickname: str,domain: str) -> str:
@ -58,17 +41,7 @@ def getAvailability(baseDir: str,nickname: str,domain: str) -> str:
actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json' actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json'
if not os.path.isfile(actorFilename): if not os.path.isfile(actorFilename):
return False return False
actorJson=None actorJson=loadJson(actorFilename)
tries=0
while tries<5:
try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception getAvailability - '+str(e))
time.sleep(1)
tries+=1
if actorJson: if actorJson:
if not actorJson.get('availability'): if not actorJson.get('availability'):
return None return None

View File

@ -10,6 +10,8 @@ import os
import time import time
import datetime import datetime
import commentjson import commentjson
from utils import loadJson
from utils import saveJson
def storePersonInCache(baseDir: str,personUrl: str,personJson: {},personCache: {}) -> None: def storePersonInCache(baseDir: str,personUrl: str,personJson: {},personCache: {}) -> None:
"""Store an actor in the cache """Store an actor in the cache
@ -26,16 +28,7 @@ def storePersonInCache(baseDir: str,personUrl: str,personJson: {},personCache: {
if os.path.isdir(baseDir+'/cache/actors'): if os.path.isdir(baseDir+'/cache/actors'):
cacheFilename=baseDir+'/cache/actors/'+personUrl.replace('/','#')+'.json' cacheFilename=baseDir+'/cache/actors/'+personUrl.replace('/','#')+'.json'
if not os.path.isfile(cacheFilename): if not os.path.isfile(cacheFilename):
tries=0 saveJson(personJson,cacheFilename)
while tries<5:
try:
with open(cacheFilename, 'w') as fp:
commentjson.dump(personJson, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
def getPersonFromCache(baseDir: str,personUrl: str,personCache: {}) -> {}: def getPersonFromCache(baseDir: str,personUrl: str,personCache: {}) -> {}:
"""Get an actor from the cache """Get an actor from the cache
@ -45,19 +38,7 @@ def getPersonFromCache(baseDir: str,personUrl: str,personCache: {}) -> {}:
if not personCache.get(personUrl): if not personCache.get(personUrl):
cacheFilename=baseDir+'/cache/actors/'+personUrl.replace('/','#')+'.json' cacheFilename=baseDir+'/cache/actors/'+personUrl.replace('/','#')+'.json'
if os.path.isfile(cacheFilename): if os.path.isfile(cacheFilename):
personJson=None personJson=loadJson(cacheFilename)
tries=0
while tries<5:
try:
with open(cacheFilename, 'r') as fp:
personJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception getPersonFromCache - '+str(e))
print('ERROR: unable to load actor from cache '+cacheFilename+' try '+str(tries))
print(e)
time.sleep(1)
tries+=1
if personJson: if personJson:
storePersonInCache(baseDir,personUrl,personJson,personCache) storePersonInCache(baseDir,personUrl,personJson,personCache)
loadedFromFile=True loadedFromFile=True

View File

@ -14,6 +14,8 @@ import commentjson
from auth import createPassword from auth import createPassword
from utils import getNicknameFromActor from utils import getNicknameFromActor
from utils import getDomainFromActor from utils import getDomainFromActor
from utils import loadJson
from utils import saveJson
def getOcapFilename(baseDir :str,nickname: str,domain: str,actor :str,subdir: str) -> str: def getOcapFilename(baseDir :str,nickname: str,domain: str,actor :str,subdir: str) -> str:
"""Returns the filename for a particular capability accepted or granted """Returns the filename for a particular capability accepted or granted
@ -127,16 +129,7 @@ def capabilitiesAccept(baseDir: str,httpPrefix: str, \
# if the capability already exists then load it from file # if the capability already exists then load it from file
if os.path.isfile(ocapFilename): if os.path.isfile(ocapFilename):
tries=0 ocapAccept=loadJson(ocapFilename)
while tries<5:
try:
with open(ocapFilename, 'r') as fp:
ocapAccept=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception capabilitiesAccept - '+str(e))
time.sleep(1)
tries+=1
# otherwise create a new capability # otherwise create a new capability
if not ocapAccept: if not ocapAccept:
acceptedActorNickname=getNicknameFromActor(acceptedActor) acceptedActorNickname=getNicknameFromActor(acceptedActor)
@ -160,16 +153,7 @@ def capabilitiesAccept(baseDir: str,httpPrefix: str, \
ocapAccept['actor']=httpPrefix+"://"+fullDomain+'/users/'+nickname ocapAccept['actor']=httpPrefix+"://"+fullDomain+'/users/'+nickname
if saveToFile: if saveToFile:
tries=0 saveJson(ocapAccept,ocapFilename)
while tries<5:
try:
with open(ocapFilename, 'w') as fp:
commentjson.dump(ocapAccept, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
return ocapAccept return ocapAccept
def capabilitiesGrantedSave(baseDir :str,nickname :str,domain :str,ocap: {}) -> bool: def capabilitiesGrantedSave(baseDir :str,nickname :str,domain :str,ocap: {}) -> bool:
@ -181,16 +165,7 @@ def capabilitiesGrantedSave(baseDir :str,nickname :str,domain :str,ocap: {}) ->
ocapFilename=getOcapFilename(baseDir,nickname,domain,ocap['actor'],'granted') ocapFilename=getOcapFilename(baseDir,nickname,domain,ocap['actor'],'granted')
if not ocapFilename: if not ocapFilename:
return False return False
tries=0 saveJson(ocap,ocapFilename)
while tries<5:
try:
with open(ocapFilename, 'w') as fp:
commentjson.dump(ocap, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
return True return True
def capabilitiesUpdate(baseDir: str,httpPrefix: str, \ def capabilitiesUpdate(baseDir: str,httpPrefix: str, \
@ -232,16 +207,7 @@ def capabilitiesUpdate(baseDir: str,httpPrefix: str, \
} }
# read the existing capability # read the existing capability
tries=0 ocapJson=loadJson(ocapFilename)
while tries<5:
try:
with open(ocapFilename, 'r') as fp:
ocapJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception capabilitiesUpdate - '+str(e))
time.sleep(1)
tries+=1
# set the new capabilities list. eg. ["inbox:write","objects:read"] # set the new capabilities list. eg. ["inbox:write","objects:read"]
ocapJson['capability']=updateCaps ocapJson['capability']=updateCaps
@ -260,16 +226,7 @@ def capabilitiesUpdate(baseDir: str,httpPrefix: str, \
ocapUpdate['object']=ocapJson ocapUpdate['object']=ocapJson
# save it again # save it again
tries=0 saveJson(ocapJson,ocapFilename)
while tries<5:
try:
with open(ocapFilename, 'w') as fp:
commentjson.dump(ocapJson, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
return ocapUpdate return ocapUpdate
@ -291,30 +248,11 @@ def capabilitiesReceiveUpdate(baseDir :str, \
print(ocapFilename) print(ocapFilename)
return False return False
ocapJson=None ocapJson=loadJson(ocapFilename)
tries=0
while tries<5:
try:
with open(ocapFilename, 'r') as fp:
ocapJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception capabilitiesReceiveUpdate - '+str(e))
time.sleep(1)
tries+=1
if ocapJson: if ocapJson:
ocapJson['id']=newCapabilitiesId ocapJson['id']=newCapabilitiesId
ocapJson['capability']=capabilityList ocapJson['capability']=capabilityList
tries=0 return saveJson(ocapJson,ocapFilename)
while tries<5:
try:
with open(ocapFilename, 'w') as fp:
commentjson.dump(ocapJson, fp, indent=2, sort_keys=False)
return True
except Exception as e:
print(e)
time.sleep(1)
tries+=1
return False return False

View File

@ -10,6 +10,8 @@ import os
import time import time
import json import json
import commentjson import commentjson
from utils import loadJson
from utils import saveJson
def createConfig(baseDir: str) -> None: def createConfig(baseDir: str) -> None:
"""Creates a configuration file """Creates a configuration file
@ -19,59 +21,26 @@ def createConfig(baseDir: str) -> None:
return return
configJson = { configJson = {
} }
tries=0 saveJson(configJson,configFilename)
while tries<5:
try:
with open(configFilename, 'w') as fp:
commentjson.dump(configJson, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
def setConfigParam(baseDir: str, variableName: str, variableValue) -> None: def setConfigParam(baseDir: str, variableName: str, variableValue) -> None:
"""Sets a configuration value """Sets a configuration value
""" """
createConfig(baseDir) createConfig(baseDir)
configFilename=baseDir+'/config.json' configFilename=baseDir+'/config.json'
tries=0 configJson={}
while tries<5: if os.path.isfile(configFilename):
try: configJson=loadJson(configFilename)
with open(configFilename, 'r') as fp:
configJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception setConfigParam - '+str(e))
time.sleep(1)
tries+=1
configJson[variableName]=variableValue configJson[variableName]=variableValue
tries=0 saveJson(configJson,configFilename)
while tries<5:
try:
with open(configFilename, 'w') as fp:
commentjson.dump(configJson, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
def getConfigParam(baseDir: str, variableName: str): def getConfigParam(baseDir: str, variableName: str):
"""Gets a configuration value """Gets a configuration value
""" """
createConfig(baseDir) createConfig(baseDir)
configFilename=baseDir+'/config.json' configFilename=baseDir+'/config.json'
tries=0 configJson=loadJson(configFilename)
while tries<5: if configJson:
try:
with open(configFilename, 'r') as fp:
configJson=commentjson.load(fp)
if configJson.get(variableName): if configJson.get(variableName):
return configJson[variableName] return configJson[variableName]
break
except Exception as e:
print('WARN: commentjson exception getConfigParam - '+str(e))
time.sleep(1)
tries+=1
return None return None

114
daemon.py
View File

@ -123,6 +123,8 @@ from utils import getNicknameFromActor
from utils import getDomainFromActor from utils import getDomainFromActor
from utils import getStatusNumber from utils import getStatusNumber
from utils import urlPermitted from utils import urlPermitted
from utils import loadJson
from utils import saveJson
from manualapprove import manualDenyFollowRequest from manualapprove import manualDenyFollowRequest
from manualapprove import manualApproveFollowRequest from manualapprove import manualApproveFollowRequest
from announce import createAnnounce from announce import createAnnounce
@ -1700,19 +1702,12 @@ class PubServer(BaseHTTPRequestHandler):
self.server.baseDir+'/accounts/'+nickname+'@'+self.server.domain+'/outbox/'+ \ self.server.baseDir+'/accounts/'+nickname+'@'+self.server.domain+'/outbox/'+ \
self.server.httpPrefix+':##'+self.server.domainFull+'#users#'+nickname+'#statuses#'+statusNumber+'.json' self.server.httpPrefix+':##'+self.server.domainFull+'#users#'+nickname+'#statuses#'+statusNumber+'.json'
if os.path.isfile(postFilename): if os.path.isfile(postFilename):
postJsonObject={} postJsonObject=loadJson(postFilename)
loadedPost=False loadedPost=False
tries=0 if postJsonObject:
while tries<5:
try:
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
loadedPost=True loadedPost=True
break else:
except Exception as e: postJsonObject={}
print('WARN: commentjson exception 1 - '+str(e))
time.sleep(1)
tries+=1
if loadedPost: if loadedPost:
# Only authorized viewers get to see likes on posts # Only authorized viewers get to see likes on posts
# Otherwize marketers could gain more social graph info # Otherwize marketers could gain more social graph info
@ -1903,19 +1898,8 @@ class PubServer(BaseHTTPRequestHandler):
nickname=postSections[0] nickname=postSections[0]
actorFilename=self.server.baseDir+'/accounts/'+nickname+'@'+self.server.domain+'.json' actorFilename=self.server.baseDir+'/accounts/'+nickname+'@'+self.server.domain+'.json'
if os.path.isfile(actorFilename): if os.path.isfile(actorFilename):
loadedActor=False actorJson=loadJson(actorFilename)
tries=0 if actorJson:
while tries<5:
try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
loadedActor=True
break
except Exception as e:
print('WARN: commentjson exception 2 - '+str(e))
time.sleep(1)
tries+=1
if loadedActor:
if actorJson.get('roles'): if actorJson.get('roles'):
if self._requestHTTP(): if self._requestHTTP():
getPerson = \ getPerson = \
@ -1970,19 +1954,8 @@ class PubServer(BaseHTTPRequestHandler):
nickname=postSections[0] nickname=postSections[0]
actorFilename=self.server.baseDir+'/accounts/'+nickname+'@'+self.server.domain+'.json' actorFilename=self.server.baseDir+'/accounts/'+nickname+'@'+self.server.domain+'.json'
if os.path.isfile(actorFilename): if os.path.isfile(actorFilename):
loadedActor=False actorJson=loadJson(actorFilename)
tries=0 if actorJson:
while tries<5:
try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
loadedActor=True
break
except Exception as e:
print('WARN: commentjson exception 3 - '+str(e))
time.sleep(1)
tries+=1
if loadedActor:
if actorJson.get('skills'): if actorJson.get('skills'):
if self._requestHTTP(): if self._requestHTTP():
getPerson = \ getPerson = \
@ -2046,20 +2019,8 @@ class PubServer(BaseHTTPRequestHandler):
self.server.baseDir+'/accounts/'+nickname+'@'+self.server.domain+'/outbox/'+ \ self.server.baseDir+'/accounts/'+nickname+'@'+self.server.domain+'/outbox/'+ \
self.server.httpPrefix+':##'+self.server.domainFull+'#users#'+nickname+'#statuses#'+statusNumber+'.json' self.server.httpPrefix+':##'+self.server.domainFull+'#users#'+nickname+'#statuses#'+statusNumber+'.json'
if os.path.isfile(postFilename): if os.path.isfile(postFilename):
postJsonObject={} postJsonObject=loadJson(postFilename)
readPost=False if not postJsonObject:
tries=0
while tries<5:
try:
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
readPost=True
break
except Exception as e:
print('WARN: commentjson exception 4 - '+str(e))
time.sleep(1)
tries+=1
if not readPost:
self.send_response(429) self.send_response(429)
self.end_headers() self.end_headers()
self.server.GETbusy=False self.server.GETbusy=False
@ -3452,19 +3413,8 @@ class PubServer(BaseHTTPRequestHandler):
actorFilename=self.server.baseDir+'/accounts/'+nickname+'@'+self.server.domain+'.json' actorFilename=self.server.baseDir+'/accounts/'+nickname+'@'+self.server.domain+'.json'
if os.path.isfile(actorFilename): if os.path.isfile(actorFilename):
loadedActor=False actorJson=loadJson(actorFilename)
tries=0 if actorJson:
while tries<5:
try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
loadedActor=True
break
except Exception as e:
print('WARN: commentjson exception 5 - '+str(e))
time.sleep(1)
tries+=1
if loadedActor:
actorChanged=False actorChanged=False
skillCtr=1 skillCtr=1
newSkills={} newSkills={}
@ -3583,29 +3533,11 @@ class PubServer(BaseHTTPRequestHandler):
os.remove(allowedInstancesFilename) os.remove(allowedInstancesFilename)
# save actor json file within accounts # save actor json file within accounts
if actorChanged: if actorChanged:
tries=0 saveJson(actorJson,actorFilename)
while tries<5:
try:
with open(actorFilename, 'w') as fp:
commentjson.dump(actorJson, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
# also copy to the actors cache and personCache in memory # also copy to the actors cache and personCache in memory
storePersonInCache(self.server.baseDir,actorJson['id'],actorJson,self.server.personCache) storePersonInCache(self.server.baseDir,actorJson['id'],actorJson,self.server.personCache)
actorCacheFilename=self.server.baseDir+'/cache/actors/'+actorJson['id'].replace('/','#')+'.json' actorCacheFilename=self.server.baseDir+'/cache/actors/'+actorJson['id'].replace('/','#')+'.json'
tries=0 saveJson(actorJson,actorCacheFilename)
while tries<5:
try:
with open(actorCacheFilename, 'w') as fp:
commentjson.dump(actorJson, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
# send actor update to followers # send actor update to followers
updateActorJson={ updateActorJson={
'type': 'Update', 'type': 'Update',
@ -4692,19 +4624,7 @@ def runDaemon(projectVersion, \
systemLanguage='en' systemLanguage='en'
translationsFile=baseDir+'/translations/'+systemLanguage+'.json' translationsFile=baseDir+'/translations/'+systemLanguage+'.json'
print('System language: '+systemLanguage) print('System language: '+systemLanguage)
httpd.translate=loadJson(translationsFile)
tries=0
while tries<5:
try:
with open(translationsFile, 'r') as fp:
httpd.translate=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception 7 - '+str(e))
print('ERROR while loading translations '+translationsFile)
print(e)
time.sleep(1)
tries+=1
httpd.outboxThread={} httpd.outboxThread={}
httpd.newPostThread={} httpd.newPostThread={}

View File

@ -20,6 +20,8 @@ from utils import getStatusNumber
from utils import followPerson from utils import followPerson
from posts import sendSignedJson from posts import sendSignedJson
from posts import getPersonBox from posts import getPersonBox
from utils import loadJson
from utils import saveJson
from acceptreject import createAccept from acceptreject import createAccept
from acceptreject import createReject from acceptreject import createReject
from webfinger import webfingerHandle from webfinger import webfingerHandle
@ -337,17 +339,7 @@ def followApprovalRequired(baseDir: str,nicknameToFollow: str, \
domainToFollow=domainToFollow.split(':')[0] domainToFollow=domainToFollow.split(':')[0]
actorFilename=baseDir+'/accounts/'+nicknameToFollow+'@'+domainToFollow+'.json' actorFilename=baseDir+'/accounts/'+nicknameToFollow+'@'+domainToFollow+'.json'
if os.path.isfile(actorFilename): if os.path.isfile(actorFilename):
actor=None actor=loadJson(actorFilename)
tries=0
while tries<5:
try:
with open(actorFilename, 'r') as fp:
actor=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception followApprovalRequired - '+str(e))
time.sleep(1)
tries+=1
if actor: if actor:
if actor.get('manuallyApprovesFollowers'): if actor.get('manuallyApprovesFollowers'):
manuallyApproveFollows=actor['manuallyApprovesFollowers'] manuallyApproveFollows=actor['manuallyApprovesFollowers']
@ -412,17 +404,7 @@ def storeFollowRequest(baseDir: str, \
if not os.path.isdir(requestsDir): if not os.path.isdir(requestsDir):
os.mkdir(requestsDir) os.mkdir(requestsDir)
followActivityfilename=requestsDir+'/'+approveHandle+'.follow' followActivityfilename=requestsDir+'/'+approveHandle+'.follow'
tries=0 return saveJson(followJson,followActivityfilename)
while tries<5:
try:
with open(followActivityfilename, 'w') as fp:
commentjson.dump(followJson, fp, indent=2, sort_keys=False)
return True
except Exception as e:
print(e)
time.sleep(1)
tries+=1
return False
def receiveFollowRequest(session,baseDir: str,httpPrefix: str, \ def receiveFollowRequest(session,baseDir: str,httpPrefix: str, \
port: int,sendThreads: [],postLog: [], \ port: int,sendThreads: [],postLog: [], \
@ -889,17 +871,7 @@ def getFollowersOfActor(baseDir :str,actor :str,debug: bool) -> {}:
if debug: if debug:
print('DEBUG: checking capabilities of'+account) print('DEBUG: checking capabilities of'+account)
if os.path.isfile(ocapFilename): if os.path.isfile(ocapFilename):
ocapJson=None ocapJson=loadJson(ocapFilename)
tries=0
while tries<5:
try:
with open(ocapFilename, 'r') as fp:
ocapJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception getFollowersOfActor - '+str(e))
time.sleep(1)
tries+=1
if ocapJson: if ocapJson:
if ocapJson.get('id'): if ocapJson.get('id'):
if debug: if debug:

132
inbox.py
View File

@ -23,6 +23,8 @@ from utils import locatePost
from utils import deletePost from utils import deletePost
from utils import removeAttachment from utils import removeAttachment
from utils import removeModerationPostFromIndex from utils import removeModerationPostFromIndex
from utils import loadJson
from utils import saveJson
from httpsig import verifyPostHeaders from httpsig import verifyPostHeaders
from session import createSession from session import createSession
from session import getJson from session import getJson
@ -314,16 +316,7 @@ def savePostToInboxQueue(baseDir: str,httpPrefix: str, \
if debug: if debug:
print('Inbox queue item created') print('Inbox queue item created')
pprint(newQueueItem) pprint(newQueueItem)
tries=0 saveJson(newQueueItem,filename)
while tries<5:
try:
with open(filename, 'w') as fp:
commentjson.dump(newQueueItem, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
return filename return filename
def inboxCheckCapabilities(baseDir :str,nickname :str,domain :str, \ def inboxCheckCapabilities(baseDir :str,nickname :str,domain :str, \
@ -348,17 +341,7 @@ def inboxCheckCapabilities(baseDir :str,nickname :str,domain :str, \
queue.pop(0) queue.pop(0)
return False return False
tries=0 oc=loadJson(ocapFilename)
oc=None
while tries<5:
try:
with open(ocapFilename, 'r') as fp:
oc=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception inboxCheckCapabilities - '+str(e))
time.sleep(1)
tries+=1
if not oc: if not oc:
return False return False
@ -424,19 +407,8 @@ def inboxPostRecipientsAdd(baseDir :str,httpPrefix :str,toList :[], \
ocapFilename=baseDir+'/accounts/'+handle+'/ocap/accept/'+actor.replace('/','#')+'.json' ocapFilename=baseDir+'/accounts/'+handle+'/ocap/accept/'+actor.replace('/','#')+'.json'
if os.path.isfile(ocapFilename): if os.path.isfile(ocapFilename):
# read the granted capabilities and obtain the id # read the granted capabilities and obtain the id
loadedOcap=False ocapJson=loadJson(ocapFilename)
tries=0 if ocapJson:
while tries<5:
try:
with open(ocapFilename, 'r') as fp:
ocapJson=commentjson.load(fp)
loadedOcap=True
break
except Exception as e:
print('WARN: commentjson exception inboxPostRecipientsAdd - '+str(e))
time.sleep(1)
tries+=1
if loadedOcap:
if ocapJson.get('id'): if ocapJson.get('id'):
# append with the capabilities id # append with the capabilities id
recipientsDict[handle]=ocapJson['id'] recipientsDict[handle]=ocapJson['id']
@ -723,19 +695,8 @@ def personReceiveUpdate(baseDir: str, \
return False return False
else: else:
if os.path.isfile(actorFilename): if os.path.isfile(actorFilename):
loadedActor=False existingPersonJson=loadJson(actorFilename)
tries=0 if existingPersonJson:
while tries<5:
try:
with open(actorFilename, 'r') as fp:
existingPersonJson=commentjson.load(fp)
loadedActor=True
break
except Exception as e:
print('WARN: commentjson exception personReceiveUpdate - '+str(e))
time.sleep(1)
tries+=1
if loadedActor:
if existingPersonJson['publicKey']['publicKeyPem']!=personJson['publicKey']['publicKeyPem']: if existingPersonJson['publicKey']['publicKeyPem']!=personJson['publicKey']['publicKeyPem']:
if debug: if debug:
print('WARN: Public key does not match cached actor when updating') print('WARN: Public key does not match cached actor when updating')
@ -743,17 +704,8 @@ def personReceiveUpdate(baseDir: str, \
# save to cache in memory # save to cache in memory
storePersonInCache(baseDir,personJson['id'],personJson,personCache) storePersonInCache(baseDir,personJson['id'],personJson,personCache)
# save to cache on file # save to cache on file
tries=0 if saveJson(personJson,actorFilename):
while tries<5:
try:
with open(actorFilename, 'w') as fp:
commentjson.dump(personJson, fp, indent=2, sort_keys=False)
print('actor updated for '+personJson['id']) print('actor updated for '+personJson['id'])
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
# remove avatar if it exists so that it will be refreshed later # remove avatar if it exists so that it will be refreshed later
# when a timeline is constructed # when a timeline is constructed
@ -1148,19 +1100,8 @@ def receiveUndoAnnounce(session,handle: str,isGroup: bool,baseDir: str, \
if debug: if debug:
print('DEBUG: announced/repeated post to be undone found in inbox') print('DEBUG: announced/repeated post to be undone found in inbox')
loadedPost=False postJsonObject=loadJson(postFilename)
tries=0 if postJsonObject:
while tries<5:
try:
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
loadedPost=True
break
except Exception as e:
print('WARN: commentjson exception receiveUndoAnnounce - '+str(e))
time.sleep(1)
tries+=1
if loadedPost:
if not postJsonObject.get('type'): if not postJsonObject.get('type'):
if postJsonObject['type']!='Announce': if postJsonObject['type']!='Announce':
if debug: if debug:
@ -1348,17 +1289,7 @@ def groupHandle(baseDir: str,handle: str) -> bool:
actorFile=baseDir+'/accounts/'+handle+'.json' actorFile=baseDir+'/accounts/'+handle+'.json'
if not os.path.isfile(actorFile): if not os.path.isfile(actorFile):
return False return False
actorJson=None actorJson=loadJson(actorFile)
tries=0
while tries<5:
try:
with open(actorFile, 'r') as fp:
actorJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception groupHandle - '+str(e))
time.sleep(1)
tries+=1
if not actorJson: if not actorJson:
return False return False
return actorJson['type']=='Group' return actorJson['type']=='Group'
@ -1369,17 +1300,7 @@ def getGroupName(baseDir: str,handle: str) -> str:
actorFile=baseDir+'/accounts/'+handle+'.json' actorFile=baseDir+'/accounts/'+handle+'.json'
if not os.path.isfile(actorFile): if not os.path.isfile(actorFile):
return False return False
actorJson=None actorJson=loadJson(actorFile)
tries=0
while tries<5:
try:
with open(actorFile, 'r') as fp:
actorJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception getGroupName - '+str(e))
time.sleep(1)
tries+=1
if not actorJson: if not actorJson:
return 'Group' return 'Group'
return actorJson['name'] return actorJson['name']
@ -1659,20 +1580,7 @@ def inboxAfterCapabilities(session,keyId: str,handle: str,messageJson: {}, \
obtainAvatarForReplyPost(session,baseDir,httpPrefix,domain,personCache,postJsonObject,debug) obtainAvatarForReplyPost(session,baseDir,httpPrefix,domain,personCache,postJsonObject,debug)
# save the post to file # save the post to file
postSavedToFile=False if saveJson(postJsonObject,destinationFilename):
tries=0
while tries<5:
try:
with open(destinationFilename, 'w+') as fp:
commentjson.dump(postJsonObject, fp, indent=2, sort_keys=False)
postSavedToFile=True
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
if postSavedToFile:
if not inboxUpdateIndex(baseDir,handle,destinationFilename,debug): if not inboxUpdateIndex(baseDir,handle,destinationFilename,debug):
print('ERROR: unable to update inbox index') print('ERROR: unable to update inbox index')
@ -2062,17 +1970,7 @@ def runInboxQueue(projectVersion: str, \
if len(recipientsDictFollowers)>0: if len(recipientsDictFollowers)>0:
sharedInboxPostFilename=queueJson['destination'].replace(inboxHandle,inboxHandle) sharedInboxPostFilename=queueJson['destination'].replace(inboxHandle,inboxHandle)
if not os.path.isfile(sharedInboxPostFilename): if not os.path.isfile(sharedInboxPostFilename):
tries=0 saveJson(queueJson['post'],sharedInboxPostFilename)
while tries<5:
try:
with open(sharedInboxPostFilename, 'w') as fp:
commentjson.dump(queueJson['post'],fp,indent=2, \
sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
# for posts addressed to specific accounts # for posts addressed to specific accounts
for handle,capsId in recipientsDict.items(): for handle,capsId in recipientsDict.items():

50
like.py
View File

@ -16,6 +16,8 @@ from utils import getNicknameFromActor
from utils import getDomainFromActor from utils import getDomainFromActor
from utils import locatePost from utils import locatePost
from utils import getCachedPostFilename from utils import getCachedPostFilename
from utils import loadJson
from utils import saveJson
from posts import sendSignedJson from posts import sendSignedJson
from session import postJson from session import postJson
from webfinger import webfingerHandle from webfinger import webfingerHandle
@ -25,18 +27,7 @@ from posts import getPersonBox
def undoLikesCollectionEntry(baseDir: str,postFilename: str,objectUrl: str,actor: str,domain: str,debug: bool) -> None: def undoLikesCollectionEntry(baseDir: str,postFilename: str,objectUrl: str,actor: str,domain: str,debug: bool) -> None:
"""Undoes a like for a particular actor """Undoes a like for a particular actor
""" """
postJsonObject=None postJsonObject=loadJson(postFilename)
tries=0
while tries<5:
try:
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception undoLikesCollectionEntry - '+str(e))
time.sleep(1)
tries+=1
if postJsonObject: if postJsonObject:
# remove any cached version of this post so that the like icon is changed # remove any cached version of this post so that the like icon is changed
nickname=getNicknameFromActor(actor) nickname=getNicknameFromActor(actor)
@ -80,16 +71,7 @@ def undoLikesCollectionEntry(baseDir: str,postFilename: str,objectUrl: str,actor
del postJsonObject['object']['likes'] del postJsonObject['object']['likes']
else: else:
postJsonObject['object']['likes']['totalItems']=len(postJsonObject['likes']['items']) postJsonObject['object']['likes']['totalItems']=len(postJsonObject['likes']['items'])
tries=0 saveJson(postJsonObject,postFilename)
while tries<5:
try:
with open(postFilename, 'w') as fp:
commentjson.dump(postJsonObject, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
def likedByPerson(postJsonObject: {}, nickname: str,domain: str) -> bool: def likedByPerson(postJsonObject: {}, nickname: str,domain: str) -> bool:
"""Returns True if the given post is liked by the given person """Returns True if the given post is liked by the given person
@ -121,18 +103,7 @@ def noOfLikes(postJsonObject: {}) -> int:
def updateLikesCollection(baseDir: str,postFilename: str,objectUrl: str, actor: str,domain: str,debug: bool) -> None: def updateLikesCollection(baseDir: str,postFilename: str,objectUrl: str, actor: str,domain: str,debug: bool) -> None:
"""Updates the likes collection within a post """Updates the likes collection within a post
""" """
postJsonObject=None postJsonObject=loadJson(postFilename)
tries=0
while tries<5:
try:
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception updateLikesCollection - '+str(e))
time.sleep(1)
tries+=1
if postJsonObject: if postJsonObject:
# remove any cached version of this post so that the like icon is changed # remove any cached version of this post so that the like icon is changed
nickname=getNicknameFromActor(actor) nickname=getNicknameFromActor(actor)
@ -178,16 +149,7 @@ def updateLikesCollection(baseDir: str,postFilename: str,objectUrl: str, actor:
if debug: if debug:
print('DEBUG: saving post with likes added') print('DEBUG: saving post with likes added')
pprint(postJsonObject) pprint(postJsonObject)
tries=0 saveJson(postJsonObject,postFilename)
while tries<5:
try:
with open(postFilename, 'w') as fp:
commentjson.dump(postJsonObject, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
def like(session,baseDir: str,federationList: [],nickname: str,domain: str,port: int, \ def like(session,baseDir: str,federationList: [],nickname: str,domain: str,port: int, \
ccList: [],httpPrefix: str,objectUrl: str,clientToServer: bool, \ ccList: [],httpPrefix: str,objectUrl: str,clientToServer: bool, \

View File

@ -13,6 +13,8 @@ import time
from follow import followedAccountAccepts from follow import followedAccountAccepts
from follow import followedAccountRejects from follow import followedAccountRejects
from follow import removeFromFollowRequests from follow import removeFromFollowRequests
from utils import loadJson
from utils import saveJson
def manualDenyFollowRequest(session,baseDir: str, \ def manualDenyFollowRequest(session,baseDir: str, \
httpPrefix: str, httpPrefix: str,
@ -97,17 +99,7 @@ def manualApproveFollowRequest(session,baseDir: str, \
requestsDir=accountsDir+'/requests' requestsDir=accountsDir+'/requests'
followActivityfilename=requestsDir+'/'+handle+'.follow' followActivityfilename=requestsDir+'/'+handle+'.follow'
if os.path.isfile(followActivityfilename): if os.path.isfile(followActivityfilename):
followJson=None followJson=loadJson(followActivityfilename)
tries=0
while tries<5:
try:
with open(followActivityfilename, 'r') as fp:
followJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception manualApproveFollowRequest - '+str(e))
time.sleep(1)
tries+=1
if followJson: if followJson:
approveNickname=approveHandle.split('@')[0] approveNickname=approveHandle.split('@')[0]
approveDomain=approveHandle.split('@')[1].replace('\n','') approveDomain=approveHandle.split('@')[1].replace('\n','')

138
person.py
View File

@ -31,6 +31,8 @@ from roles import setRole
from media import removeMetaData from media import removeMetaData
from utils import validNickname from utils import validNickname
from utils import noOfAccounts from utils import noOfAccounts
from utils import loadJson
from utils import saveJson
from auth import createPassword from auth import createPassword
from config import setConfigParam from config import setConfigParam
from config import getConfigParam from config import getConfigParam
@ -91,31 +93,11 @@ def setProfileImage(baseDir: str,httpPrefix :str,nickname: str,domain: str, \
iconFilename=iconFilenameBase+'.gif' iconFilename=iconFilenameBase+'.gif'
profileFilename=baseDir+'/accounts/'+handle+'/'+iconFilename profileFilename=baseDir+'/accounts/'+handle+'/'+iconFilename
personJson=None personJson=loadJson(personFilename)
tries=0
while tries<5:
try:
with open(personFilename, 'r') as fp:
personJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception setProfileImage - '+str(e))
time.sleep(1)
tries+=1
if personJson: if personJson:
personJson[iconFilenameBase]['mediaType']=mediaType personJson[iconFilenameBase]['mediaType']=mediaType
personJson[iconFilenameBase]['url']=httpPrefix+'://'+fullDomain+'/users/'+nickname+'/'+iconFilename personJson[iconFilenameBase]['url']=httpPrefix+'://'+fullDomain+'/users/'+nickname+'/'+iconFilename
tries=0 saveJson(personJson,personFilename)
while tries<5:
try:
with open(personFilename, 'w') as fp:
commentjson.dump(personJson, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
cmd = '/usr/bin/convert '+imageFilename+' -size '+resolution+' -quality 50 '+profileFilename cmd = '/usr/bin/convert '+imageFilename+' -size '+resolution+' -quality 50 '+profileFilename
subprocess.call(cmd, shell=True) subprocess.call(cmd, shell=True)
@ -136,30 +118,10 @@ def setOrganizationScheme(baseDir: str,nickname: str,domain: str, \
if not os.path.isfile(actorFilename): if not os.path.isfile(actorFilename):
return False return False
actorJson=None actorJson=loadJson(actorFilename)
tries=0
while tries<5:
try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception setOrganizationScheme - '+str(e))
time.sleep(1)
tries+=1
if actorJson: if actorJson:
actorJson['orgSchema']=schema actorJson['orgSchema']=schema
tries=0 saveJson(actorJson,actorFilename)
while tries<5:
try:
with open(actorFilename, 'w') as fp:
commentjson.dump(actorJson, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
return True return True
def accountExists(baseDir: str,nickname: str,domain: str) -> bool: def accountExists(baseDir: str,nickname: str,domain: str) -> bool:
@ -279,16 +241,7 @@ def createPersonBase(baseDir: str,nickname: str,domain: str,port: int, \
if not os.path.isdir(baseDir+peopleSubdir+'/'+handle+'/queue'): if not os.path.isdir(baseDir+peopleSubdir+'/'+handle+'/queue'):
os.mkdir(baseDir+peopleSubdir+'/'+handle+'/queue') os.mkdir(baseDir+peopleSubdir+'/'+handle+'/queue')
filename=baseDir+peopleSubdir+'/'+handle+'.json' filename=baseDir+peopleSubdir+'/'+handle+'.json'
tries=0 saveJson(newPerson,filename)
while tries<5:
try:
with open(filename, 'w') as fp:
commentjson.dump(newPerson, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
# save to cache # save to cache
if not os.path.isdir(baseDir+'/cache'): if not os.path.isdir(baseDir+'/cache'):
@ -296,16 +249,7 @@ def createPersonBase(baseDir: str,nickname: str,domain: str,port: int, \
if not os.path.isdir(baseDir+'/cache/actors'): if not os.path.isdir(baseDir+'/cache/actors'):
os.mkdir(baseDir+'/cache/actors') os.mkdir(baseDir+'/cache/actors')
cacheFilename=baseDir+'/cache/actors/'+newPerson['id'].replace('/','#')+'.json' cacheFilename=baseDir+'/cache/actors/'+newPerson['id'].replace('/','#')+'.json'
tries=0 saveJson(newPerson,cacheFilename)
while tries<5:
try:
with open(cacheFilename, 'w') as fp:
commentjson.dump(newPerson, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
# save the private key # save the private key
privateKeysSubdir='/keys/private' privateKeysSubdir='/keys/private'
@ -453,20 +397,9 @@ def personLookup(domain: str,path: str,baseDir: str) -> {}:
filename=baseDir+'/accounts/'+handle+'.json' filename=baseDir+'/accounts/'+handle+'.json'
if not os.path.isfile(filename): if not os.path.isfile(filename):
return None return None
personJson={"user": "unknown"} personJson=loadJson(filename)
tries=0 #if not personJson:
while tries<5: # personJson={"user": "unknown"}
try:
with open(filename, 'r') as fp:
personJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception personLookup - '+str(e))
print('WARN: Failed to load actor '+filename)
time.sleep(1)
tries+=1
if tries>=5:
return None
return personJson return personJson
def personBoxJson(session,baseDir: str,domain: str,port: int,path: str, \ def personBoxJson(session,baseDir: str,domain: str,port: int,path: str, \
@ -578,31 +511,11 @@ def setDisplayNickname(baseDir: str,nickname: str, domain: str, \
if not os.path.isfile(filename): if not os.path.isfile(filename):
return False return False
personJson=None personJson=loadJson(filename)
tries=0
while tries<5:
try:
with open(filename, 'r') as fp:
personJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception setDisplayNickname - '+str(e))
time.sleep(1)
tries+=1
if not personJson: if not personJson:
return False return False
personJson['name']=displayName personJson['name']=displayName
tries=0 saveJson(personJson,filename)
while tries<5:
try:
with open(filename, 'w') as fp:
commentjson.dump(personJson, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
return True return True
def setBio(baseDir: str,nickname: str, domain: str, bio: str) -> bool: def setBio(baseDir: str,nickname: str, domain: str, bio: str) -> bool:
@ -613,35 +526,14 @@ def setBio(baseDir: str,nickname: str, domain: str, bio: str) -> bool:
if not os.path.isfile(filename): if not os.path.isfile(filename):
return False return False
personJson=None personJson=loadJson(filename)
tries=0
while tries<5:
try:
with open(filename, 'r') as fp:
personJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception setBio - '+str(e))
time.sleep(1)
tries+=1
if not personJson: if not personJson:
return False return False
if not personJson.get('summary'): if not personJson.get('summary'):
return False return False
personJson['summary']=bio personJson['summary']=bio
tries=0 saveJson(personJson,filename)
while tries<5:
try:
with open(filename, 'w') as fp:
commentjson.dump(personJson, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
return True return True
def isSuspended(baseDir: str,nickname: str) -> bool: def isSuspended(baseDir: str,nickname: str) -> bool:

100
posts.py
View File

@ -39,6 +39,8 @@ from utils import getDomainFromActor
from utils import deletePost from utils import deletePost
from utils import validNickname from utils import validNickname
from utils import locatePost from utils import locatePost
from utils import loadJson
from utils import saveJson
from capabilities import getOcapFilename from capabilities import getOcapFilename
from capabilities import capabilitiesUpdate from capabilities import capabilitiesUpdate
from media import attachMedia from media import attachMedia
@ -434,16 +436,7 @@ def savePostToBox(baseDir: str,httpPrefix: str,postId: str, \
boxDir = createPersonDir(nickname,domain,baseDir,boxname) boxDir = createPersonDir(nickname,domain,baseDir,boxname)
filename=boxDir+'/'+postId.replace('/','#')+'.json' filename=boxDir+'/'+postId.replace('/','#')+'.json'
tries=0 saveJson(postJsonObject,filename)
while tries<5:
try:
with open(filename, 'w') as fp:
commentjson.dump(postJsonObject, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
return filename return filename
def updateHashtagsIndex(baseDir: str,tag: {},newPostId: str) -> None: def updateHashtagsIndex(baseDir: str,tag: {},newPostId: str) -> None:
@ -540,17 +533,7 @@ def createPostBase(baseDir: str,nickname: str, domain: str, port: int, \
# the same warning # the same warning
replyPostFilename=locatePost(baseDir,nickname,domain,inReplyTo) replyPostFilename=locatePost(baseDir,nickname,domain,inReplyTo)
if replyPostFilename: if replyPostFilename:
replyToJson=None replyToJson=loadJson(replyPostFilename)
tries=0
while tries<5:
try:
with open(replyPostFilename, 'r') as fp:
replyToJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception createPostBase - '+str(e))
time.sleep(1)
tries+=1
if replyToJson: if replyToJson:
if replyToJson.get('object'): if replyToJson.get('object'):
if replyToJson['object'].get('sensitive'): if replyToJson['object'].get('sensitive'):
@ -605,18 +588,10 @@ def createPostBase(baseDir: str,nickname: str, domain: str, port: int, \
ocapFilename=getOcapFilename(baseDir,nickname,domain,toUrl,'granted') ocapFilename=getOcapFilename(baseDir,nickname,domain,toUrl,'granted')
if ocapFilename: if ocapFilename:
if os.path.isfile(ocapFilename): if os.path.isfile(ocapFilename):
tries=0 oc=loadJson(ocapFilename)
while tries<5: if oc:
try:
with open(ocapFilename, 'r') as fp:
oc=commentjson.load(fp)
if oc.get('id'): if oc.get('id'):
capabilityIdList=[oc['id']] capabilityIdList=[oc['id']]
break
except Exception as e:
print('WARN: commentjson exception createPostBase - '+str(e))
time.sleep(1)
tries+=1
newPost = { newPost = {
"@context": postContext, "@context": postContext,
'id': newPostId+'/activity', 'id': newPostId+'/activity',
@ -1816,17 +1791,9 @@ def createModeration(baseDir: str,nickname: str,domain: str,port: int,httpPrefix
for postUrl in pageLines: for postUrl in pageLines:
postFilename=boxDir+'/'+postUrl.replace('/','#')+'.json' postFilename=boxDir+'/'+postUrl.replace('/','#')+'.json'
if os.path.isfile(postFilename): if os.path.isfile(postFilename):
tries=0 postJsonObject=loadJson(postFilename)
while tries<5: if postJsonObject:
try:
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
boxItems['orderedItems'].append(postJsonObject) boxItems['orderedItems'].append(postJsonObject)
break
except Exception as e:
print('WARN: commentjson exception createModeration - '+str(e))
time.sleep(1)
tries+=1
if headerOnly: if headerOnly:
return boxHeader return boxHeader
@ -2402,19 +2369,8 @@ def populateRepliesJson(baseDir: str,nickname: str,domain: str, \
if os.path.isfile(searchFilename): if os.path.isfile(searchFilename):
if authorized or \ if authorized or \
'https://www.w3.org/ns/activitystreams#Public' in open(searchFilename).read(): 'https://www.w3.org/ns/activitystreams#Public' in open(searchFilename).read():
loadedPost=False postJsonObject=loadJson(searchFilename)
tries=0 if postJsonObject:
while tries<5:
try:
with open(searchFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
loadedPost=True
break
except Exception as e:
print('WARN: commentjson exception populateRepliesJson - '+str(e))
time.sleep(1)
tries+=1
if loadedPost:
if postJsonObject['object'].get('cc'): if postJsonObject['object'].get('cc'):
if authorized or \ if authorized or \
('https://www.w3.org/ns/activitystreams#Public' in postJsonObject['object']['to'] or \ ('https://www.w3.org/ns/activitystreams#Public' in postJsonObject['object']['to'] or \
@ -2438,19 +2394,8 @@ def populateRepliesJson(baseDir: str,nickname: str,domain: str, \
if authorized or \ if authorized or \
'https://www.w3.org/ns/activitystreams#Public' in open(searchFilename).read(): 'https://www.w3.org/ns/activitystreams#Public' in open(searchFilename).read():
# get the json of the reply and append it to the collection # get the json of the reply and append it to the collection
loadedPost=False postJsonObject=loadJson(searchFilename)
tries=0 if postJsonObject:
while tries<5:
try:
with open(searchFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
loadedPost=True
break
except Exception as e:
print('WARN: commentjson exception populateRepliesJson 2 - '+str(e))
time.sleep(1)
tries+=1
if loadedPost:
if postJsonObject['object'].get('cc'): if postJsonObject['object'].get('cc'):
if authorized or \ if authorized or \
('https://www.w3.org/ns/activitystreams#Public' in postJsonObject['object']['to'] or \ ('https://www.w3.org/ns/activitystreams#Public' in postJsonObject['object']['to'] or \
@ -2489,16 +2434,9 @@ def downloadAnnounce(session,baseDir: str,httpPrefix: str,nickname: str,domain:
if os.path.isfile(announceFilename): if os.path.isfile(announceFilename):
print('Reading cached Announce content for '+postJsonObject['object']) print('Reading cached Announce content for '+postJsonObject['object'])
tries=0 postJsonObject=loadJson(announceFilename)
while tries<5: if postJsonObject:
try:
with open(announceFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
return postJsonObject return postJsonObject
except Exception as e:
print('WARN: commentjson exception downloadAnnounce - '+str(e))
time.sleep(1)
tries+=1
else: else:
print('Downloading Announce content for '+postJsonObject['object']) print('Downloading Announce content for '+postJsonObject['object'])
asHeader={'Accept': 'application/activity+json; profile="https://www.w3.org/ns/activitystreams"'} asHeader={'Accept': 'application/activity+json; profile="https://www.w3.org/ns/activitystreams"'}
@ -2556,14 +2494,6 @@ def downloadAnnounce(session,baseDir: str,httpPrefix: str,nickname: str,domain:
rejectAnnounce(announceFilename) rejectAnnounce(announceFilename)
return None return None
postJsonObject=announcedJson postJsonObject=announcedJson
tries=0 if saveJson(postJsonObject,announceFilename):
while tries<5:
try:
with open(announceFilename, 'w') as fp:
commentjson.dump(postJsonObject, fp, indent=2, sort_keys=False)
return postJsonObject return postJsonObject
except Exception as e:
print(e)
time.sleep(1)
tries+=1
return None return None

View File

@ -16,6 +16,8 @@ from posts import getPersonBox
from session import postJson from session import postJson
from utils import getNicknameFromActor from utils import getNicknameFromActor
from utils import getDomainFromActor from utils import getDomainFromActor
from utils import loadJson
from utils import saveJson
def clearModeratorStatus(baseDir: str) -> None: def clearModeratorStatus(baseDir: str) -> None:
"""Removes moderator status from all accounts """Removes moderator status from all accounts
@ -29,32 +31,12 @@ def clearModeratorStatus(baseDir: str) -> None:
if filename.endswith(".json") and '@' in filename: if filename.endswith(".json") and '@' in filename:
filename=os.path.join(baseDir+'/accounts/', filename) filename=os.path.join(baseDir+'/accounts/', filename)
if '"moderator"' in open(filename).read(): if '"moderator"' in open(filename).read():
actorJson=None actorJson=loadJson(filename)
tries=0
while tries<5:
try:
with open(filename, 'r') as fp:
actorJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception clearModeratorStatus - '+str(e))
time.sleep(1)
tries+=1
if actorJson: if actorJson:
if actorJson['roles'].get('instance'): if actorJson['roles'].get('instance'):
if 'moderator' in actorJson['roles']['instance']: if 'moderator' in actorJson['roles']['instance']:
actorJson['roles']['instance'].remove('moderator') actorJson['roles']['instance'].remove('moderator')
tries=0 saveJson(actorJson,filename)
while tries<5:
try:
with open(filename, 'w') as fp:
commentjson.dump(actorJson, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
def addModerator(baseDir: str,nickname: str,domain: str) -> None: def addModerator(baseDir: str,nickname: str,domain: str) -> None:
"""Adds a moderator nickname to the file """Adds a moderator nickname to the file
@ -108,18 +90,7 @@ def setRole(baseDir: str,nickname: str,domain: str, \
if not os.path.isfile(actorFilename): if not os.path.isfile(actorFilename):
return False return False
actorJson=None actorJson=loadJson(actorFilename)
tries=0
while tries<5:
try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception setRole - '+str(e))
time.sleep(1)
tries+=1
if actorJson: if actorJson:
if role: if role:
# add the role # add the role
@ -139,16 +110,7 @@ def setRole(baseDir: str,nickname: str,domain: str, \
# if the project contains no roles then remove it # if the project contains no roles then remove it
if len(actorJson['roles'][project])==0: if len(actorJson['roles'][project])==0:
del actorJson['roles'][project] del actorJson['roles'][project]
tries=0 saveJson(actorJson,actorFilename)
while tries<5:
try:
with open(actorFilename, 'w') as fp:
commentjson.dump(actorJson, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
return True return True
def getRoles(baseDir: str,nickname: str,domain: str, \ def getRoles(baseDir: str,nickname: str,domain: str, \
@ -159,18 +121,7 @@ def getRoles(baseDir: str,nickname: str,domain: str, \
if not os.path.isfile(actorFilename): if not os.path.isfile(actorFilename):
return False return False
actorJson=None actorJson=loadJson(actorFilename)
tries=0
while tries<5:
try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception getRoles - '+str(e))
time.sleep(1)
tries+=1
if actorJson: if actorJson:
if not actorJson.get('roles'): if not actorJson.get('roles'):
return None return None

View File

@ -18,6 +18,8 @@ from session import postJson
from utils import validNickname from utils import validNickname
from utils import getNicknameFromActor from utils import getNicknameFromActor
from utils import getDomainFromActor from utils import getDomainFromActor
from utils import loadJson
from utils import saveJson
from media import removeMetaData from media import removeMetaData
def removeShare(baseDir: str,nickname: str,domain: str, \ def removeShare(baseDir: str,nickname: str,domain: str, \
@ -26,16 +28,7 @@ def removeShare(baseDir: str,nickname: str,domain: str, \
""" """
sharesFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/shares.json' sharesFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/shares.json'
if os.path.isfile(sharesFilename): if os.path.isfile(sharesFilename):
tries=0 sharesJson=loadJson(sharesFilename)
while tries<5:
try:
with open(sharesFilename, 'r') as fp:
sharesJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception removeShare - '+str(e))
time.sleep(1)
tries+=1
itemID=displayName.replace(' ','') itemID=displayName.replace(' ','')
if sharesJson.get(itemID): if sharesJson.get(itemID):
@ -50,16 +43,7 @@ def removeShare(baseDir: str,nickname: str,domain: str, \
os.remove(itemIDfile+'.gif') os.remove(itemIDfile+'.gif')
# remove the item itself # remove the item itself
del sharesJson[itemID] del sharesJson[itemID]
tries=0 saveJson(sharesJson,sharesFilename)
while tries<5:
try:
with open(sharesFilename, 'w') as fp:
commentjson.dump(sharesJson, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
def addShare(baseDir: str, \ def addShare(baseDir: str, \
httpPrefix: str,nickname: str,domain: str,port: int, \ httpPrefix: str,nickname: str,domain: str,port: int, \
@ -76,16 +60,7 @@ def addShare(baseDir: str, \
sharesFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/shares.json' sharesFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/shares.json'
sharesJson={} sharesJson={}
if os.path.isfile(sharesFilename): if os.path.isfile(sharesFilename):
tries=0 sharesJson=loadJson(sharesFilename)
while tries<5:
try:
with open(sharesFilename, 'r') as fp:
sharesJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception addShare - '+str(e))
time.sleep(1)
tries+=1
duration=duration.lower() duration=duration.lower()
durationSec=0 durationSec=0
@ -161,16 +136,7 @@ def addShare(baseDir: str, \
"expire": durationSec "expire": durationSec
} }
tries=0 saveJson(sharesJson,sharesFilename)
while tries<5:
try:
with open(sharesFilename, 'w') as fp:
commentjson.dump(sharesJson, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
def expireShares(baseDir: str) -> None: def expireShares(baseDir: str) -> None:
"""Removes expired items from shares """Removes expired items from shares
@ -192,17 +158,7 @@ def expireSharesForAccount(baseDir: str,nickname: str,domain: str) -> None:
handle=nickname+'@'+handleDomain handle=nickname+'@'+handleDomain
sharesFilename=baseDir+'/accounts/'+handle+'/shares.json' sharesFilename=baseDir+'/accounts/'+handle+'/shares.json'
if os.path.isfile(sharesFilename): if os.path.isfile(sharesFilename):
sharesJson=None sharesJson=loadJson(sharesFilename)
tries=0
while tries<5:
try:
with open(sharesFilename, 'r') as fp:
sharesJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception expireSharesForAccount - '+str(e))
time.sleep(1)
tries+=1
if sharesJson: if sharesJson:
currTime=int(time.time()) currTime=int(time.time())
deleteItemID=[] deleteItemID=[]
@ -220,16 +176,7 @@ def expireSharesForAccount(baseDir: str,nickname: str,domain: str) -> None:
os.remove(itemIDfile+'.jpg') os.remove(itemIDfile+'.jpg')
if os.path.isfile(itemIDfile+'.gif'): if os.path.isfile(itemIDfile+'.gif'):
os.remove(itemIDfile+'.gif') os.remove(itemIDfile+'.gif')
tries=0 saveJson(sharesJson,sharesFilename)
while tries<5:
try:
with open(sharesFilename, 'w') as fp:
commentjson.dump(sharesJson, fp, indent=2, sort_keys=False)
break
except Exception as e:
print('WARN: commentjson exception expireSharesForAccount 2 - '+str(e))
time.sleep(1)
tries+=1
def getSharesFeedForPerson(baseDir: str, \ def getSharesFeedForPerson(baseDir: str, \
domain: str,port: int, \ domain: str,port: int, \
@ -280,17 +227,9 @@ def getSharesFeedForPerson(baseDir: str, \
if headerOnly: if headerOnly:
noOfShares=0 noOfShares=0
if os.path.isfile(sharesFilename): if os.path.isfile(sharesFilename):
tries=0 sharesJson=loadJson(sharesFilename)
while tries<5: if sharesJson:
try:
with open(sharesFilename, 'r') as fp:
sharesJson=commentjson.load(fp)
noOfShares=len(sharesJson.items()) noOfShares=len(sharesJson.items())
break
except Exception as e:
print('WARN: commentjson exception getSharesFeedForPerson - '+str(e))
time.sleep(1)
tries+=1
shares = { shares = {
'@context': 'https://www.w3.org/ns/activitystreams', '@context': 'https://www.w3.org/ns/activitystreams',
'first': httpPrefix+'://'+domain+'/users/'+nickname+'/shares?page=1', 'first': httpPrefix+'://'+domain+'/users/'+nickname+'/shares?page=1',
@ -318,18 +257,7 @@ def getSharesFeedForPerson(baseDir: str, \
pageCtr=0 pageCtr=0
totalCtr=0 totalCtr=0
sharesJson=None sharesJson=loadJson(sharesFilename)
tries=0
while tries<5:
try:
with open(sharesFilename, 'r') as fp:
sharesJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception getSharesFeedForPerson 2 - '+str(e))
time.sleep(1)
tries+=1
if sharesJson: if sharesJson:
for itemID,item in sharesJson.items(): for itemID,item in sharesJson.items():
pageCtr += 1 pageCtr += 1

View File

@ -16,6 +16,8 @@ from posts import getPersonBox
from session import postJson from session import postJson
from utils import getNicknameFromActor from utils import getNicknameFromActor
from utils import getDomainFromActor from utils import getDomainFromActor
from utils import loadJson
from utils import saveJson
def setSkillLevel(baseDir: str,nickname: str,domain: str, \ def setSkillLevel(baseDir: str,nickname: str,domain: str, \
skill: str,skillLevelPercent: int) -> bool: skill: str,skillLevelPercent: int) -> bool:
@ -28,18 +30,7 @@ def setSkillLevel(baseDir: str,nickname: str,domain: str, \
if not os.path.isfile(actorFilename): if not os.path.isfile(actorFilename):
return False return False
actorJson=None actorJson=loadJson(actorFilename)
tries=0
while tries<5:
try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception setSkillLevel - '+str(e))
time.sleep(1)
tries+=1
if actorJson: if actorJson:
if not actorJson.get('skills'): if not actorJson.get('skills'):
actorJson['skills']={} actorJson['skills']={}
@ -47,16 +38,7 @@ def setSkillLevel(baseDir: str,nickname: str,domain: str, \
actorJson['skills'][skill]=skillLevelPercent actorJson['skills'][skill]=skillLevelPercent
else: else:
del actorJson['skills'][skill] del actorJson['skills'][skill]
tries=0 saveJson(actorJson,actorFilename)
while tries<5:
try:
with open(actorFilename, 'w') as fp:
commentjson.dump(actorJson, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
return True return True
def setSkills(baseDir: str,nickname: str,domain: str,skills: {}) -> None: def setSkills(baseDir: str,nickname: str,domain: str,skills: {}) -> None:
@ -64,30 +46,10 @@ def setSkills(baseDir: str,nickname: str,domain: str,skills: {}) -> None:
if not os.path.isfile(actorFilename): if not os.path.isfile(actorFilename):
return False return False
actorJson=None actorJson=loadJson(actorFilename)
tries=0
while tries<5:
try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception setSkills - '+str(e))
time.sleep(1)
tries+=1
if actorJson: if actorJson:
actorJson['skills']=skills actorJson['skills']=skills
tries=0 saveJson(actorJson,actorFilename)
while tries<5:
try:
with open(actorFilename, 'w') as fp:
commentjson.dump(actorJson, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
def getSkills(baseDir: str,nickname: str,domain: str) -> []: def getSkills(baseDir: str,nickname: str,domain: str) -> []:
"""Returns the skills for a given person """Returns the skills for a given person
@ -96,18 +58,7 @@ def getSkills(baseDir: str,nickname: str,domain: str) -> []:
if not os.path.isfile(actorFilename): if not os.path.isfile(actorFilename):
return False return False
actorJson=None actorJson=loadJson(actorFilename)
tries=0
while tries<5:
try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception getSkills - '+str(e))
time.sleep(1)
tries+=1
if actorJson: if actorJson:
if not actorJson.get('skills'): if not actorJson.get('skills'):
return None return None

View File

@ -12,6 +12,37 @@ import shutil
import datetime import datetime
import commentjson import commentjson
def saveJson(jsonObject: {},filename: str) -> bool:
"""Saves json to a file
"""
tries=0
while tries<5:
try:
with open(filename, 'w') as fp:
commentjson.dump(jsonObject, fp, indent=2, sort_keys=False)
return True
except Exception as e:
print(e)
time.sleep(1)
tries+=1
return False
def loadJson(filename: str) -> {}:
"""Makes a few attempts to load a json formatted file
"""
jsonObject=None
tries=0
while tries<5:
try:
with open(filename, 'r') as fp:
jsonObject=commentjson.load(fp)
break
except Exception as e:
print('WARN: loadJson exception - '+str(e))
time.sleep(2)
tries+=1
return jsonObject
def getStatusNumber() -> (str,str): def getStatusNumber() -> (str,str):
"""Returns the status number and published date """Returns the status number and published date
""" """

View File

@ -17,6 +17,8 @@ import time
from session import getJson from session import getJson
from cache import storeWebfingerInCache from cache import storeWebfingerInCache
from cache import getWebfingerFromCache from cache import getWebfingerFromCache
from utils import loadJson
from utils import saveJson
def parseHandle(handle: str) -> (str,str): def parseHandle(handle: str) -> (str,str):
if '.' not in handle: if '.' not in handle:
@ -92,29 +94,11 @@ def storeWebfingerEndpoint(nickname: str,domain: str,port: int,baseDir: str, \
if not os.path.isdir(baseDir+wfSubdir): if not os.path.isdir(baseDir+wfSubdir):
os.mkdir(baseDir+wfSubdir) os.mkdir(baseDir+wfSubdir)
filename=baseDir+wfSubdir+'/'+handle.lower()+'.json' filename=baseDir+wfSubdir+'/'+handle.lower()+'.json'
tries=0 saveJson(wfJson,filename)
while tries<5:
try:
with open(filename, 'w') as fp:
commentjson.dump(wfJson, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
if nickname=='inbox': if nickname=='inbox':
handle=originalDomain+'@'+domain handle=originalDomain+'@'+domain
filename=baseDir+wfSubdir+'/'+handle.lower()+'.json' filename=baseDir+wfSubdir+'/'+handle.lower()+'.json'
tries=0 saveJson(wfJson,filename)
while tries<5:
try:
with open(filename, 'w') as fp:
commentjson.dump(wfJson, fp, indent=2, sort_keys=False)
break
except Exception as e:
print(e)
time.sleep(1)
tries+=1
return True return True
def createWebfingerEndpoint(nickname: str,domain: str,port: int, \ def createWebfingerEndpoint(nickname: str,domain: str,port: int, \
@ -232,15 +216,7 @@ def webfingerLookup(path: str,baseDir: str,port: int,debug: bool) -> {}:
if debug: if debug:
print('DEBUG: WEBFINGER filename not found '+filename) print('DEBUG: WEBFINGER filename not found '+filename)
return None return None
wfJson=loadJson(filename)
if not wfJson:
wfJson={"nickname": "unknown"} wfJson={"nickname": "unknown"}
tries=0
while tries<5:
try:
with open(filename, 'r') as fp:
wfJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception webfingerLookup - '+str(e))
time.sleep(1)
tries+=1
return wfJson return wfJson

View File

@ -25,6 +25,8 @@ from utils import isPublicPost
from utils import getDisplayName from utils import getDisplayName
from utils import getCachedPostDirectory from utils import getCachedPostDirectory
from utils import getCachedPostFilename from utils import getCachedPostFilename
from utils import loadJson
from utils import saveJson
from follow import isFollowingActor from follow import isFollowingActor
from webfinger import webfingerHandle from webfinger import webfingerHandle
from posts import isDM from posts import isDM
@ -155,19 +157,8 @@ def htmlSearchEmoji(translate: {},baseDir: str,searchStr: str) -> str:
emojiForm+=htmlFooter() emojiForm+=htmlFooter()
return emojiForm return emojiForm
loadedEmoji=False emojiJson=loadJson(emojiLookupFilename)
tries=0 if emojiJson:
while tries<5:
try:
with open(emojiLookupFilename, 'r') as fp:
emojiJson=commentjson.load(fp)
loadedEmoji=True
break
except Exception as e:
print('WARN: commentjson exception htmlSearchEmoji - '+str(e))
time.sleep(1)
tries+=1
if loadedEmoji:
results={} results={}
for emojiName,filename in emojiJson.items(): for emojiName,filename in emojiJson.items():
if searchStr in emojiName: if searchStr in emojiName:
@ -229,17 +220,7 @@ def htmlSearchSharedItems(translate: {}, \
if not os.path.isfile(sharesFilename): if not os.path.isfile(sharesFilename):
continue continue
sharesJson=None sharesJson=loadJson(sharesFilename)
tries=0
while tries<5:
try:
with open(sharesFilename, 'r') as fp:
sharesJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception htmlSearchSharedItems - '+str(e))
time.sleep(1)
tries+=1
if not sharesJson: if not sharesJson:
continue continue
@ -395,19 +376,8 @@ def htmlHashtagSearch(translate: {}, \
if not postFilename: if not postFilename:
index-=1 index-=1
continue continue
loadedPost=False postJsonObject=loadJson(postFilename)
tries=0 if postJsonObject:
while tries<5:
try:
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
loadedPost=True
break
except Exception as e:
print('WARN: commentjson exception htmlHashtagSearch - '+str(e))
time.sleep(1)
tries+=1
if loadedPost:
if not isPublicPost(postJsonObject): if not isPublicPost(postJsonObject):
index-=1 index-=1
continue continue
@ -447,19 +417,8 @@ def htmlSkillsSearch(translate: {},baseDir: str, \
if f.startswith('inbox@'): if f.startswith('inbox@'):
continue continue
actorFilename = os.path.join(subdir, f) actorFilename = os.path.join(subdir, f)
loadedActor=False actorJson=loadJson(actorFilename)
tries=0 if actorJson:
while tries<5:
try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
loadedActor=True
break
except Exception as e:
print('WARN: commentjson exception htmlSkillsSearch - '+str(e))
time.sleep(1)
tries+=1
if loadedActor:
if actorJson.get('id') and \ if actorJson.get('id') and \
actorJson.get('skills') and \ actorJson.get('skills') and \
actorJson.get('name') and \ actorJson.get('name') and \
@ -487,19 +446,8 @@ def htmlSkillsSearch(translate: {},baseDir: str, \
if f.startswith('inbox@'): if f.startswith('inbox@'):
continue continue
actorFilename = os.path.join(subdir, f) actorFilename = os.path.join(subdir, f)
loadedActor=False cachedActorJson=loadJson(actorFilename)
tries=0 if cachedActorJson:
while tries<5:
try:
with open(actorFilename, 'r') as fp:
cachedActorJson=commentjson.load(fp)
loadedActor=True
break
except Exception as e:
print('WARN: commentjson exception htmlSkillsSearch - '+str(e))
time.sleep(1)
tries+=1
if loadedActor:
if cachedActorJson.get('actor'): if cachedActorJson.get('actor'):
actorJson=cachedActorJson['actor'] actorJson=cachedActorJson['actor']
if actorJson.get('id') and \ if actorJson.get('id') and \
@ -573,19 +521,8 @@ def htmlEditProfile(translate: {},baseDir: str,path: str,domain: str,port: int)
displayNickname=nickname displayNickname=nickname
bioStr='' bioStr=''
manuallyApprovesFollowers='' manuallyApprovesFollowers=''
loadedActor=False actorJson=loadJson(actorFilename)
tries=0 if actorJson:
while tries<5:
try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
loadedActor=True
break
except Exception as e:
print('WARN: commentjson exception htmlEditProfile - '+str(e))
time.sleep(1)
tries+=1
if loadedActor:
if actorJson.get('name'): if actorJson.get('name'):
displayNickname=actorJson['name'] displayNickname=actorJson['name']
if actorJson.get('summary'): if actorJson.get('summary'):
@ -1616,19 +1553,8 @@ def followerApprovalActive(baseDir: str,nickname: str,domain: str) -> bool:
manuallyApprovesFollowers=False manuallyApprovesFollowers=False
actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json' actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json'
if os.path.isfile(actorFilename): if os.path.isfile(actorFilename):
loadedActor=False actorJson=loadJson(actorFilename)
tries=0 if actorJson:
while tries<5:
try:
with open(actorFilename, 'r') as fp:
actorJson=commentjson.load(fp)
loadedActor=True
break
except Exception as e:
print('WARN: commentjson exception followerApprovalActive - '+str(e))
time.sleep(1)
tries+=1
if loadedActor:
if actorJson.get('manuallyApprovesFollowers'): if actorJson.get('manuallyApprovesFollowers'):
manuallyApprovesFollowers=actorJson['manuallyApprovesFollowers'] manuallyApprovesFollowers=actorJson['manuallyApprovesFollowers']
return manuallyApprovesFollowers return manuallyApprovesFollowers
@ -2460,19 +2386,8 @@ def htmlIndividualPost(translate: {}, \
postFilename=locatePost(baseDir,nickname,domain,postJsonObject['object']['inReplyTo']) postFilename=locatePost(baseDir,nickname,domain,postJsonObject['object']['inReplyTo'])
if not postFilename: if not postFilename:
break break
loadedPost=False postJsonObject=loadJson(postFilename)
tries=0 if postJsonObject:
while tries<5:
try:
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
loadedPost=True
break
except Exception as e:
print('WARN: commentjson exception htmlIndividualPost - '+str(e))
time.sleep(1)
tries+=1
if loadedPost:
postStr= \ postStr= \
individualPostAsHtml(iconsDir,translate,None, \ individualPostAsHtml(iconsDir,translate,None, \
baseDir,session,wfRequest,personCache, \ baseDir,session,wfRequest,personCache, \
@ -2537,18 +2452,7 @@ def htmlRemoveSharedItem(translate: {},baseDir: str,actor: str,shareName: str) -
sharesFile=baseDir+'/accounts/'+nickname+'@'+domain+'/shares.json' sharesFile=baseDir+'/accounts/'+nickname+'@'+domain+'/shares.json'
if not os.path.isfile(sharesFile): if not os.path.isfile(sharesFile):
return None return None
sharesJson=None sharesJson=loadJson(sharesFile)
tries=0
while tries<5:
try:
with open(sharesFile, 'r') as fp:
sharesJson=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception htmlRemoveSharedItem - '+str(e))
time.sleep(1)
tries+=1
if not sharesJson: if not sharesJson:
return None return None
if not sharesJson.get(shareName): if not sharesJson.get(shareName):
@ -2604,17 +2508,7 @@ def htmlDeletePost(translate,pageNumber: int, \
if not postFilename: if not postFilename:
return None return None
postJsonObject=None postJsonObject=loadJson(postFilename)
tries=0
while tries<5:
try:
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception htmlDeletePost - '+str(e))
time.sleep(1)
tries+=1
if not postJsonObject: if not postJsonObject:
return None return None
@ -2918,18 +2812,7 @@ def getCalendarEvents(baseDir: str,nickname: str,domain: str,year: int,monthNumb
postId=postId.replace('\n','') postId=postId.replace('\n','')
postFilename=locatePost(baseDir,nickname,domain,postId) postFilename=locatePost(baseDir,nickname,domain,postId)
if postFilename: if postFilename:
postJsonObject=None postJsonObject=loadJson(postFilename)
tries=0
postJsonObject=None
while tries<5:
try:
with open(postFilename, 'r') as fp:
postJsonObject=commentjson.load(fp)
break
except Exception as e:
print('WARN: commentjson exception getCalendarEvents - '+str(e))
time.sleep(1)
tries+=1
if postJsonObject: if postJsonObject:
if postJsonObject.get('object'): if postJsonObject.get('object'):
if isinstance(postJsonObject['object'], dict): if isinstance(postJsonObject['object'], dict):