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

44 lines
1.5 KiB
JavaScript
Raw Normal View History

import { genericUserAgent } from "../../config.js";
2023-05-24 19:32:41 +02:00
const videoRegex = /"url":"(https:\/\/v1\.pinimg\.com\/videos\/.*?)"/g;
const imageRegex = /src="(https:\/\/i\.pinimg\.com\/.*\.(jpg|gif))"/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 => 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 => r.text()).catch(() => {});
2023-05-24 19:32:41 +02:00
if (!html) return { error: 'ErrorCouldntFetch' };
2023-05-24 19:32:41 +02:00
let videoLink = [...html.matchAll(videoRegex)]
2024-04-27 16:29:30 +02:00
.map(([, link]) => link)
.find(a => a.endsWith('.mp4') && a.includes('720p'));
2024-04-27 16:29:30 +02:00
if (videoLink) return {
2024-04-27 16:29:30 +02:00
urls: videoLink,
filename: `pinterest_${o.id}.mp4`,
audioFilename: `pinterest_${o.id}_audio`
}
let imageLink = [...html.matchAll(imageRegex)]
.map(([, link]) => link)
.find(a => a.endsWith('.jpg') || a.endsWith('.gif'));
if (imageLink) return {
urls: imageLink,
isPhoto: true
}
return { error: 'ErrorEmptyDownload' };
2023-05-24 19:32:41 +02:00
}