Skip to content

Instantly share code, notes, and snippets.

@bacalj
Last active June 23, 2022 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bacalj/aa71a8670e109f7171648436ce5b67c8 to your computer and use it in GitHub Desktop.
Save bacalj/aa71a8670e109f7171648436ce5b67c8 to your computer and use it in GitHub Desktop.
netlify function - aggregates three pages of an artlogic feed and only returns the data we care about
const fetch = require('node-fetch')
function deCloudinaried(url){
const urlTokenized = url.split('/')
urlTokenized.splice(3,1)
const basicUrl = urlTokenized.join('/')
return basicUrl
}
let urls = [
process.env.ART_LOGIC_FEED_BASE + '1',
process.env.ART_LOGIC_FEED_BASE + '2',
process.env.ART_LOGIC_FEED_BASE + '3'
]
const handler = async function () {
try {
const promises = urls.map(url => fetch(url))
const resolveds = await Promise.all(promises)
const data1 = await resolveds[0].json()
const data2 = await resolveds[1].json()
const data3 = await resolveds[2].json()
const data = [...data1.rows, ...data2.rows, ...data3.rows]
const pruned = data.map((r) => {
console.log(`- ${r.title} by ${r.artist}`)
if (r.img_urls_secondary_images.length > 0 ){
const {
_uid,
id,
stock_number,
title,
year,
artist,
artist_id,
dimensions,
height,
width,
medium,
img_url,
img_urls_secondary_images
} = r
const staticImgUrl = deCloudinaried(img_url)
const staticSecondaryImgUrls = img_urls_secondary_images.map((url) => {
return deCloudinaried(url)
})
return {
_uid,
id,
stock_number,
title,
year,
artist,
artist_id,
dimensions,
height,
width,
medium,
staticImgUrl,
staticSecondaryImgUrls
}
}
else {
const {
_uid,
id,
stock_number,
title,
year,
artist,
artist_id,
dimensions,
height,
width,
medium,
img_url
} = r
const staticImgUrl = deCloudinaried(img_url)
return {
_uid,
id,
stock_number,
title,
year,
artist,
artist_id,
dimensions,
height,
width,
medium,
staticImgUrl,
}
}
})
return {
statusCode: 200,
body: JSON.stringify({
works: pruned
}),
}
}
catch (error) {
console.log(error)
return {
statusCode: 500,
body: JSON.stringify({ msg: error.message }),
}
}
}
module.exports = { handler }
const fetch = require('node-fetch')
function deCloudinaried(url){
const urlTokenized = url.split('/')
urlTokenized.splice(3,1)
const basicUrl = urlTokenized.join('/')
return basicUrl
}
let pgs;
let urls;
async function getUrls(int){
let urlz = []
for (var i = 0; i < int; i++){
let plusOne = i + 1
urlz.push(process.env.ART_LOGIC_FEED_BASE + plusOne.toString())
}
return urlz
}
async function getPageCount(){
return await fetch(process.env.ART_LOGIC_FEED_BASE).then((r) => {
return r.json().then((x) => {
return x.feed_data.no_of_pages
})
})
}
async function getPagesArrays(arrOfPromises){
return await arrOfPromises.map((p) => {
return p.json().then((x) => {
return x.rows
})
})
}
const handler = async function () {
try {
pgs = await getPageCount()
urls = await getUrls(pgs)
let data = []
const promisedResponses = urls.map(url => fetch(url))
const responses = await Promise.all(promisedResponses)
const promisedRows = await getPagesArrays(responses)
const pageArrays = await Promise.all(promisedRows)
pageArrays.forEach((p) => {
p.forEach((row) => {
data.push(row)
})
})
const pruned = data.map((r) => {
console.log(`- ${r.title} by ${r.artist}`)
if (r.img_urls_secondary_images.length > 0 ){
const {
_uid,
stock_number,
title,
year,
artist,
artist_id,
dimensions,
height,
width,
medium,
img_url,
img_urls_secondary_images
} = r
const staticImgUrl = deCloudinaried(img_url)
const staticSecondaryImgUrls = img_urls_secondary_images.map((url) => {
return deCloudinaried(url)
})
return {
_uid,
stock_number,
title,
year,
artist,
artist_id,
dimensions,
height,
width,
medium,
staticImgUrl,
staticSecondaryImgUrls
}
}
else {
const {
_uid,
id,
stock_number,
title,
year,
artist,
artist_id,
dimensions,
height,
width,
medium,
img_url
} = r
const staticImgUrl = deCloudinaried(img_url)
return {
_uid,
id,
stock_number,
title,
year,
artist,
artist_id,
dimensions,
height,
width,
medium,
staticImgUrl,
}
}
})
return {
headers: {
"access-control-allow-origin": "*",
},
statusCode: 200,
body: JSON.stringify({
works: pruned
}),
}
}
catch (error) {
console.log(error)
return {
statusCode: 500,
body: JSON.stringify({ msg: error.message }),
}
}
}
module.exports = { handler }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment