diff --git a/src/commands/instagram.ts b/src/commands/instagram.ts index e6ac68ed..3d8ba77e 100644 --- a/src/commands/instagram.ts +++ b/src/commands/instagram.ts @@ -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": @@ -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); diff --git a/src/service/instagram.ts b/src/service/instagram.ts index df31346f..0dc427a4 100644 --- a/src/service/instagram.ts +++ b/src/service/instagram.ts @@ -29,7 +29,7 @@ export type InstagramResponse = SuccessInstagramResponse | ErrorInstagramRespons export interface SuccessInstagramResponse { success: true; - mediaUrl: string; + mediaUrls: string[]; } export interface ErrorInstagramResponse { @@ -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 :(", @@ -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);