fix: misc bugfixes

This commit is contained in:
dangered wolf
2026-07-26 11:30:34 -07:00
parent 22c85ca981
commit 1338b65ac1
3 changed files with 47 additions and 17 deletions
@@ -1,7 +1,7 @@
import type { SocialConversationInstagram } from '../../types/api-schemas.js';
import { fetchCommentPageGraphql, fetchInstagramCsrfToken } from './client.js';
import { decodeCommentCursor, encodeCommentCursor } from './cursors.js';
import { extractCommentsConnection, extractLsdFromHtml } from './extractors.js';
import { extractCommentsConnection } from './extractors.js';
import { fetchInstagramPageWithWebInfo } from './fetch-shortcode-page.js';
import {
extractCommentsFromGraphqlJson,
@@ -111,8 +111,23 @@ export async function constructInstagramConversation(
return { ok: false, message: 'Invalid cursor' };
}
// Prefer LSD preserved on the page result (required for polaris-graphql, which has empty HTML).
const lsd = page.lsd;
if (!lsd) {
return {
ok: false,
message: 'Instagram comment fetch failed',
data: {
code: 500,
status,
thread: [status],
replies: [],
author: status.author,
cursor: { bottom: options.cursor }
}
};
}
const csrf = await fetchInstagramCsrfToken(options.userAgent);
const lsd = extractLsdFromHtml(htmlBody) ?? 'a';
const gql = await fetchCommentPageGraphql({
mediaId: decoded.mediaId,
after: decoded.after,
@@ -5,6 +5,7 @@ import {
fetchRulingForContent
} from './client.js';
import {
extractLsdFromHtml,
extractPolarisProductFromGraphqlJson,
extractPostMediaItem,
type PolarisMediaBundle
@@ -20,6 +21,8 @@ export type InstagramWebInfoPage =
pathUsed: string;
comments: PolarisMediaBundle['comments'];
source: 'polaris-graphql' | 'polaris-html' | 'web-info-html';
/** Session/doc LSD for GraphQL comment pagination (Polaris GraphQL has no HTML to parse). */
lsd: string | null;
}
| {
ok: false;
@@ -29,6 +32,7 @@ export type InstagramWebInfoPage =
pathUsed: null;
comments: null;
source: null;
lsd: null;
};
function isLoginRedirect(finalUrl: string | undefined): boolean {
@@ -92,7 +96,8 @@ export async function fetchInstagramPageWithWebInfo(
item: bundle.product,
pathUsed: refererPath,
comments: bundle.comments,
source: 'polaris-graphql'
source: 'polaris-graphql',
lsd: session.lsd
};
}
}
@@ -145,7 +150,8 @@ export async function fetchInstagramPageWithWebInfo(
item,
pathUsed: path,
comments: null,
source: looksPolaris ? 'polaris-html' : 'web-info-html'
source: looksPolaris ? 'polaris-html' : 'web-info-html',
lsd: extractLsdFromHtml(r.html) ?? session?.lsd ?? null
};
}
@@ -180,6 +186,7 @@ export async function fetchInstagramPageWithWebInfo(
item: null,
pathUsed: null,
comments: null,
source: null
source: null,
lsd: null
};
}
+20 -12
View File
@@ -1,4 +1,4 @@
import { Hono } from 'hono';
import { Context, Hono } from 'hono';
import { trimTrailingSlash } from 'hono/trailing-slash';
import { getBranding } from '../../helpers/branding';
import { activityRequest } from './routes/activity';
@@ -6,7 +6,10 @@ import { instagramPostRequest } from './routes/post';
import { oembed } from './routes/oembed';
import { versionRoute } from '../common/version';
import { constructInstagramPost } from '@fxembed/atmosphere/providers/instagram/post';
import { normalizeInstagramPostId } from '@fxembed/atmosphere/providers/instagram/shortcode';
import {
instagramShortcodeToPk,
normalizeInstagramPostId
} from '@fxembed/atmosphere/providers/instagram/shortcode';
export const instagram = new Hono();
instagram.use(trimTrailingSlash());
@@ -14,24 +17,29 @@ instagram.use(trimTrailingSlash());
instagram.get('/owoembed', oembed);
instagram.get('/api/v1/statuses/:snowcode', activityRequest);
instagram.get('/raw/:id', async c => {
const instagramJsonPostRequest = async (c: Context) => {
const { id } = c.req.param();
if (!id) {
return c.json({ error: 'Invalid request' }, 400);
}
const shortcode = normalizeInstagramPostId(id);
const thread = await constructInstagramPost(shortcode, c.req.header('User-Agent'));
return c.json(thread);
});
instagram.get('/api/:id', async c => {
const { id } = c.req.param();
if (!id) {
let shortcode: string;
try {
shortcode = normalizeInstagramPostId(id);
if (!shortcode) {
throw new Error('empty shortcode');
}
instagramShortcodeToPk(shortcode);
} catch {
return c.json({ error: 'Invalid request' }, 400);
}
const shortcode = normalizeInstagramPostId(id);
const thread = await constructInstagramPost(shortcode, c.req.header('User-Agent'));
return c.json(thread);
});
};
instagram.get('/raw/:id', instagramJsonPostRequest);
instagram.get('/api/:id', instagramJsonPostRequest);
// Instagram permalinks: /p/SHORTCODE and /reel/SHORTCODE (same media object)
instagram.get('/p/:id', instagramPostRequest);