Skip to content

Commit a5cb096

Browse files
committed
feat(route/twitter): add third-party twitter api support
1 parent 52c8c8f commit a5cb096

4 files changed

Lines changed: 41 additions & 29 deletions

File tree

lib/config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ export type Config = {
330330
authenticationSecret?: string[];
331331
phoneOrEmail?: string[];
332332
authToken?: string[];
333+
thirdPartyApi?: string;
333334
};
334335
uestc: {
335336
bbsCookie?: string;
@@ -749,11 +750,12 @@ const calculateValue = () => {
749750
cookie: envs.TSDM39_COOKIES,
750751
},
751752
twitter: {
752-
username: envs.TWITTER_USERNAME?.split(','),
753+
username: envs.TWITTER_PHONE_OR_EMAIL?.split(','),
753754
password: envs.TWITTER_PASSWORD?.split(','),
754755
authenticationSecret: envs.TWITTER_AUTHENTICATION_SECRET?.split(','),
755756
phoneOrEmail: envs.TWITTER_PHONE_OR_EMAIL?.split(','),
756757
authToken: envs.TWITTER_AUTH_TOKEN?.split(','),
758+
thirdPartyApi: envs.TWITTER_THIRD_PARTY_API,
757759
},
758760
uestc: {
759761
bbsCookie: envs.UESTC_BBS_COOKIE,

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

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
1-
import { baseUrl, gqlMap, gqlFeatures } from './constants';
1+
import { baseUrl, gqlMap, gqlFeatures, gqlMapThirdParty } from './constants';
22
import { config } from '@/config';
33
import cache from '@/utils/cache';
44
import { twitterGot, paginationTweets, gatherLegacyFromData } from './utils';
55
import InvalidParameterError from '@/errors/types/invalid-parameter';
6+
import ofetch from '@/utils/ofetch';
67

78
const getUserData = (id) =>
89
cache.tryGet(`twitter-userdata-${id}`, () => {
9-
if (id.startsWith('+')) {
10-
return twitterGot(`${baseUrl}${gqlMap.UserByRestId}`, {
11-
variables: JSON.stringify({
12-
userId: id.slice(1),
13-
withSafetyModeUserFields: true,
14-
}),
15-
features: JSON.stringify(gqlFeatures.UserByRestId),
16-
fieldToggles: JSON.stringify({
17-
withAuxiliaryUserLabels: false,
18-
}),
10+
const params = {
11+
variables: id.startsWith('+')
12+
? JSON.stringify({
13+
userId: id.slice(1),
14+
withSafetyModeUserFields: true,
15+
})
16+
: JSON.stringify({
17+
screen_name: id,
18+
withSafetyModeUserFields: true,
19+
}),
20+
features: JSON.stringify(id.startsWith('+') ? gqlFeatures.UserByRestId : gqlFeatures.UserByScreenName),
21+
fieldToggles: JSON.stringify({
22+
withAuxiliaryUserLabels: false,
23+
}),
24+
};
25+
26+
if (config.twitter.thirdPartyApi) {
27+
const endpoint = id.startsWith('+') ? gqlMapThirdParty.UserByRestId : gqlMapThirdParty.UserByScreenName;
28+
29+
return ofetch(`${config.twitter.thirdPartyApi}${endpoint}`, {
30+
method: 'GET',
31+
params,
1932
});
2033
}
21-
return twitterGot(
22-
`${baseUrl}${gqlMap.UserByScreenName}`,
23-
{
24-
variables: JSON.stringify({
25-
screen_name: id,
26-
withSafetyModeUserFields: true,
27-
}),
28-
features: JSON.stringify(gqlFeatures.UserByScreenName),
29-
fieldToggles: JSON.stringify({
30-
withAuxiliaryUserLabels: false,
31-
}),
32-
},
33-
{
34-
allowNoAuth: true,
35-
}
36-
);
34+
35+
return twitterGot(`${baseUrl}${id.startsWith('+') ? gqlMap.UserByRestId : gqlMap.UserByScreenName}`, params, {
36+
allowNoAuth: !id.startsWith('+'),
37+
});
3738
});
3839

3940
const cacheTryGet = async (_id, params, func) => {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ const graphQLEndpointsPlain = [
1515

1616
const gqlMap = Object.fromEntries(graphQLEndpointsPlain.map((endpoint) => [endpoint.split('/')[3].replace(/V2$|Query$|QueryV2$/, ''), endpoint]));
1717

18+
const graphQLEndpointsThirdParty = ['/graphql/xxxxxxx/UserByScreenName', '/graphql/xxxxxxx/UserByRestId'];
19+
20+
const gqlMapThirdParty = Object.fromEntries(graphQLEndpointsThirdParty.map((endpoint) => [endpoint.split('/')[3].replace(/V2$|Query$|QueryV2$/, ''), endpoint]));
21+
1822
const gqlFeatureUser = {
1923
hidden_profile_subscriptions_enabled: true,
2024
rweb_tipjar_consumption_enabled: true,
@@ -112,4 +116,4 @@ const timelineParams = {
112116

113117
const bearerToken = 'Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA';
114118

115-
export { baseUrl, gqlMap, gqlFeatures, timelineParams, bearerToken };
119+
export { baseUrl, gqlMap, gqlMapThirdParty, gqlFeatures, timelineParams, bearerToken };

lib/routes/twitter/user.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ export const route: Route = {
3232
name: 'TWITTER_AUTH_TOKEN',
3333
description: 'Please see above for details.',
3434
},
35+
{
36+
name: 'TWITTER_THIRD_PARTY_API',
37+
description: 'Use third-party API to query twitter data',
38+
optional: true,
39+
},
3540
],
3641
requirePuppeteer: false,
3742
antiCrawler: false,

0 commit comments

Comments
 (0)