Skip to content
This repository has been archived by the owner on Jun 9, 2022. It is now read-only.

Get all of the tagged items known to UPP. #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 53 additions & 28 deletions bin/lib/search-topics.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,61 @@
const debug = require('debug')('bin:lib:search-topics');
const fetch = require('node-fetch');

module.exports = function(topic){
module.exports = function(topic, offset = 0, recursive = false){
const SAPI_URL_MINUS_KEY = `${process.env.CAPI_ENDPOINT}/search/v1`;
const SAPI_URL = `${SAPI_URL_MINUS_KEY}?apiKey=${process.env.CAPI_KEY}`;

const results = [];

const SEARCH_BODY = {
'queryString': `topics:${topic}`,
'queryContext' : {
'curations' : [ 'ARTICLES', 'BLOGS' ]
},
'resultContext' : {
'maxResults' : '100',
'offset' : '0',
'aspects' : [ 'title', 'location', 'summary', 'lifecycle', 'metadata']
}
};

return fetch(SAPI_URL, {
'method' : 'POST',
'body' : JSON.stringify(SEARCH_BODY),
'headers' : {
'accept' : 'application/json',
'content-type' : 'application/json'
}
})
.then(res => {
if(res.status !== 200){
throw `An error occurred retrieving ${SAPI_URL} with body=${JSON.stringify(SEARCH_BODY)},\nres=${JSON.stringify(res)}`;
} else {
return res.json();
function search(topic, offset = 0){

const SEARCH_BODY = {
'queryString': `topics:${topic}`,
'queryContext' : {
'curations' : [ 'ARTICLES', 'BLOGS' ]
},
'resultContext' : {
'maxResults' : '100',
'offset' : offset,
'aspects' : [ 'title', 'location', 'summary', 'lifecycle', 'metadata']
}
})
;
};

return fetch(SAPI_URL, {
'method' : 'POST',
'body' : JSON.stringify(SEARCH_BODY),
'headers' : {
'accept' : 'application/json',
'content-type' : 'application/json'
}
})
.then(res => {
if(res.status !== 200){
throw `An error occurred retrieving ${SAPI_URL} with body=${JSON.stringify(SEARCH_BODY)},\nres=${JSON.stringify(res)}`;
} else {
return res.json();
}
})
.then(data => {

debug(data);

if(data.results[0].indexCount < offset + 100){
debug('Got em all. Number of results:', results.length);
return results;
} else {
debug('Doing another search offset from:', offset + 100);
data.results[0].results.forEach(result => {
results.push(result);
});
return search(topic, offset + 100);
}

})
;

}

return search(topic, offset)

}
2 changes: 0 additions & 2 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ router.get('/', (req, res) => {
searchTopics('audio-articles')
.then(taggedArticles => {

taggedArticles = taggedArticles.results[0].results;

const readiedAssets = data.Items.filter(item => {
// Items that have been deleted from the database still have their UUID
// and enabled values saved, so that if they're reabsorbed, a previously
Expand Down