api/instagram: add support for share urls

closes #998
This commit is contained in:
jj 2025-02-08 17:24:02 +00:00
parent 1be13a30bf
commit cca61275f1
No known key found for this signature in database
3 changed files with 33 additions and 7 deletions

View file

@ -41,7 +41,8 @@ export const services = {
"p/:postId", "p/:postId",
":username/p/:postId", ":username/p/:postId",
"tv/:postId", "tv/:postId",
"stories/:username/:storyId" "stories/:username/:storyId",
"share/:shareId"
], ],
altDomains: ["ddinstagram.com"], altDomains: ["ddinstagram.com"],
}, },

View file

@ -6,7 +6,8 @@ export const testers = {
"dailymotion": pattern => pattern.id?.length <= 32, "dailymotion": pattern => pattern.id?.length <= 32,
"instagram": pattern => "instagram": pattern =>
pattern.postId?.length <= 12 pattern.postId?.length <= 48
|| pattern.shareId?.length <= 16
|| (pattern.username?.length <= 30 && pattern.storyId?.length <= 24), || (pattern.username?.length <= 30 && pattern.storyId?.length <= 24),
"loom": pattern => "loom": pattern =>

View file

@ -2,6 +2,7 @@ import { genericUserAgent } from "../../config.js";
import { createStream } from "../../stream/manage.js"; import { createStream } from "../../stream/manage.js";
import { getCookie, updateCookie } from "../cookie/manager.js"; import { getCookie, updateCookie } from "../cookie/manager.js";
import { randomBytes } from "node:crypto"; import { randomBytes } from "node:crypto";
import { resolveRedirectingURL } from "../url.js";
const commonHeaders = { const commonHeaders = {
"user-agent": genericUserAgent, "user-agent": genericUserAgent,
@ -54,7 +55,7 @@ const getObjectFromEntries = (name, data) => {
return obj && JSON.parse(obj); return obj && JSON.parse(obj);
} }
export default function(obj) { export default function instagram(obj) {
const dispatcher = obj.dispatcher; const dispatcher = obj.dispatcher;
async function findDtsgId(cookie) { async function findDtsgId(cookie) {
@ -300,11 +301,18 @@ export default function(obj) {
function extractOldPost(data, id, alwaysProxy) { function extractOldPost(data, id, alwaysProxy) {
const shortcodeMedia = data?.gql_data?.shortcode_media || data?.gql_data?.xdt_shortcode_media; const shortcodeMedia = data?.gql_data?.shortcode_media || data?.gql_data?.xdt_shortcode_media;
const sidecar = shortcodeMedia?.edge_sidecar_to_children; const sidecar = shortcodeMedia?.edge_sidecar_to_children;
if (sidecar) { if (sidecar) {
const picker = sidecar.edges.filter(e => e.node?.display_url) const picker = sidecar.edges.filter(e => e.node?.display_url)
.map((e, i) => { .map((e, i) => {
const type = e.node?.is_video ? "video" : "photo"; const type = e.node?.is_video ? "video" : "photo";
const url = type === "video" ? e.node?.video_url : e.node?.display_url;
let url;
if (type === 'video') {
url = e.node?.video_url;
} else if (type === 'photo') {
url = e.node?.display_url;
}
let itemExt = type === "video" ? "mp4" : "jpg"; let itemExt = type === "video" ? "mp4" : "jpg";
@ -331,13 +339,17 @@ export default function(obj) {
}); });
if (picker.length) return { picker } if (picker.length) return { picker }
} else if (shortcodeMedia?.video_url) { }
if (shortcodeMedia?.video_url) {
return { return {
urls: shortcodeMedia.video_url, urls: shortcodeMedia.video_url,
filename: `instagram_${id}.mp4`, filename: `instagram_${id}.mp4`,
audioFilename: `instagram_${id}_audio` audioFilename: `instagram_${id}_audio`
} }
} else if (shortcodeMedia?.display_url) { }
if (shortcodeMedia?.display_url) {
return { return {
urls: shortcodeMedia.display_url, urls: shortcodeMedia.display_url,
isPhoto: true isPhoto: true
@ -504,7 +516,19 @@ export default function(obj) {
return { error: "link.unsupported" }; return { error: "link.unsupported" };
} }
const { postId, storyId, username, alwaysProxy } = obj; const { postId, shareId, storyId, username, alwaysProxy } = obj;
if (shareId) {
return resolveRedirectingURL(
`https://www.instagram.com/share/${shareId}/`,
dispatcher,
'curl/7.88.1'
).then(match => instagram({
...obj, ...match,
shareId: undefined
}));
}
if (postId) return getPost(postId, alwaysProxy); if (postId) return getPost(postId, alwaysProxy);
if (username && storyId) return getStory(username, storyId); if (username && storyId) return getStory(username, storyId);