Skip to content

Commit

Permalink
hopefully support multiple pages per insta link
Browse files Browse the repository at this point in the history
  • Loading branch information
twobiers committed Oct 18, 2024
1 parent 697c629 commit 7f3924e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
11 changes: 5 additions & 6 deletions src/commands/instagram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { BotContext } from "@/context.js";
import * as instagramService from "@/service/instagram.js";

const instagramOptions = {
// TODO: https://www.instagram.com/p/C_OQe4FON7Q/
uriPattern: /https?:\/\/(?:www\.)?instagram\.com\/(?:reel|tv|p)\/(?:[0-9a-zA-Z_-]+)\/?/gi,
headers: {
"User-Agent":
Expand Down Expand Up @@ -62,12 +63,10 @@ export default class InstagramLink implements SpecialCommand {
// We need to reply, since we cannot edit a message created by a different user (only remove embeds)
await message.reply({
content: "Dein Dreckspost du Hund:",
files: [
{
attachment: result.mediaUrl,
name: "Drecksvideo.mp4",
},
],
files: result.mediaUrls.map((url, idx) => ({
attachment: url,
name: `Drecksvideo_${idx}.mp4`,
})),
});
}
await message.suppressEmbeds(true);
Expand Down
23 changes: 18 additions & 5 deletions src/service/instagram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export type InstagramResponse = SuccessInstagramResponse | ErrorInstagramRespons

export interface SuccessInstagramResponse {
success: true;
mediaUrl: string;
mediaUrls: string[];
}

export interface ErrorInstagramResponse {
Expand Down Expand Up @@ -87,8 +87,22 @@ export async function downloadInstagramContent(
};
}

const videoLinks = result.links.filter(l => l.quality.startsWith("video_"));
if (videoLinks.length === 0) {
// I'm too stupid to do this in a reduce function.
const linksPerPage: LinkEntry[][] = [];
const linkCandidates = result.links.filter(l => l.quality.startsWith("video_"));
for (const link of linkCandidates) {
const lastIndexOfUnderscore = link.quality.lastIndexOf("_");
const idx = Number(link.quality.substring(lastIndexOfUnderscore + 1));
if (linksPerPage[idx] === undefined) {
linksPerPage[idx] = [];
}
linksPerPage[idx].push(link);
}

const mediaUrls = linksPerPage
// biome-ignore lint/style/noNonNullAssertion: It exists
.map(links => sortLinksByVideoQuality(links).at(-1)!.link);
if (mediaUrls.length === 0) {
return {
success: false,
message: "Got no links :(",
Expand All @@ -98,8 +112,7 @@ export async function downloadInstagramContent(

return {
success: true,
// biome-ignore lint/style/noNonNullAssertion: It exists
mediaUrl: sortLinksByVideoQuality(videoLinks).at(-1)!.link,
mediaUrls,
};
} catch (error) {
sentry.captureException(error);
Expand Down

0 comments on commit 7f3924e

Please sign in to comment.