fix(route/twitter/user): missing latest tweets (#12870)
* fix(route/twitter/user): missing latest tweets Signed-off-by: Rongrong <i@rong.moe> * Update lib/v2/twitter/web-api/twitter-api.js --------- Signed-off-by: Rongrong <i@rong.moe>
This commit is contained in:
@@ -2,6 +2,7 @@ const twitterGot = require('./twitter-got');
|
||||
const { graphQLMap, featuresMap, auth } = require('./constants');
|
||||
const config = require('@/config').value;
|
||||
const got = require('@/utils/got');
|
||||
const logger = require('@/utils/logger');
|
||||
|
||||
// https://github.com/mikf/gallery-dl/blob/a53cfc845e12d9e98fefd07e43ebffaec488c18f/gallery_dl/extractor/twitter.py#L727-L755
|
||||
const _params = {
|
||||
@@ -232,7 +233,45 @@ const cacheTryGet = async (cache, _id, params, func) => {
|
||||
return cache.tryGet(`twitter:${id}:${funcName}:${paramsString}`, () => func(id, params), config.cache.routeExpire, false);
|
||||
};
|
||||
|
||||
const getUserTweets = (cache, id, params = {}) => cacheTryGet(cache, id, params, getUserTweetsByID);
|
||||
// returns:
|
||||
// 1. nothing for some users
|
||||
// 2. HOT tweets for the other users, instead of the LATEST ones
|
||||
const _getUserTweets = (cache, id, params = {}) => cacheTryGet(cache, id, params, getUserTweetsByID);
|
||||
// workaround for the above issue:
|
||||
// 1. getUserTweetsAndReplies return LATEST tweets and replies, which requires filtering
|
||||
// a. if one replies a lot (e.g. elonmusk), there is sometimes no tweets left after filtering, caching may help
|
||||
// 2. getUserMedia return LATEST media tweets, which is a good plus
|
||||
const getUserTweets = async (cache, id, params = {}) => {
|
||||
let tweets = [];
|
||||
const rest_id = await getUserID(cache, id);
|
||||
await Promise.all(
|
||||
[_getUserTweets, getUserTweetsAndReplies, getUserMedia].map(async (func) => {
|
||||
try {
|
||||
tweets.push(...(await func(cache, id, params)));
|
||||
} catch (e) {
|
||||
logger.warn(`Failed to get tweets for ${id} with ${func.name}: ${e}`);
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
const cacheKey = `twitter:user:tweets-cache:${rest_id}`;
|
||||
let cacheValue = await cache.get(cacheKey);
|
||||
if (cacheValue) {
|
||||
cacheValue = JSON.parse(cacheValue);
|
||||
if (cacheValue && cacheValue.length) {
|
||||
tweets = cacheValue.concat(tweets);
|
||||
}
|
||||
}
|
||||
const idSet = new Set();
|
||||
tweets = tweets
|
||||
.filter((tweet) => !tweet.in_reply_to_screen_name) // exclude replies
|
||||
.map((tweet) => (idSet.has(tweet.id_str) ? null : idSet.add(tweet.id_str)) && tweet) // deduplicate
|
||||
.filter(Boolean) // remove null
|
||||
.sort((a, b) => b.id_str - a.id_str) // desc
|
||||
.slice(0, 20);
|
||||
cache.set(cacheKey, JSON.stringify(tweets));
|
||||
return tweets;
|
||||
};
|
||||
const getUserTweetsAndReplies = (cache, id, params = {}) => cacheTryGet(cache, id, params, getUserTweetsAndRepliesByID);
|
||||
const getUserMedia = (cache, id, params = {}) => cacheTryGet(cache, id, params, getUserMediaByID);
|
||||
const getUserLikes = (cache, id, params = {}) => cacheTryGet(cache, id, params, getUserLikesByID);
|
||||
|
||||
Reference in New Issue
Block a user