cobalt/src/modules/processing/services/pinterest.js

34 lines
1.1 KiB
JavaScript
Raw Normal View History

import { genericUserAgent } from "../../config.js";
2023-05-24 19:32:41 +02:00
2024-04-27 16:29:30 +02:00
const linkRegex = /"url":"(https:\/\/v1.pinimg.com\/videos\/.*?)"/g;
export default async function(o) {
2024-04-27 16:29:30 +02:00
let id = o.id;
2023-05-24 19:32:41 +02:00
if (!o.id && o.shortLink) {
id = await fetch(`https://api.pinterest.com/url_shortener/${o.shortLink}/redirect/`, { redirect: "manual" }).then((r) => {
return r.headers.get("location").split('pin/')[1].split('/')[0]
}).catch(() => {});
}
2024-04-27 16:29:30 +02:00
if (id.includes("--")) id = id.split("--")[1];
if (!id) return { error: 'ErrorCouldntFetch' };
2023-05-24 19:32:41 +02:00
let html = await fetch(`https://www.pinterest.com/pin/${id}/`, {
headers: { "user-agent": genericUserAgent }
}).then((r) => { return r.text() }).catch(() => { return false });
2023-05-24 19:32:41 +02:00
if (!html) return { error: 'ErrorCouldntFetch' };
2023-05-24 19:32:41 +02:00
2024-04-27 16:29:30 +02:00
let videoLink = [...html.matchAll(linkRegex)]
.map(([, link]) => link)
.filter(a => a.endsWith('.mp4') && a.includes('720p'))[0];
if (!videoLink) return { error: 'ErrorEmptyDownload' };
return {
2024-04-27 16:29:30 +02:00
urls: videoLink,
filename: `pinterest_${o.id}.mp4`,
audioFilename: `pinterest_${o.id}_audio`
}
2023-05-24 19:32:41 +02:00
}