From c5ea105fab0e96f987a19f34d7f07f1eb8b922ba Mon Sep 17 00:00:00 2001 From: DIYgod Date: Tue, 19 Mar 2024 22:36:56 +0800 Subject: [PATCH] feat(twitter): following for webapi --- lib/routes/twitter/api/index.ts | 2 + lib/routes/twitter/api/web-api/api.ts | 18 ++++++ lib/routes/twitter/api/web-api/constants.ts | 22 ++++++++ lib/routes/twitter/followings.ts | 62 +++++++-------------- 4 files changed, 63 insertions(+), 41 deletions(-) diff --git a/lib/routes/twitter/api/index.ts b/lib/routes/twitter/api/index.ts index a7d885f9c..10d721d09 100644 --- a/lib/routes/twitter/api/index.ts +++ b/lib/routes/twitter/api/index.ts @@ -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) { diff --git a/lib/routes/twitter/api/web-api/api.ts b/lib/routes/twitter/api/web-api/api.ts index 6063b1cc4..870c569e7 100644 --- a/lib/routes/twitter/api/web-api/api.ts +++ b/lib/routes/twitter/api/web-api/api.ts @@ -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) => + 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: () => {}, }; diff --git a/lib/routes/twitter/api/web-api/constants.ts b/lib/routes/twitter/api/web-api/constants.ts index 0982fc6a2..bfd1c904f 100644 --- a/lib/routes/twitter/api/web-api/constants.ts +++ b/lib/routes/twitter/api/web-api/constants.ts @@ -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 = { diff --git a/lib/routes/twitter/followings.ts b/lib/routes/twitter/followings.ts index b01a8080e..a12d93c80 100644 --- a/lib/routes/twitter/followings.ts +++ b/lib/routes/twitter/followings.ts @@ -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, + }), }; }