feat(route/twitter): add third-party twitter api support

This commit is contained in:
pseudoyu
2024-11-29 13:50:59 +08:00
parent 52c8c8f955
commit 3f12f922bf
4 changed files with 40 additions and 28 deletions
+2
View File
@@ -330,6 +330,7 @@ export type Config = {
authenticationSecret?: string[];
phoneOrEmail?: string[];
authToken?: string[];
thirdPartyApi?: string;
};
uestc: {
bbsCookie?: string;
@@ -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,
+28 -27
View File
@@ -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) => {
+5 -1
View File
@@ -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,
@@ -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
View File
@@ -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,