From 2c1cd07dcd01d9af83062e1408fbc2d666c34511 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Sun, 25 Jul 2021 14:30:42 +0100 Subject: [PATCH] csv catalog endpoint --- daemon.py | 34 +++++++++++++++++++++++++++------- shares.py | 27 +++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/daemon.py b/daemon.py index 02ba043ba..c84b20566 100644 --- a/daemon.py +++ b/daemon.py @@ -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) diff --git a/shares.py b/shares.py index da878e2fc..30db312fd 100644 --- a/shares.py +++ b/shares.py @@ -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