Skip to content

Commit

Permalink
Merge pull request #659 from DIYgod/master
Browse files Browse the repository at this point in the history
[pull] master from diygod:master
  • Loading branch information
pull[bot] authored Dec 6, 2024
2 parents 440226f + 0d07768 commit f98ea12
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 105 deletions.
57 changes: 37 additions & 20 deletions lib/routes/twitter/api/web-api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,32 +222,49 @@ export const twitterGot = async (
};

export const paginationTweets = async (endpoint: string, userId: number | undefined, variables: Record<string, any>, path?: string[]) => {
const { data } = await twitterGot(baseUrl + gqlMap[endpoint], {
variables: JSON.stringify({
...variables,
userId,
}),
const params = {
variables: JSON.stringify({ ...variables, userId }),
features: JSON.stringify(gqlFeatures[endpoint]),
});
let instructions;
if (path) {
instructions = data;
for (const p of path) {
instructions = instructions[p];
};

const fetchData = async () => {
if (config.twitter.thirdPartyApi) {
const { data } = await ofetch(`${config.twitter.thirdPartyApi}${gqlMap[endpoint]}`, {
method: 'GET',
params,
});
return data;
}
instructions = instructions.instructions;
} else {
if (data?.user?.result?.timeline_v2?.timeline?.instructions) {
instructions = data.user.result.timeline_v2.timeline.instructions;
} else {
// throw new Error('Because Twitter Premium has features that hide your likes, this RSS link is not available for Twitter Premium accounts.');
const { data } = await twitterGot(baseUrl + gqlMap[endpoint], params);
return data;
};

const getInstructions = (data: any) => {
if (path) {
let instructions = data;
for (const p of path) {
instructions = instructions[p];
}
return instructions.instructions;
}

const instructions = data?.user?.result?.timeline_v2?.timeline?.instructions;
if (!instructions) {
logger.debug(`twitter debug: instructions not found in data: ${JSON.stringify(data)}`);
}
return instructions;
};

const data = await fetchData();
const instructions = getInstructions(data);
if (!instructions) {
return [];
}

const entries1 = instructions?.find((i) => i.type === 'TimelineAddToModule')?.moduleItems; // Media
const entries2 = instructions?.find((i) => i.type === 'TimelineAddEntries').entries;
return entries1 || entries2 || [];
const moduleItems = instructions.find((i) => i.type === 'TimelineAddToModule')?.moduleItems;
const entries = instructions.find((i) => i.type === 'TimelineAddEntries')?.entries;

return moduleItems || entries || [];
};

export function gatherLegacyFromData(entries: any[], filterNested?: string[], userId?: number | string) {
Expand Down
11 changes: 9 additions & 2 deletions lib/routes/twitter/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Route, ViewType } from '@/types';
import utils from './utils';
import api from './api';
import logger from '@/utils/logger';
import { config } from '@/config';

export const route: Route = {
path: '/user/:id/:routeParams?',
Expand Down Expand Up @@ -45,7 +46,7 @@ export const route: Route = {
supportScihub: false,
},
name: 'User timeline',
maintainers: ['DIYgod', 'yindaheng98', 'Rongronggg9', 'CaoMeiYouRen'],
maintainers: ['DIYgod', 'yindaheng98', 'Rongronggg9', 'CaoMeiYouRen', 'pseudoyu'],
handler,
radar: [
{
Expand All @@ -59,9 +60,15 @@ async function handler(ctx) {
const id = ctx.req.param('id');

// For compatibility
const { count, exclude_replies, include_rts } = utils.parseRouteParams(ctx.req.param('routeParams'));
const { count, exclude_replies: initialExcludeReplies, include_rts } = utils.parseRouteParams(ctx.req.param('routeParams'));
const params = count ? { count } : {};

// Third party API does not support replies for now
let exclude_replies = initialExcludeReplies;
if (config.twitter.thirdPartyApi) {
exclude_replies = true;
}

await api.init();
const userInfo = await api.getUser(id);
let data;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@
"fanfou-sdk": "5.0.0",
"form-data": "4.0.1",
"googleapis": "144.0.0",
"hono": "4.6.12",
"hono": "4.6.13",
"html-to-text": "9.0.5",
"http-cookie-agent": "6.0.6",
"https-proxy-agent": "7.0.5",
"iconv-lite": "0.6.3",
"imapflow": "1.0.169",
"imapflow": "1.0.171",
"instagram-private-api": "1.46.1",
"ioredis": "5.4.1",
"ip-regex": "5.0.0",
Expand Down
106 changes: 25 additions & 81 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f98ea12

Please sign in to comment.