Skip to content

Commit

Permalink
api/instagram: add support for share urls
Browse files Browse the repository at this point in the history
closes #998
  • Loading branch information
dumbmoron committed Feb 8, 2025
1 parent 1be13a3 commit cca6127
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
3 changes: 2 additions & 1 deletion api/src/processing/service-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export const services = {
"p/:postId",
":username/p/:postId",
"tv/:postId",
"stories/:username/:storyId"
"stories/:username/:storyId",
"share/:shareId"
],
altDomains: ["ddinstagram.com"],
},
Expand Down
3 changes: 2 additions & 1 deletion api/src/processing/service-patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export const testers = {
"dailymotion": pattern => pattern.id?.length <= 32,

"instagram": pattern =>
pattern.postId?.length <= 12
pattern.postId?.length <= 48
|| pattern.shareId?.length <= 16
|| (pattern.username?.length <= 30 && pattern.storyId?.length <= 24),

"loom": pattern =>
Expand Down
34 changes: 29 additions & 5 deletions api/src/processing/services/instagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { genericUserAgent } from "../../config.js";
import { createStream } from "../../stream/manage.js";
import { getCookie, updateCookie } from "../cookie/manager.js";
import { randomBytes } from "node:crypto";
import { resolveRedirectingURL } from "../url.js";

const commonHeaders = {
"user-agent": genericUserAgent,
Expand Down Expand Up @@ -54,7 +55,7 @@ const getObjectFromEntries = (name, data) => {
return obj && JSON.parse(obj);
}

export default function(obj) {
export default function instagram(obj) {
const dispatcher = obj.dispatcher;

async function findDtsgId(cookie) {
Expand Down Expand Up @@ -300,11 +301,18 @@ export default function(obj) {
function extractOldPost(data, id, alwaysProxy) {
const shortcodeMedia = data?.gql_data?.shortcode_media || data?.gql_data?.xdt_shortcode_media;
const sidecar = shortcodeMedia?.edge_sidecar_to_children;

if (sidecar) {
const picker = sidecar.edges.filter(e => e.node?.display_url)
.map((e, i) => {
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";

Expand All @@ -331,13 +339,17 @@ export default function(obj) {
});

if (picker.length) return { picker }
} else if (shortcodeMedia?.video_url) {
}

if (shortcodeMedia?.video_url) {
return {
urls: shortcodeMedia.video_url,
filename: `instagram_${id}.mp4`,
audioFilename: `instagram_${id}_audio`
}
} else if (shortcodeMedia?.display_url) {
}

if (shortcodeMedia?.display_url) {
return {
urls: shortcodeMedia.display_url,
isPhoto: true
Expand Down Expand Up @@ -504,7 +516,19 @@ export default function(obj) {
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 (username && storyId) return getStory(username, storyId);

Expand Down

0 comments on commit cca6127

Please sign in to comment.