csv catalog endpoint

main
Bob Mottram 2021-07-25 14:30:42 +01:00
parent a47faed994
commit 2c1cd07dcd
2 changed files with 54 additions and 7 deletions

View File

@ -208,6 +208,7 @@ from shares import addShare
from shares import removeSharedItem
from shares import expireShares
from shares import sharesCatalogEndpoint
from shares import sharesCatalogCSVEndpoint
from categories import setHashtagCategory
from languages import getActorLanguages
from languages import setActorLanguages
@ -517,6 +518,16 @@ class PubServer(BaseHTTPRequestHandler):
print('Blocked User agent: ' + agentDomain)
return blockedUA
def _requestCSV(self) -> bool:
"""Should a csv response be given?
"""
if not self.headers.get('Accept'):
return False
acceptStr = self.headers['Accept']
if 'text/csv' in acceptStr:
return True
return False
def _requestHTTP(self) -> bool:
"""Should a http response be given?
"""
@ -10669,13 +10680,22 @@ class PubServer(BaseHTTPRequestHandler):
# show shared items DFC catalog
if self._hasAccept(callingDomain) and catalogAuthorized:
if not self._requestHTTP():
catalogJson = \
sharesCatalogEndpoint(self.server.baseDir,
self.server.httpPrefix,
self.server.domainFull,
self.server.path)
msg = json.dumps(catalogJson,
ensure_ascii=False).encode('utf-8')
if self._requestCSV():
catalogStr = \
sharesCatalogCSVEndpoint(self.server.baseDir,
self.server.httpPrefix,
self.server.domainFull,
self.server.path)
msg = json.dumps(catalogStr,
ensure_ascii=False).encode('utf-8')
else:
catalogJson = \
sharesCatalogEndpoint(self.server.baseDir,
self.server.httpPrefix,
self.server.domainFull,
self.server.path)
msg = json.dumps(catalogJson,
ensure_ascii=False).encode('utf-8')
msglen = len(msg)
self._set_headers('application/json',
msglen, None, callingDomain)

View File

@ -913,3 +913,30 @@ def sharesCatalogEndpoint(baseDir: str, httpPrefix: str,
endpoint['DFC:supplies'].append(catalogItem)
return endpoint
def sharesCatalogCSVEndpoint(baseDir: str, httpPrefix: str,
domainFull: str,
path: str) -> str:
"""Returns a CSV version of the shares catalog
"""
catalogJson = \
sharesCatalogEndpoint(baseDir, httpPrefix, domainFull, path)
if not catalogJson:
return ''
if not catalogJson.get('DFC:supplies'):
return ''
csvStr = \
'id,type,hasType,startDate,expiryDate,' + \
'quantity,price,Image,description\n'
for item in catalogJson['DFC:supplies']:
csvStr += item['@id'] + ','
csvStr += item['@type'] + ','
csvStr += item['DFC:hasType'] + ','
csvStr += item['DFC:startDate'] + ','
csvStr += item['DFC:expiryDate'] + ','
csvStr += item['DFC:quantity'] + ','
csvStr += item['DFC:price'] + ','
csvStr += item['DFC:Image'] + ','
csvStr += item['DFC:description'] + '\n'
return csvStr