Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x | "use strict";
const axios = require("axios");
const cheerio = require("cheerio");
let webScrape = async (event, context) => {
return await new Promise(async (resolve, reject) => {
try {
console.log("event:", JSON.stringify(event));
console.log("context:", JSON.stringify(context));
let htmlUrl = JSON.parse(event.body).url;
let response = await axios.get(htmlUrl);
let metadata = {};
let $ = cheerio.load(response.data);
let ogTitle = $("head title").text();
let ogDescription = $('meta[name="description"]').attr("content");
let ogImages = $("img");
let ogKeywords = $('meta[name="keywords"]').attr("content");
let ogtitle = $('meta[property="og:title"]').attr("content");
let ogtype = $('meta[property="og:type"]').attr("content");
let ogimages = $('meta[property="og:image"]').attr("content");
let ogurl = $('meta[property="og:url"]').attr("content");
Eif (ogTitle) {
metadata.title = ogTitle;
}
Eif (ogDescription) {
metadata.description = ogDescription;
}
Eif (ogImages && ogImages.length) {
metadata.images = [];
for (let i = 0; i < ogImages.length; i++) {
metadata.images.push($(ogImages[i]).attr("src"));
}
}
Eif (ogtitle && ogtitle.length) {
metadata.ogtitle = ogtitle;
}
Eif (ogtype && ogtype.length) {
metadata.ogtype = ogtype;
}
Eif (ogimages && ogimages.length) {
metadata.ogimages = ogimages;
}
Eif (ogurl && ogurl.length) {
metadata.ogurl = ogurl;
}
return resolve(prepareResponse(200, metadata));
} catch (error) {
console.error("Error: ", error);
return resolve(prepareResponse(500, "INTERNAL SERVER ERROR"));
}
});
};
function prepareResponse(statusCode, message) {
return {
"statusCode": statusCode,
"headers": {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST,GET,OPTIONS'
},
"body": JSON.stringify(message),
"isBase64Encoded": false,
};
}
module.exports = {
handler: webScrape,
};
|