feat(twitter): following for webapi

This commit is contained in:
DIYgod
2024-03-19 22:36:56 +08:00
parent 1bc24075c7
commit c5ea105fab
4 changed files with 63 additions and 41 deletions
+2
View File
@@ -16,6 +16,7 @@ let api: {
getUserTweet: ApiItem;
getSearch: ApiItem;
getList: ApiItem;
getHomeTimeline: ApiItem;
} = {
init: () => {
throw new Error('Twitter API is not configured');
@@ -28,6 +29,7 @@ let api: {
getUserTweet: () => null,
getSearch: () => null,
getList: () => null,
getHomeTimeline: () => null,
};
if (enableWebApi) {
+18
View File
@@ -149,6 +149,23 @@ const getUser = async (id: string) => {
return (userData.data?.user || userData.data?.user_result)?.result?.legacy;
};
const getHomeTimeline = async (id: string, params?: Record<string, any>) =>
gatherLegacyFromData(
await paginationTweets(
'HomeTimeline',
undefined,
{
...params,
count: 20,
includePromotedContent: true,
latestControlAvailable: true,
requestContext: 'launch',
withCommunity: true,
},
['home', 'home_timeline_urt']
)
);
export default {
getUser,
getUserTweets,
@@ -158,5 +175,6 @@ export default {
getUserTweet,
getSearch,
getList,
getHomeTimeline,
init: () => {},
};
@@ -129,6 +129,28 @@ const gqlFeatures = {
longform_notetweets_inline_media_enabled: true,
responsive_web_enhance_cards_enabled: false,
},
HomeTimeline: {
responsive_web_graphql_exclude_directive_enabled: true,
verified_phone_label_enabled: false,
creator_subscriptions_tweet_preview_api_enabled: true,
responsive_web_graphql_timeline_navigation_enabled: true,
responsive_web_graphql_skip_user_profile_image_extensions_enabled: false,
c9s_tweet_anatomy_moderator_badge_enabled: true,
tweetypie_unmention_optimization_enabled: true,
responsive_web_edit_tweet_api_enabled: true,
graphql_is_translatable_rweb_tweet_is_translatable_enabled: true,
view_counts_everywhere_api_enabled: true,
longform_notetweets_consumption_enabled: true,
responsive_web_twitter_article_tweet_consumption_enabled: true,
tweet_awards_web_tipping_enabled: false,
freedom_of_speech_not_reach_fetch_enabled: true,
standardized_nudges_misinfo: true,
tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled: true,
rweb_video_timestamps_enabled: true,
longform_notetweets_rich_text_read_enabled: true,
longform_notetweets_inline_media_enabled: true,
responsive_web_enhance_cards_enabled: false,
},
};
const timelineParams = {
+21 -41
View File
@@ -1,14 +1,11 @@
import { Route } from '@/types';
import utils from './utils';
import { config } from '@/config';
const T = {};
import { TwitterApi } from 'twitter-api-v2';
import { fallback, queryToBoolean } from '@/utils/readable-social';
import api from './api';
export const route: Route = {
path: '/followings/:id/:routeParams?',
path: '/followings/:routeParams?',
categories: ['social-media'],
example: '/twitter/followings/DIYgod',
example: '/twitter/followings',
parameters: { id: 'username', routeParams: 'extra parameters, see the table above' },
features: {
requireConfig: [
@@ -20,6 +17,10 @@ export const route: Route = {
name: 'TWITTER_PASSWORD',
description: '',
},
{
name: 'TWITTER_COOKIE',
description: '',
},
],
requirePuppeteer: false,
antiCrawler: false,
@@ -30,46 +31,25 @@ export const route: Route = {
name: 'User following timeline',
maintainers: ['DIYgod'],
handler,
description: `:::warning
This route requires Twitter token's corresponding id, therefore it's only available when self-hosting, refer to the [Deploy Guide](/install/#route-specific-configurations) for route-specific configurations.
:::`,
};
async function handler(ctx) {
const id = ctx.req.param('id');
const cookie = config.twitter.tokens[id];
if (!cookie) {
throw new Error(`lacking twitter token for user ${id}`);
} else if (!T[id]) {
const token = cookie.split(',');
T[id] = new TwitterApi({
appKey: token[0],
appSecret: token[1],
accessToken: token[2],
accessSecret: token[3],
}).readOnly;
// For compatibility
const { count, include_rts } = utils.parseRouteParams(ctx.req.param('routeParams'));
const params = count ? { count } : {};
await api.init();
let data = await api.getHomeTimeline('', params);
if (!include_rts) {
data = utils.excludeRetweet(data);
}
const data = await T[id].v1.get('statuses/home_timeline.json', {
tweet_mode: 'extended',
count: 100,
});
// undefined and strings like "exclude_rts_replies" is also safely parsed, so no if branch is needed
const routeParams = new URLSearchParams(ctx.req.param('routeParams'));
return {
title: `${id} 的 Twitter 关注时间线`,
link: `https://twitter.com/${id}/`,
item: utils.ProcessFeed(
ctx,
{
data,
},
{
showAuthorInTitle: fallback(undefined, queryToBoolean(routeParams.get('showAuthorInTitle')), true),
showAuthorInDesc: fallback(undefined, queryToBoolean(routeParams.get('showAuthorInDesc')), true),
}
),
title: `Twitter following timeline`,
link: `https://twitter.com/home`,
// description: userInfo?.description,
item: utils.ProcessFeed(ctx, {
data,
}),
};
}