Skip to content

Commit 60dd8f6

Browse files
committed
feat(route/twitter): support getUserTweets thirdparty api
1 parent db18c56 commit 60dd8f6

File tree

2 files changed

+46
-22
lines changed

2 files changed

+46
-22
lines changed

lib/routes/twitter/api/web-api/utils.ts

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -222,32 +222,49 @@ export const twitterGot = async (
222222
};
223223

224224
export const paginationTweets = async (endpoint: string, userId: number | undefined, variables: Record<string, any>, path?: string[]) => {
225-
const { data } = await twitterGot(baseUrl + gqlMap[endpoint], {
226-
variables: JSON.stringify({
227-
...variables,
228-
userId,
229-
}),
225+
const params = {
226+
variables: JSON.stringify({ ...variables, userId }),
230227
features: JSON.stringify(gqlFeatures[endpoint]),
231-
});
232-
let instructions;
233-
if (path) {
234-
instructions = data;
235-
for (const p of path) {
236-
instructions = instructions[p];
228+
};
229+
230+
const fetchData = async () => {
231+
if (config.twitter.thirdPartyApi) {
232+
const { data } = await ofetch(`${config.twitter.thirdPartyApi}${gqlMap[endpoint]}`, {
233+
method: 'GET',
234+
params,
235+
});
236+
return data;
237237
}
238-
instructions = instructions.instructions;
239-
} else {
240-
if (data?.user?.result?.timeline_v2?.timeline?.instructions) {
241-
instructions = data.user.result.timeline_v2.timeline.instructions;
242-
} else {
243-
// throw new Error('Because Twitter Premium has features that hide your likes, this RSS link is not available for Twitter Premium accounts.');
238+
const { data } = await twitterGot(baseUrl + gqlMap[endpoint], params);
239+
return data;
240+
};
241+
242+
const getInstructions = (data: any) => {
243+
if (path) {
244+
let instructions = data;
245+
for (const p of path) {
246+
instructions = instructions[p];
247+
}
248+
return instructions.instructions;
249+
}
250+
251+
const instructions = data?.user?.result?.timeline_v2?.timeline?.instructions;
252+
if (!instructions) {
244253
logger.debug(`twitter debug: instructions not found in data: ${JSON.stringify(data)}`);
245254
}
255+
return instructions;
256+
};
257+
258+
const data = await fetchData();
259+
const instructions = getInstructions(data);
260+
if (!instructions) {
261+
return [];
246262
}
247263

248-
const entries1 = instructions?.find((i) => i.type === 'TimelineAddToModule')?.moduleItems; // Media
249-
const entries2 = instructions?.find((i) => i.type === 'TimelineAddEntries').entries;
250-
return entries1 || entries2 || [];
264+
const moduleItems = instructions.find((i) => i.type === 'TimelineAddToModule')?.moduleItems;
265+
const entries = instructions.find((i) => i.type === 'TimelineAddEntries')?.entries;
266+
267+
return moduleItems || entries || [];
251268
};
252269

253270
export function gatherLegacyFromData(entries: any[], filterNested?: string[], userId?: number | string) {

lib/routes/twitter/user.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Route, ViewType } from '@/types';
22
import utils from './utils';
33
import api from './api';
44
import logger from '@/utils/logger';
5+
import { config } from '@/config';
56

67
export const route: Route = {
78
path: '/user/:id/:routeParams?',
@@ -45,7 +46,7 @@ export const route: Route = {
4546
supportScihub: false,
4647
},
4748
name: 'User timeline',
48-
maintainers: ['DIYgod', 'yindaheng98', 'Rongronggg9', 'CaoMeiYouRen'],
49+
maintainers: ['DIYgod', 'yindaheng98', 'Rongronggg9', 'CaoMeiYouRen', 'pseudoyu'],
4950
handler,
5051
radar: [
5152
{
@@ -59,9 +60,15 @@ async function handler(ctx) {
5960
const id = ctx.req.param('id');
6061

6162
// For compatibility
62-
const { count, exclude_replies, include_rts } = utils.parseRouteParams(ctx.req.param('routeParams'));
63+
const { count, exclude_replies: initialExcludeReplies, include_rts } = utils.parseRouteParams(ctx.req.param('routeParams'));
6364
const params = count ? { count } : {};
6465

66+
// Third party API does not support replies for now
67+
let exclude_replies = initialExcludeReplies;
68+
if (config.twitter.thirdPartyApi) {
69+
exclude_replies = true;
70+
}
71+
6572
await api.init();
6673
const userInfo = await api.getUser(id);
6774
let data;

0 commit comments

Comments
 (0)