Skip to content

Commit

Permalink
fix(route): fix /twitter/likes (DIYgod#15384)
Browse files Browse the repository at this point in the history
* fix(route): fix /twitter/likes

* fix: delete not used variables

* update: router /twitter/likes config

* feat: throw a error for Twitter Premium accounts.

* feat: throw a error for Twitter Premium accounts
  • Loading branch information
ZhangChunXian authored May 4, 2024
1 parent 41a8eec commit 75d8152
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
15 changes: 14 additions & 1 deletion lib/routes/twitter/api/web-api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,20 @@ const getUserMedia = (id: string, params?: Record<string, any>) =>
);
});

const getUserLikes = (id: string, params?: Record<string, any>) => cacheTryGet(id, params, async (id, params = {}) => gatherLegacyFromData(await paginationTweets('Likes', id, params)));
const getUserLikes = (id: string, params?: Record<string, any>) =>
cacheTryGet(id, params, async (id, params = {}) =>
gatherLegacyFromData(
await paginationTweets(
'Likes',
id,
{
...params,
includeHasBirdwatchNotes: false,
includePromotedContent: false,
withBirdwatchNotes: false,
withVoice: false,
withV2Timeline: true,
})));

const getUserTweet = (id: string, params?: Record<string, any>) =>
cacheTryGet(id, params, async (id, params = {}) =>
Expand Down
1 change: 1 addition & 0 deletions lib/routes/twitter/api/web-api/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const gqlFeatures = {
ListLatestTweetsTimeline: gqlFeatureFeed,
HomeTimeline: gqlFeatureFeed,
TweetDetail: TweetDetailFeatures,
Likes: gqlFeatureFeed
};

const timelineParams = {
Expand Down
6 changes: 5 additions & 1 deletion lib/routes/twitter/api/web-api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ export const paginationTweets = async (endpoint: string, userId: number | undefi
}
instructions = instructions.instructions;
} else {
instructions = data.user.result.timeline_v2.timeline.instructions;
if (data?.user?.result?.timeline_v2?.timeline?.instructions) {
instructions = data.user.result.timeline_v2.timeline.instructions;
} else {
throw new Error('Because Twitter Premium has features that hide your likes, this RSS link is not available for Twitter Premium accounts.');
}
}

const entries1 = instructions.find((i) => i.type === 'TimelineAddToModule')?.moduleItems; // Media
Expand Down
26 changes: 15 additions & 11 deletions lib/routes/twitter/likes.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { Route } from '@/types';
import utils from './utils';
import { config } from '@/config';
import ConfigNotFoundError from '@/errors/types/config-not-found';
import api from './api';

export const route: Route = {
path: '/likes/:id/:routeParams?',
categories: ['social-media'],
example: '/twitter/likes/DIYgod',
parameters: { id: 'username', routeParams: 'extra parameters, see the table above' },
features: {
requireConfig: false,
requireConfig: [
{
name: 'TWITTER_COOKIE',
description: 'Please see above for details.',
},
],
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
Expand All @@ -22,15 +26,15 @@ export const route: Route = {
};

async function handler(ctx) {
if (!config.twitter || !config.twitter.consumer_key || !config.twitter.consumer_secret) {
throw new ConfigNotFoundError('Twitter RSS is disabled due to the lack of <a href="https://docs.rsshub.app/deploy/config#route-specific-configurations">relevant config</a>');
}
const id = ctx.req.param('id');
const client = await utils.getAppClient();
const data = await client.v1.get('favorites/list.json', {
screen_name: id,
tweet_mode: 'extended',
});
const { count, include_rts } = utils.parseRouteParams(ctx.req.param('routeParams'));
const params = count ? { count } : {};

await api.init();
let data = await api.getUserLikes(id, params);
if (!include_rts) {
data = utils.excludeRetweet(data);
}

return {
title: `Twitter Likes - ${id}`,
Expand Down

0 comments on commit 75d8152

Please sign in to comment.