api/pinterest: add missing filenames to images & gifs

This commit is contained in:
wukko 2024-08-31 14:09:34 +06:00
parent d0d0f16c5f
commit 4476ae0672
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2

View file

@ -14,13 +14,13 @@ export default async function(o) {
if (id.includes("--")) id = id.split("--")[1]; if (id.includes("--")) id = id.split("--")[1];
if (!id) return { error: "fetch.fail" }; if (!id) return { error: "fetch.fail" };
let html = await fetch(`https://www.pinterest.com/pin/${id}/`, { const html = await fetch(`https://www.pinterest.com/pin/${id}/`, {
headers: { "user-agent": genericUserAgent } headers: { "user-agent": genericUserAgent }
}).then(r => r.text()).catch(() => {}); }).then(r => r.text()).catch(() => {});
if (!html) return { error: "fetch.fail" }; if (!html) return { error: "fetch.fail" };
let videoLink = [...html.matchAll(videoRegex)] const videoLink = [...html.matchAll(videoRegex)]
.map(([, link]) => link) .map(([, link]) => link)
.find(a => a.endsWith('.mp4') && a.includes('720p')); .find(a => a.endsWith('.mp4') && a.includes('720p'));
@ -30,13 +30,16 @@ export default async function(o) {
audioFilename: `pinterest_${o.id}_audio` audioFilename: `pinterest_${o.id}_audio`
} }
let imageLink = [...html.matchAll(imageRegex)] const imageLink = [...html.matchAll(imageRegex)]
.map(([, link]) => link) .map(([, link]) => link)
.find(a => a.endsWith('.jpg') || a.endsWith('.gif')); .find(a => a.endsWith('.jpg') || a.endsWith('.gif'));
const imageType = imageLink.endsWith(".gif") ? "gif" : "jpg"
if (imageLink) return { if (imageLink) return {
urls: imageLink, urls: imageLink,
isPhoto: true isPhoto: true,
filename: `pinterest_${o.id}.${imageType}`
} }
return { error: "fetch.empty" }; return { error: "fetch.empty" };