flake8 format

main
Bob Mottram 2020-04-04 12:27:51 +01:00
parent d652485f8a
commit b7fc2b948d
1 changed files with 291 additions and 257 deletions

236
shares.py
View File

@ -6,32 +6,36 @@ __maintainer__="Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
import json
import os
import time
from shutil import copyfile
from webfinger import webfingerHandle
from auth import createBasicAuthHeader
from posts import getPersonBox
from session import postJson
from session import postImage
from utils import validNickname
from utils import getNicknameFromActor
from utils import getDomainFromActor
from utils import loadJson
from utils import saveJson
from media import removeMetaData
def getValidSharedItemID(displayName: str) -> str:
"""Removes any invalid characters from the display name to
produce an item ID
"""
return displayName.replace(' ','').replace('+','-').replace('/','-').replace('\\','-').replace('.','_').replace('?','-').replace('\n','').replace("","'").replace('&','-')
displayName = displayName.replace(' ', '').replace('+', '-')
displayName = displayName.replace('/', '-').replace('\\', '-')
displayName = displayName.replace('.', '_').replace('?', '-')
displayName = displayName.replace('\n', '').replace("", "'")
return displayName.replace('&', '-')
def removeShare(baseDir: str,nickname: str,domain: str, \
def removeShare(baseDir: str, nickname: str, domain: str,
displayName: str) -> None:
"""Removes a share for a person
"""
sharesFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/shares.json'
sharesFilename = baseDir + '/accounts/' + \
nickname + '@' + domain + '/shares.json'
if not os.path.isfile(sharesFilename):
print('ERROR: missing shares.json ' + sharesFilename)
return
@ -59,21 +63,19 @@ def removeShare(baseDir: str,nickname: str,domain: str, \
del sharesJson[itemID]
saveJson(sharesJson, sharesFilename)
else:
print('ERROR: share index "'+itemID+'" does not exist in '+sharesFilename)
print('ERROR: share index "' + itemID +
'" does not exist in ' + sharesFilename)
def addShare(baseDir: str, \
httpPrefix: str,nickname: str,domain: str,port: int, \
displayName: str, \
summary: str, \
imageFilename: str, \
itemType: str, \
itemCategory: str, \
location: str, \
duration: str,
debug: bool) -> None:
def addShare(baseDir: str,
httpPrefix: str, nickname: str, domain: str, port: int,
displayName: str, summary: str, imageFilename: str,
itemType: str, itemCategory: str, location: str,
duration: str, debug: bool) -> None:
"""Updates the likes collection within a post
"""
sharesFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/shares.json'
sharesFilename = baseDir + '/accounts/' + \
nickname + '@' + domain + '/shares.json'
sharesJson = {}
if os.path.isfile(sharesFilename):
sharesJson = loadJson(sharesFilename)
@ -89,11 +91,14 @@ def addShare(baseDir: str, \
if 'day' in durationList[1]:
durationSec = published + (int(durationList[0]) * 60 * 60 * 24)
if 'week' in durationList[1]:
durationSec=published+(int(durationList[0])*60*60*24*7)
durationSec = \
published + (int(durationList[0]) * 60 * 60 * 24 * 7)
if 'month' in durationList[1]:
durationSec=published+(int(durationList[0])*60*60*24*30)
durationSec = \
published + (int(durationList[0]) * 60 * 60 * 24 * 30)
if 'year' in durationList[1]:
durationSec=published+(int(durationList[0])*60*60*24*365)
durationSec = \
published + (int(durationList[0]) * 60 * 60 * 24 * 365)
itemID = getValidSharedItemID(displayName)
@ -101,7 +106,8 @@ def addShare(baseDir: str, \
imageUrl = None
moveImage = False
if not imageFilename:
sharesImageFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/upload'
sharesImageFilename = \
baseDir + '/accounts/' + nickname + '@' + domain + '/upload'
if os.path.isfile(sharesImageFilename + '.png'):
imageFilename = sharesImageFilename + '.png'
moveImage = True
@ -130,17 +136,23 @@ def addShare(baseDir: str, \
removeMetaData(imageFilename, itemIDfile + '.png')
if moveImage:
os.remove(imageFilename)
imageUrl=httpPrefix+'://'+domainFull+'/sharefiles/'+nickname+'/'+itemID+'.png'
imageUrl = \
httpPrefix + '://' + domainFull + \
'/sharefiles/' + nickname + '/' + itemID + '.png'
if imageFilename.endswith('.jpg'):
removeMetaData(imageFilename, itemIDfile + '.jpg')
if moveImage:
os.remove(imageFilename)
imageUrl=httpPrefix+'://'+domainFull+'/sharefiles/'+nickname+'/'+itemID+'.jpg'
imageUrl = \
httpPrefix + '://' + domainFull + \
'/sharefiles/' + nickname + '/' + itemID + '.jpg'
if imageFilename.endswith('.gif'):
removeMetaData(imageFilename, itemIDfile + '.gif')
if moveImage:
os.remove(imageFilename)
imageUrl=httpPrefix+'://'+domainFull+'/sharefiles/'+nickname+'/'+itemID+'.gif'
imageUrl = \
httpPrefix + '://' + domainFull + \
'/sharefiles/' + nickname + '/' + itemID + '.gif'
sharesJson[itemID] = {
"displayName": displayName,
@ -165,10 +177,12 @@ def addShare(baseDir: str, \
nickname = handle.split('@')[0]
try:
with open(newShareFile, 'w') as fp:
fp.write(httpPrefix+'://'+domainFull+'/users/'+nickname+'/tlshares')
except:
fp.write(httpPrefix + '://' + domainFull +
'/users/' + nickname + '/tlshares')
except BaseException:
pass
def expireShares(baseDir: str) -> None:
"""Removes expired items from shares
"""
@ -180,6 +194,7 @@ def expireShares(baseDir: str) -> None:
domain = account.split('@')[1]
expireSharesForAccount(baseDir, nickname, domain)
def expireSharesForAccount(baseDir: str, nickname: str, domain: str) -> None:
"""Removes expired items from shares
"""
@ -200,7 +215,8 @@ def expireSharesForAccount(baseDir: str,nickname: str,domain: str) -> None:
for itemID in deleteItemID:
del sharesJson[itemID]
# remove any associated images
itemIDfile=baseDir+'/sharefiles/'+nickname+'/'+itemID
itemIDfile = \
baseDir + '/sharefiles/' + nickname + '/' + itemID
if os.path.isfile(itemIDfile + '.png'):
os.remove(itemIDfile + '.png')
if os.path.isfile(itemIDfile + '.jpg'):
@ -209,9 +225,10 @@ def expireSharesForAccount(baseDir: str,nickname: str,domain: str) -> None:
os.remove(itemIDfile + '.gif')
saveJson(sharesJson, sharesFilename)
def getSharesFeedForPerson(baseDir: str, \
domain: str,port: int, \
path: str,httpPrefix: str, \
def getSharesFeedForPerson(baseDir: str,
domain: str, port: int,
path: str, httpPrefix: str,
sharesPerPage=12) -> {}:
"""Returns the shares for an account from GET requests
"""
@ -227,7 +244,7 @@ def getSharesFeedForPerson(baseDir: str, \
else:
try:
pageNumber = int(pageNumber)
except:
except BaseException:
pass
path = path.split('?page=')[0]
headerOnly = False
@ -261,10 +278,11 @@ def getSharesFeedForPerson(baseDir: str, \
sharesJson = loadJson(sharesFilename)
if sharesJson:
noOfShares = len(sharesJson.items())
idStr = httpPrefix + '://' + domain + '/users/' + nickname
shares = {
'@context': 'https://www.w3.org/ns/activitystreams',
'first': httpPrefix+'://'+domain+'/users/'+nickname+'/shares?page=1',
'id': httpPrefix+'://'+domain+'/users/'+nickname+'/shares',
'first': idStr+'/shares?page=1',
'id': idStr+'/shares',
'totalItems': str(noOfShares),
'type': 'OrderedCollection'
}
@ -274,11 +292,12 @@ def getSharesFeedForPerson(baseDir: str, \
pageNumber = 1
nextPageNumber = int(pageNumber + 1)
idStr = httpPrefix + '://' + domain + '/users/' + nickname
shares = {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': httpPrefix+'://'+domain+'/users/'+nickname+'/shares?page='+str(pageNumber),
'id': idStr+'/shares?page='+str(pageNumber),
'orderedItems': [],
'partOf': httpPrefix+'://'+domain+'/users/'+nickname+'/shares',
'partOf': idStr+'/shares',
'totalItems': 0,
'type': 'OrderedCollectionPage'
}
@ -305,23 +324,21 @@ def getSharesFeedForPerson(baseDir: str, \
if lastPage < 1:
lastPage = 1
if nextPageNumber > lastPage:
shares['next']=httpPrefix+'://'+domain+'/users/'+nickname+'/shares?page='+str(lastPage)
shares['next'] = \
httpPrefix + '://' + domain + '/users/' + nickname + \
'/shares?page=' + str(lastPage)
return shares
def sendShareViaServer(baseDir,session, \
fromNickname: str,password: str, \
fromDomain: str,fromPort: int, \
httpPrefix: str, \
displayName: str, \
summary: str, \
imageFilename: str, \
itemType: str, \
itemCategory: str, \
location: str, \
duration: str, \
cachedWebfingers: {},personCache: {}, \
debug: bool, \
projectVersion: str) -> {}:
def sendShareViaServer(baseDir, session,
fromNickname: str, password: str,
fromDomain: str, fromPort: int,
httpPrefix: str, displayName: str,
summary: str, imageFilename: str,
itemType: str, itemCategory: str,
location: str, duration: str,
cachedWebfingers: {}, personCache: {},
debug: bool, projectVersion: str) -> {}:
"""Creates an item share via c2s
"""
if not session:
@ -335,19 +352,21 @@ def sendShareViaServer(baseDir,session, \
fromDomainFull = fromDomain + ':' + str(fromPort)
toUrl = 'https://www.w3.org/ns/activitystreams#Public'
ccUrl=httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname+'/followers'
ccUrl = httpPrefix + '://' + fromDomainFull + \
'/users/' + fromNickname + '/followers'
actor = httpPrefix + '://' + fromDomainFull + '/users/' + fromNickname
newShareJson = {
"@context": "https://www.w3.org/ns/activitystreams",
'type': 'Add',
'actor': httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname,
'target': httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname+'/shares',
'actor': actor,
'target': actor+'/shares',
'object': {
"type": "Offer",
"displayName": displayName,
"summary": summary,
"itemType": itemType,
"category": category,
"category": itemCategory,
"location": location,
"duration": duration,
'to': [toUrl],
@ -360,7 +379,9 @@ def sendShareViaServer(baseDir,session, \
handle = httpPrefix + '://' + fromDomainFull + '/@' + fromNickname
# lookup the inbox for the To handle
wfRequest=webfingerHandle(session,handle,httpPrefix,cachedWebfingers, \
wfRequest = \
webfingerHandle(session, handle, httpPrefix,
cachedWebfingers,
fromDomain, projectVersion)
if not wfRequest:
if debug:
@ -370,10 +391,13 @@ def sendShareViaServer(baseDir,session, \
postToBox = 'outbox'
# get the actor inbox for the To handle
inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl,displayName= \
getPersonBox(baseDir,session,wfRequest,personCache, \
projectVersion,httpPrefix, \
fromNickname,fromDomain,postToBox)
(inboxUrl, pubKeyId, pubKey,
fromPersonId, sharedInbox,
capabilityAcquisition,
avatarUrl, displayName) = getPersonBox(baseDir, session, wfRequest,
personCache, projectVersion,
httpPrefix, fromNickname,
fromDomain, postToBox)
if not inboxUrl:
if debug:
@ -388,22 +412,24 @@ def sendShareViaServer(baseDir,session, \
if imageFilename:
headers = {
'host': fromDomain, \
'host': fromDomain,
'Authorization': authHeader
}
postResult = \
postImage(session,imageFilename,[],inboxUrl.replace('/'+postToBox,'/shares'),headers,"inbox:write")
postImage(session, imageFilename, [],
inboxUrl.replace('/' + postToBox, '/shares'),
headers, "inbox:write")
headers = {
'host': fromDomain, \
'Content-type': 'application/json', \
'host': fromDomain,
'Content-type': 'application/json',
'Authorization': authHeader
}
postResult = \
postJson(session, newShareJson, [], inboxUrl, headers, "inbox:write")
#if not postResult:
# if debug:
# print('DEBUG: POST announce failed for c2s to '+inboxUrl)
if not postResult:
if debug:
print('DEBUG: POST announce failed for c2s to ' + inboxUrl)
# return 5
if debug:
@ -411,12 +437,12 @@ def sendShareViaServer(baseDir,session, \
return newShareJson
def sendUndoShareViaServer(baseDir: str,session, \
fromNickname: str,password: str, \
fromDomain: str,fromPort: int, \
httpPrefix: str, \
displayName: str, \
cachedWebfingers: {},personCache: {}, \
def sendUndoShareViaServer(baseDir: str, session,
fromNickname: str, password: str,
fromDomain: str, fromPort: int,
httpPrefix: str, displayName: str,
cachedWebfingers: {}, personCache: {},
debug: bool, projectVersion: str) -> {}:
"""Undoes a share via c2s
"""
@ -431,13 +457,15 @@ def sendUndoShareViaServer(baseDir: str,session, \
fromDomainFull = fromDomain + ':' + str(fromPort)
toUrl = 'https://www.w3.org/ns/activitystreams#Public'
ccUrl=httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname+'/followers'
ccUrl = httpPrefix + '://' + fromDomainFull + \
'/users/' + fromNickname + '/followers'
actor = httpPrefix + '://' + fromDomainFull + '/users/' + fromNickname
undoShareJson = {
"@context": "https://www.w3.org/ns/activitystreams",
'type': 'Remove',
'actor': httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname,
'target': httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname+'/shares',
'actor': actor,
'target': actor + '/shares',
'object': {
"type": "Offer",
"displayName": displayName,
@ -451,7 +479,8 @@ def sendUndoShareViaServer(baseDir: str,session, \
handle = httpPrefix + '://' + fromDomainFull + '/@' + fromNickname
# lookup the inbox for the To handle
wfRequest=webfingerHandle(session,handle,httpPrefix,cachedWebfingers, \
wfRequest = \
webfingerHandle(session, handle, httpPrefix, cachedWebfingers,
fromDomain, projectVersion)
if not wfRequest:
if debug:
@ -461,10 +490,13 @@ def sendUndoShareViaServer(baseDir: str,session, \
postToBox = 'outbox'
# get the actor inbox for the To handle
inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl,displayName= \
getPersonBox(baseDir,session,wfRequest,personCache, \
projectVersion,httpPrefix, \
fromNickname,fromDomain,postToBox)
(inboxUrl, pubKeyId, pubKey,
fromPersonId, sharedInbox,
capabilityAcquisition,
avatarUrl, displayName) = getPersonBox(baseDir, session, wfRequest,
personCache, projectVersion,
httpPrefix, fromNickname,
fromDomain, postToBox)
if not inboxUrl:
if debug:
@ -478,15 +510,15 @@ def sendUndoShareViaServer(baseDir: str,session, \
authHeader = createBasicAuthHeader(fromNickname, password)
headers = {
'host': fromDomain, \
'Content-type': 'application/json', \
'host': fromDomain,
'Content-type': 'application/json',
'Authorization': authHeader
}
postResult = \
postJson(session, undoShareJson, [], inboxUrl, headers, "inbox:write")
#if not postResult:
# if debug:
# print('DEBUG: POST announce failed for c2s to '+inboxUrl)
if not postResult:
if debug:
print('DEBUG: POST announce failed for c2s to ' + inboxUrl)
# return 5
if debug:
@ -494,8 +526,9 @@ def sendUndoShareViaServer(baseDir: str,session, \
return undoShareJson
def outboxShareUpload(baseDir: str,httpPrefix: str, \
nickname: str,domain: str,port: int, \
def outboxShareUpload(baseDir: str, httpPrefix: str,
nickname: str, domain: str, port: int,
messageJson: {}, debug: bool) -> None:
""" When a shared item is received by the outbox from c2s
"""
@ -539,21 +572,22 @@ def outboxShareUpload(baseDir: str,httpPrefix: str, \
if debug:
print('DEBUG: duration missing from Offer')
return
addShare(baseDir, \
httpPrefix,nickname,domain,port, \
messageJson['object']['displayName'], \
messageJson['object']['summary'], \
messageJson['object']['imageFilename'], \
messageJson['object']['itemType'], \
messageJson['object']['itemCategory'], \
messageJson['object']['location'], \
messageJson['object']['duration'], \
addShare(baseDir,
httpPrefix, nickname, domain, port,
messageJson['object']['displayName'],
messageJson['object']['summary'],
messageJson['object']['imageFilename'],
messageJson['object']['itemType'],
messageJson['object']['itemCategory'],
messageJson['object']['location'],
messageJson['object']['duration'],
debug)
if debug:
print('DEBUG: shared item received via c2s')
def outboxUndoShareUpload(baseDir: str,httpPrefix: str, \
nickname: str,domain: str,port: int, \
def outboxUndoShareUpload(baseDir: str, httpPrefix: str,
nickname: str, domain: str, port: int,
messageJson: {}, debug: bool) -> None:
""" When a shared item is removed via c2s
"""
@ -577,7 +611,7 @@ def outboxUndoShareUpload(baseDir: str,httpPrefix: str, \
if debug:
print('DEBUG: displayName missing from Offer')
return
removeShare(baseDir,nickname,domain, \
removeShare(baseDir, nickname, domain,
messageJson['object']['displayName'])
if debug:
print('DEBUG: shared item removed via c2s')