From 40291c4d24cb5f441cdddfd26104f149bc4ee27c Mon Sep 17 00:00:00 2001 From: Snazzah Date: Wed, 24 May 2023 12:32:41 -0500 Subject: [PATCH 1/2] feat: add pinterest support --- src/modules/processing/match.js | 4 ++ src/modules/processing/matchActionDecider.js | 1 + src/modules/processing/services/pinterest.js | 24 ++++++++++ src/modules/processing/servicesConfig.json | 5 +++ .../processing/servicesPatternTesters.js | 4 +- src/modules/sub/utils.js | 3 ++ src/test/tests.json | 45 +++++++++++++++++++ 7 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 src/modules/processing/services/pinterest.js diff --git a/src/modules/processing/match.js b/src/modules/processing/match.js index 3328a32..e06166d 100644 --- a/src/modules/processing/match.js +++ b/src/modules/processing/match.js @@ -17,6 +17,7 @@ import vimeo from "./services/vimeo.js"; import soundcloud from "./services/soundcloud.js"; import instagram from "./services/instagram.js"; import vine from "./services/vine.js"; +import pinterest from "./services/pinterest.js"; export default async function (host, patternMatch, url, lang, obj) { try { @@ -110,6 +111,9 @@ export default async function (host, patternMatch, url, lang, obj) { case "vine": r = await vine({ id: patternMatch["id"] }); break; + case "pinterest": + r = await pinterest({ id: patternMatch["id"] }); + break; default: return apiJSON(0, { t: errorUnsupported(lang) }); } diff --git a/src/modules/processing/matchActionDecider.js b/src/modules/processing/matchActionDecider.js index ba10af2..e26d8ef 100644 --- a/src/modules/processing/matchActionDecider.js +++ b/src/modules/processing/matchActionDecider.js @@ -56,6 +56,7 @@ export default function(r, host, ip, audioFormat, isAudioOnly, lang, isAudioMute case "instagram": case "tumblr": case "twitter": + case "pinterest": responseType = 1; break; } diff --git a/src/modules/processing/services/pinterest.js b/src/modules/processing/services/pinterest.js new file mode 100644 index 0000000..2e3354e --- /dev/null +++ b/src/modules/processing/services/pinterest.js @@ -0,0 +1,24 @@ +import { maxVideoDuration } from "../../config.js"; + +export default async function(obj) { + const pinId = obj.id.split('--').reverse()[0]; + if (!/^\d+$/.test(pinId)) return { error: 'ErrorCantGetID' }; + let data = await fetch(`https://www.pinterest.com/resource/PinResource/get?data=${encodeURIComponent(JSON.stringify({ + options: { + field_set_key: "unauth_react_main_pin", + id: pinId + } + }))}`).then((r) => { return r.json() }).catch(() => { return false }); + if (!data) return { error: 'ErrorCouldntFetch' }; + + data = data["resource_response"]["data"]; + + let video = null; + + if (data.videos !== null) video = data.videos.video_list.V_720P; + else if (data.story_pin_data !== null) video = data.story_pin_data.pages[0].blocks[0].video.video_list.V_EXP7; + + if (!video) return { error: 'ErrorEmptyDownload' }; + if (video.duration > maxVideoDuration) return { error: ['ErrorLengthLimit', maxVideoDuration / 60000] }; + return { urls: video.url, filename: `pinterest_${pinId}.mp4`, audioFilename: `pinterest_${pinId}_audio` } +} diff --git a/src/modules/processing/servicesConfig.json b/src/modules/processing/servicesConfig.json index 2bb7f8f..1890eda 100644 --- a/src/modules/processing/servicesConfig.json +++ b/src/modules/processing/servicesConfig.json @@ -62,6 +62,11 @@ "tld": "co", "patterns": ["v/:id"], "enabled": true + }, + "pinterest": { + "alias": "pinterest videos & stories", + "patterns": ["pin/:id"], + "enabled": true } } } diff --git a/src/modules/processing/servicesPatternTesters.js b/src/modules/processing/servicesPatternTesters.js index 8f70613..e3286c3 100644 --- a/src/modules/processing/servicesPatternTesters.js +++ b/src/modules/processing/servicesPatternTesters.js @@ -28,5 +28,7 @@ export const testers = { "instagram": (patternMatch) => (patternMatch["id"] && patternMatch["id"].length <= 12), - "vine": (patternMatch) => (patternMatch["id"] && patternMatch["id"].length <= 12) + "vine": (patternMatch) => (patternMatch["id"] && patternMatch["id"].length <= 12), + + "pinterest": (patternMatch) => (patternMatch["id"] && patternMatch["id"].length <= 128) } diff --git a/src/modules/sub/utils.js b/src/modules/sub/utils.js index 48e6b7c..92b1082 100644 --- a/src/modules/sub/utils.js +++ b/src/modules/sub/utils.js @@ -72,6 +72,9 @@ export function cleanURL(url, host) { break; case "tiktok": url = url.replace(/@([a-zA-Z]+(\.[a-zA-Z]+)+)/, "@a") + case "pinterest": + // Redirect all TLDs back to .com + url = url.replace(/:\/\/(?:www.)pinterest(?:\.[a-z.]+)/, "://pinterest.com") default: url = url.split('?')[0]; if (url.substring(url.length - 1) === "/") url = url.substring(0, url.length - 1); diff --git a/src/test/tests.json b/src/test/tests.json index 17ed2c4..f3381b5 100644 --- a/src/test/tests.json +++ b/src/test/tests.json @@ -893,5 +893,50 @@ "code": 200, "status": "stream" } + }], + "pinterest": [{ + "name": "regular video", + "url": "https://www.pinterest.com/pin/70437485604616/", + "params": {}, + "expected": { + "code": 200, + "status": "redirect" + } + }, { + "name": "regular video (isAudioOnly)", + "url": "https://www.pinterest.com/pin/70437485604616/", + "params": { + "isAudioOnly": true + }, + "expected": { + "code": 200, + "status": "stream" + } + }, { + "name": "regular video (isAudioMuted)", + "url": "https://www.pinterest.com/pin/70437485604616/", + "params": { + "isAudioMuted": true + }, + "expected": { + "code": 200, + "status": "stream" + } + }, { + "name": "regular video (.ca TLD)", + "url": "https://www.pinterest.ca/pin/70437485604616/", + "params": {}, + "expected": { + "code": 200, + "status": "redirect" + } + }, { + "name": "story", + "url": "https://www.pinterest.com/pin/gadget-cool-products-amazon-product-technology-kitchen-gadgets--1084663891475263837/", + "params": {}, + "expected": { + "code": 200, + "status": "redirect" + } }] } \ No newline at end of file From f68edb51c73100e5494aa4203629c34fdd988769 Mon Sep 17 00:00:00 2001 From: wukko Date: Mon, 5 Jun 2023 11:27:43 +0600 Subject: [PATCH 2/2] 4 spaces!! four!! --- src/modules/processing/services/pinterest.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/modules/processing/services/pinterest.js b/src/modules/processing/services/pinterest.js index 2e3354e..335d736 100644 --- a/src/modules/processing/services/pinterest.js +++ b/src/modules/processing/services/pinterest.js @@ -2,12 +2,12 @@ import { maxVideoDuration } from "../../config.js"; export default async function(obj) { const pinId = obj.id.split('--').reverse()[0]; - if (!/^\d+$/.test(pinId)) return { error: 'ErrorCantGetID' }; + if (!(/^\d+$/.test(pinId))) return { error: 'ErrorCantGetID' }; let data = await fetch(`https://www.pinterest.com/resource/PinResource/get?data=${encodeURIComponent(JSON.stringify({ - options: { - field_set_key: "unauth_react_main_pin", - id: pinId - } + options: { + field_set_key: "unauth_react_main_pin", + id: pinId + } }))}`).then((r) => { return r.json() }).catch(() => { return false }); if (!data) return { error: 'ErrorCouldntFetch' };