Skip to content

Commit

Permalink
fix: twitter keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Jul 23, 2023
1 parent 79bfb3a commit 48626a8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 38 deletions.
3 changes: 3 additions & 0 deletions lib/v2/twitter/web-api/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ const featuresMap = {
}),
};

const auth = 'Bearer AAAAAAAAAAAAAAAAAAAAAFQODgEAAAAAVHTp76lzh3rFzcHbmHVvQxYYpTw%3DckAlMINMjmCwxUcaXbAN4XqJVdgMJaHqNOFgPMK0zN1qLqLQCF';

module.exports = {
tokens,
graphQLMap,
featuresMap,
auth,
};
52 changes: 14 additions & 38 deletions lib/v2/twitter/web-api/twitter-api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const twitterGot = require('./twitter-got');
const { graphQLMap, featuresMap } = require('./constants');
const { graphQLMap, featuresMap, auth } = require('./constants');
const config = require('@/config').value;
const got = require('@/utils/got');

// https://github.com/mikf/gallery-dl/blob/a53cfc845e12d9e98fefd07e43ebffaec488c18f/gallery_dl/extractor/twitter.py#L727-L755
const _params = {
Expand Down Expand Up @@ -104,13 +105,17 @@ const timelineLikes = (userId, params = {}) => paginationTweets(graphQLMap.Likes

// https://github.com/mikf/gallery-dl/blob/a53cfc845e12d9e98fefd07e43ebffaec488c18f/gallery_dl/extractor/twitter.py#L858-L866
const timelineKeywords = (keywords, params = {}) =>
twitterGot('https://twitter.com/i/api/2/search/adaptive.json', {
..._params,
...params,
q: keywords,
tweet_search_mode: 'live',
query_source: 'typed_query',
pc: 1,
got('https://api.twitter.com/1.1/search/universal.json', {
headers: {
Authorization: auth,
},
searchParams: {
..._params,
...params,
q: keywords,
modules: 'status',
result_type: 'recent',
},
});

// https://github.com/mikf/gallery-dl/blob/a53cfc845e12d9e98fefd07e43ebffaec488c18f/gallery_dl/extractor/twitter.py#L795-L805
Expand Down Expand Up @@ -180,35 +185,6 @@ function gatherLegacyFromData(entries, filter = 'tweet-') {
return tweets;
}

function pickLegacyByID(id, tweets_dict, users_dict) {
function pickLegacyFromTweet(tweet) {
tweet.user = users_dict[tweet.user_id_str];
if (tweet.retweeted_status_id_str) {
tweet.retweeted_status = pickLegacyFromTweet(tweets_dict[tweet.retweeted_status_id_str]);
}
return tweet;
}

if (tweets_dict[id]) {
return pickLegacyFromTweet(tweets_dict[id]);
}
}

function gatherLegacyFromLegacyApiData(data, filter = 'tweet-') {
const tweets_dict = data.globalObjects.tweets;
const users_dict = data.globalObjects.users;
const tweets = [];
data.timeline.instructions[0].addEntries.entries.forEach((entry) => {
if (entry.entryId && entry.entryId.indexOf(filter) !== -1) {
const tweet = pickLegacyByID(entry.content.item.content.tweet.id, tweets_dict, users_dict);
if (tweet) {
tweets.push(tweet);
}
}
});
return tweets;
}

const getUserTweetsByID = async (id, params = {}) => gatherLegacyFromData(await timelineTweets(id, params));
const getUserTweetsAndRepliesByID = async (id, params = {}) => gatherLegacyFromData(await timelineTweetsAndReplies(id, params));
const getUserMediaByID = async (id, params = {}) => gatherLegacyFromData(await timelineMedia(id, params));
Expand Down Expand Up @@ -262,7 +238,7 @@ const getUserMedia = (cache, id, params = {}) => cacheTryGet(cache, id, params,
const getUserLikes = (cache, id, params = {}) => cacheTryGet(cache, id, params, getUserLikesByID);
const getUserTweet = (cache, id, params) => cacheTryGet(cache, id, params, getUserTweetByStatus);

const getSearch = async (keywords, params = {}) => gatherLegacyFromLegacyApiData(await timelineKeywords(keywords, params), 'sq-I-t-');
const getSearch = async (keywords, params = {}) => (await timelineKeywords(keywords, params)).data.modules.map((module) => module.status.data);

module.exports = {
getUser,
Expand Down

0 comments on commit 48626a8

Please sign in to comment.