Skip to content

Commit

Permalink
feat(route/twitter): add third-party twitter api support
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudoyu committed Nov 29, 2024
1 parent 52c8c8f commit 3f12f92
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 28 deletions.
2 changes: 2 additions & 0 deletions lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ export type Config = {
authenticationSecret?: string[];
phoneOrEmail?: string[];
authToken?: string[];
thirdPartyApi?: string;
};
uestc: {
bbsCookie?: string;
Expand Down Expand Up @@ -754,6 +755,7 @@ const calculateValue = () => {
authenticationSecret: envs.TWITTER_AUTHENTICATION_SECRET?.split(','),
phoneOrEmail: envs.TWITTER_PHONE_OR_EMAIL?.split(','),
authToken: envs.TWITTER_AUTH_TOKEN?.split(','),
thirdPartyApi: envs.TWITTER_THIRD_PARTY_API,
},
uestc: {
bbsCookie: envs.UESTC_BBS_COOKIE,
Expand Down
55 changes: 28 additions & 27 deletions lib/routes/twitter/api/web-api/api.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
import { baseUrl, gqlMap, gqlFeatures } from './constants';
import { baseUrl, gqlMap, gqlFeatures, gqlMapThirdParty } from './constants';
import { config } from '@/config';
import cache from '@/utils/cache';
import { twitterGot, paginationTweets, gatherLegacyFromData } from './utils';
import InvalidParameterError from '@/errors/types/invalid-parameter';
import ofetch from '@/utils/ofetch';

const getUserData = (id) =>
cache.tryGet(`twitter-userdata-${id}`, () => {
if (id.startsWith('+')) {
return twitterGot(`${baseUrl}${gqlMap.UserByRestId}`, {
variables: JSON.stringify({
userId: id.slice(1),
withSafetyModeUserFields: true,
}),
features: JSON.stringify(gqlFeatures.UserByRestId),
fieldToggles: JSON.stringify({
withAuxiliaryUserLabels: false,
}),
const params = {
variables: id.startsWith('+')
? JSON.stringify({
userId: id.slice(1),
withSafetyModeUserFields: true,
})
: JSON.stringify({
screen_name: id,
withSafetyModeUserFields: true,
}),
features: JSON.stringify(id.startsWith('+') ? gqlFeatures.UserByRestId : gqlFeatures.UserByScreenName),
fieldToggles: JSON.stringify({
withAuxiliaryUserLabels: false,
}),
};

if (config.twitter.thirdPartyApi) {
const endpoint = id.startsWith('+') ? gqlMapThirdParty.UserByRestId : gqlMapThirdParty.UserByScreenName;

return ofetch(`${config.twitter.thirdPartyApi}${endpoint}`, {
method: 'GET',
params,
});
}
return twitterGot(
`${baseUrl}${gqlMap.UserByScreenName}`,
{
variables: JSON.stringify({
screen_name: id,
withSafetyModeUserFields: true,
}),
features: JSON.stringify(gqlFeatures.UserByScreenName),
fieldToggles: JSON.stringify({
withAuxiliaryUserLabels: false,
}),
},
{
allowNoAuth: true,
}
);

return twitterGot(`${baseUrl}${id.startsWith('+') ? gqlMap.UserByRestId : gqlMap.UserByScreenName}`, params, {
allowNoAuth: !id.startsWith('+'),
});
});

const cacheTryGet = async (_id, params, func) => {
Expand Down
6 changes: 5 additions & 1 deletion lib/routes/twitter/api/web-api/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ const graphQLEndpointsPlain = [

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

const graphQLEndpointsThirdParty = ['/graphql/xxxxxxx/UserByScreenName', '/graphql/xxxxxxx/UserByRestId'];

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

const gqlFeatureUser = {
hidden_profile_subscriptions_enabled: true,
rweb_tipjar_consumption_enabled: true,
Expand Down Expand Up @@ -112,4 +116,4 @@ const timelineParams = {

const bearerToken = 'Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA';

export { baseUrl, gqlMap, gqlFeatures, timelineParams, bearerToken };
export { baseUrl, gqlMap, gqlMapThirdParty, gqlFeatures, timelineParams, bearerToken };
5 changes: 5 additions & 0 deletions lib/routes/twitter/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export const route: Route = {
name: 'TWITTER_AUTH_TOKEN',
description: 'Please see above for details.',
},
{
name: 'TWITTER_THIRD_PARTY_API',
description: 'Use third-party API to query twitter data',
optional: true,
},
],
requirePuppeteer: false,
antiCrawler: false,
Expand Down

0 comments on commit 3f12f92

Please sign in to comment.