Skip to content

Commit

Permalink
chore(route/threads): bring back formal get user api and add fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudoyu committed Oct 29, 2024
1 parent fc6df4a commit 91f7bcf
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions lib/routes/threads/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import dayjs from 'dayjs';
import cache from '@/utils/cache';
import { destr } from 'destr';
import NotFoundError from '@/errors/types/not-found';

const profileUrl = (user: string) => `https://www.threads.net/@${user}`;
Expand All @@ -14,7 +15,7 @@ const THREADS_QUERY = 6_232_751_443_445_612;
const REPLIES_QUERY = 6_307_072_669_391_286;
const USER_AGENT = 'Barcelona 289.0.0.77.109 Android';
const appId = '238260118697367';
// const asbdId = '129477';
const asbdId = '129477';

const extractTokens = async (user): Promise<{ lsd: string }> => {
const response = await ofetch(profileUrl(user), {
Expand Down Expand Up @@ -52,17 +53,39 @@ const makeHeader = (user: string, lsd: string) => ({
// the formal way always reachs the rate limit, so use instagram api to get user id instead
const getUserId = (user: string, lsd: string): Promise<string> =>
cache.tryGet(`threads:userId:${user}`, async () => {
const response = await ofetch(instagramUrl(user), {
headers: makeHeader(user, lsd),
const pathName = `/@${user}`;
const payload: any = {
'route_urls[0]': pathName,
__a: '1',
__comet_req: '29',
lsd,
};
const response = await ofetch('https://www.threads.net/ajax/bulk-route-definitions/', {
method: 'POST',
headers: {
...makeHeader(user, lsd),
'content-type': 'application/x-www-form-urlencoded',
'X-ASBD-ID': asbdId,
},
body: new URLSearchParams(payload).toString(),
parseResponse: (txt) => destr(txt.slice(9)), // remove "for (;;);"
});

if (!response?.data?.user) {
throw new NotFoundError('Instagram getUser API response is invalid');
}
let userId = response.payload.payloads[pathName].result.exports.rootView.props.user_id;

const userId = response.data.user.id;
if (!userId) {
throw new NotFoundError('User ID not found in Instagram getUser API response');
const fallbackResponse = await ofetch(instagramUrl(user), {
headers: makeHeader(user, lsd),
});

if (!fallbackResponse?.data?.user) {
throw new NotFoundError('Instagram getUser API response is invalid');
}

userId = fallbackResponse.data.user.id;
if (!userId) {
throw new NotFoundError('User ID not found in Instagram getUser API response');
}
}

return userId;
Expand Down

0 comments on commit 91f7bcf

Please sign in to comment.