Skip to content

Instantly share code, notes, and snippets.

@DannyAkintunde
Created December 16, 2024 08:08
Show Gist options
  • Select an option

  • Save DannyAkintunde/36ce1e935edb8ae68b4c71f4374f1f30 to your computer and use it in GitHub Desktop.

Select an option

Save DannyAkintunde/36ce1e935edb8ae68b4c71f4374f1f30 to your computer and use it in GitHub Desktop.
A simple scraper to fetch social previews from sites
const cheerio = require('cheerio')
/*
* Checks if the provided string is a valid URL.
* @param {string} url - The URL to validate.
* @returns {boolean} - True if valid, false otherwise.
*/
function isUrl(url) {
const pattern = new RegExp(
"^((https|http)?:\\/\\/)?" + // protocol
"((([a-z0-9]+([\\-\\.][a-z0-9]+)*\\.)+[a-z]{2,}|localhost|" + // domain name
"(([0-9]{1,3}\\.){3}[0-9]{1,3}))" + // OR ip (v4) address
"(\\:[0-9]{1,5})?)" + // optional port
"(\\/.*)?$"
); // path
return pattern.test(url);
}
/*
* Fetches the social preview from a url.
* @param {string} url - The URL to fetch.
* @returns {string} - return an image url/base64 string.
*/
async function fetchSocialPreview(url) {
if (!isUrl(url)) throw new Error("Invalid url");
try {
const $ = await cheerio.fromURL(url);
const socialPreview =
$("meta[property='og:image']").attr("content") ||
"https://picsum.photos/512/512";
return socialPreview;
} catch {
throw new Error("Error fetching social preview");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment