cotr-extract_opengraph_data.web.val.run
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
import axios from "npm:axios";
import * as cheerio from "npm:cheerio";
export async function extractOpenGraphTags(request: Request) {
try {
const body = await request.json();
const url = body.url;
if (url) {
const response = await axios.get(url);
const html = response.data;
const $ = cheerio.load(html);
const openGraphTags = [];
$("meta[property^='og:']").each((i, el) => {
const property = $(el).attr("property");
const content = $(el).attr("content");
if (property && content) {
openGraphTags.push({ property, content });
}
});
return Response.json(openGraphTags);
} else {
return Response.json({ message: "URL not found" }, {
status: 400,
});
}
} catch (error) {
console.error("Error:", error);
return Response.json({ message: "The body of this request was not JSON-encoded." }, {
status: 400,
});
}
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
April 9, 2024