-
-
Save gatolynx/844b6e065b39c45f9309e4c38413fb30 to your computer and use it in GitHub Desktop.
Netlify Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.handler = (event, _context, callback) => { | |
if ('url' in event.queryStringParameters === false) { | |
console.error("parameter 'url' is necessary!!"); | |
return; | |
} | |
const url = event.queryStringParameters.url; | |
const options = { | |
'url': encodeURI(url), | |
'timeout': 10000, | |
'headers': { | |
'user-agent': 'My-Functions', | |
}, | |
} | |
const ogs = require("open-graph-scraper"); | |
ogs(options).then(function(data) { | |
const metadata = data.result; | |
let ogpData = {}; | |
ogpData['siteName'] = metadata.ogSiteName; | |
ogpData['title'] = metadata.ogTitle; | |
ogpData['description'] = metadata.ogDescription; | |
if (Array.isArray(metadata.ogImage)) { | |
const jpgUrl = metadata.ogImage.find((image) => image.url.endsWith('.jpg') || image.url.endsWith('.jpeg')).url | |
ogpData['image'] = jpgUrl | |
} else if (typeof metadata.ogImage !== 'undefined'){ | |
ogpData['image'] = metadata.ogImage.url; | |
} | |
console.log(ogpData); | |
console.log(JSON.stringify(ogpData)); | |
callback(null, { | |
statusCode: 200, | |
"headers": { "Content-Type": "application/json; charset=utf-8"}, | |
body: JSON.stringify(ogpData) | |
}); | |
}).catch(function(error) { | |
console.error(error); | |
let ogpData = {}; | |
ogpData['status'] = "404"; | |
return; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment