refactor: switch to twitter-api-v2 from twit (#9652)
This commit is contained in:
@@ -2,14 +2,14 @@ const utils = require('../utils');
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const keyword = ctx.params.keyword;
|
||||
const limit = ctx.params.limit || 50;
|
||||
const result = await utils.getTwit().get('search/tweets', {
|
||||
const limit = ctx.params.limit ?? 50;
|
||||
const client = await utils.getAppClient();
|
||||
const data = await client.v1.get('search/tweets.json', {
|
||||
q: keyword,
|
||||
count: limit,
|
||||
tweet_mode: 'extended',
|
||||
result_type: 'recent',
|
||||
});
|
||||
const data = result.data;
|
||||
|
||||
ctx.state.data = {
|
||||
title: `Twitter Keyword - ${keyword}`,
|
||||
|
||||
@@ -5,14 +5,14 @@ module.exports = async (ctx) => {
|
||||
|
||||
// For compatibility
|
||||
const { exclude_replies, include_rts, count } = utils.parseRouteParams(ctx.params.routeParams);
|
||||
const result = await utils.getTwit().get('statuses/user_timeline', {
|
||||
const client = await utils.getAppClient();
|
||||
const data = await client.v1.get('statuses/user_timeline.json', {
|
||||
screen_name: id,
|
||||
tweet_mode: 'extended',
|
||||
exclude_replies,
|
||||
include_rts,
|
||||
count,
|
||||
});
|
||||
const data = result.data;
|
||||
const userInfo = data[0].user;
|
||||
|
||||
const profileImageUrl = userInfo.profile_image_url || userInfo.profile_image_url_https;
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
const querystring = require('querystring');
|
||||
const utils = require('./utils');
|
||||
const config = require('@/config').value;
|
||||
const T = {};
|
||||
const Twit = require('twit');
|
||||
const { TwitterApi } = require('twitter-api-v2');
|
||||
const { fallback, queryToBoolean } = require('@/utils/readable-social');
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
@@ -12,22 +11,21 @@ module.exports = async (ctx) => {
|
||||
throw Error(`lacking twitter token for user ${id}`);
|
||||
} else if (!T[id]) {
|
||||
const token = cookie.split(',');
|
||||
T[id] = new Twit({
|
||||
consumer_key: token[0],
|
||||
consumer_secret: token[1],
|
||||
access_token: token[2],
|
||||
access_token_secret: token[3],
|
||||
});
|
||||
T[id] = new TwitterApi({
|
||||
appKey: token[0],
|
||||
appSecret: token[1],
|
||||
accessToken: token[2],
|
||||
accessSecret: token[3],
|
||||
}).readOnly;
|
||||
}
|
||||
|
||||
const result = await T[id].get('statuses/home_timeline', {
|
||||
const data = await T[id].v1.get('statuses/home_timeline.json', {
|
||||
tweet_mode: 'extended',
|
||||
count: 100,
|
||||
});
|
||||
const data = result.data;
|
||||
|
||||
// undefined and strings like "exclude_rts_replies" is also safely parsed, so no if branch is needed
|
||||
const routeParams = querystring.parse(ctx.params.routeParams);
|
||||
const routeParams = new URLSearchParams(ctx.params.routeParams);
|
||||
|
||||
ctx.state.data = {
|
||||
title: `${id} 的 Twitter 关注时间线`,
|
||||
@@ -38,8 +36,8 @@ module.exports = async (ctx) => {
|
||||
data,
|
||||
},
|
||||
{
|
||||
showAuthorInTitle: fallback(undefined, queryToBoolean(routeParams.showAuthorInTitle), true),
|
||||
showAuthorInDesc: fallback(undefined, queryToBoolean(routeParams.showAuthorInDesc), true),
|
||||
showAuthorInTitle: fallback(undefined, queryToBoolean(routeParams.get('showAuthorInTitle')), true),
|
||||
showAuthorInDesc: fallback(undefined, queryToBoolean(routeParams.get('showAuthorInDesc')), true),
|
||||
}
|
||||
),
|
||||
};
|
||||
|
||||
@@ -6,11 +6,11 @@ module.exports = async (ctx) => {
|
||||
throw 'Twitter RSS is disabled due to the lack of <a href="https://docs.rsshub.app/install/#pei-zhi-bu-fen-rss-mo-kuai-pei-zhi">relevant config</a>';
|
||||
}
|
||||
const id = ctx.params.id;
|
||||
const result = await utils.getTwit().get('favorites/list', {
|
||||
const client = await utils.getAppClient();
|
||||
const data = await client.v1.get('favorites/list.json', {
|
||||
screen_name: id,
|
||||
tweet_mode: 'extended',
|
||||
});
|
||||
const data = result.data;
|
||||
|
||||
ctx.state.data = {
|
||||
title: `Twitter Likes - ${id}`,
|
||||
|
||||
@@ -6,14 +6,15 @@ module.exports = async (ctx) => {
|
||||
throw 'Twitter RSS is disabled due to the lack of <a href="https://docs.rsshub.app/install/#pei-zhi-bu-fen-rss-mo-kuai-pei-zhi">relevant config</a>';
|
||||
}
|
||||
const { id, name } = ctx.params;
|
||||
const client = await utils.getAppClient();
|
||||
|
||||
const list_data = await ctx.cache.tryGet(`twitter_lists_list_screen_name:${id}`, async () => {
|
||||
const result = await utils.getTwit().get('lists/list', {
|
||||
const data = await client.v1.get('lists/list.json', {
|
||||
screen_name: id,
|
||||
});
|
||||
|
||||
const cached_lists = {};
|
||||
result.data.forEach((e) => {
|
||||
data.forEach((e) => {
|
||||
cached_lists[e.name] = { id: e.id_str, slug: e.slug };
|
||||
});
|
||||
|
||||
@@ -21,12 +22,11 @@ module.exports = async (ctx) => {
|
||||
});
|
||||
const cur_list = list_data[name];
|
||||
|
||||
const result = await utils.getTwit().get('lists/statuses', {
|
||||
const data = await client.v1.get('lists/statuses.json', {
|
||||
list_id: cur_list.id,
|
||||
slug: cur_list.slug,
|
||||
tweet_mode: 'extended',
|
||||
});
|
||||
const data = result.data;
|
||||
|
||||
ctx.state.data = {
|
||||
title: `Twitter List - ${id}/${name}`,
|
||||
|
||||
@@ -5,12 +5,13 @@ module.exports = async (ctx) => {
|
||||
if (!config.twitter || !config.twitter.consumer_key || !config.twitter.consumer_secret) {
|
||||
throw 'Twitter RSS is disabled due to the lack of <a href="https://docs.rsshub.app/install/#pei-zhi-bu-fen-rss-mo-kuai-pei-zhi">relevant config</a>';
|
||||
}
|
||||
const woeid = ctx.params.woeid || 1; // Global information is available by using 1 as the WOEID
|
||||
const result = await utils.getTwit().get('trends/place', { id: woeid });
|
||||
const [{ trends }] = result.data;
|
||||
const woeid = ctx.params.woeid ?? 1; // Global information is available by using 1 as the WOEID
|
||||
const client = await utils.getAppClient();
|
||||
const data = await client.v1.get('trends/place.json', { id: woeid });
|
||||
const [{ trends }] = data;
|
||||
|
||||
ctx.state.data = {
|
||||
title: `Twitter Trends on ${result.data[0].locations[0].name}`,
|
||||
title: `Twitter Trends on ${data[0].locations[0].name}`,
|
||||
link: `https://twitter.com/i/trends`,
|
||||
item: trends
|
||||
.filter((t) => !t.promoted_content)
|
||||
|
||||
+72
-70
@@ -1,30 +1,61 @@
|
||||
const querystring = require('querystring');
|
||||
const URL = require('url');
|
||||
const config = require('@/config').value;
|
||||
const Twit = require('twit');
|
||||
const { TwitterApi } = require('twitter-api-v2');
|
||||
const { fallback, queryToBoolean, queryToInteger } = require('@/utils/readable-social');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
|
||||
const getQueryParams = (url) => URL.parse(url, true).query;
|
||||
const getOriginalImg = (url) => {
|
||||
// https://greasyfork.org/zh-CN/scripts/2312-resize-image-on-open-image-in-new-tab/code#n150
|
||||
let m = null;
|
||||
if ((m = url.match(/^(https?:\/\/\w+\.twimg\.com\/media\/[^/:]+)\.(jpg|jpeg|gif|png|bmp|webp)(:\w+)?$/i))) {
|
||||
let format = m[2];
|
||||
if (m[2] === 'jpeg') {
|
||||
format = 'jpg';
|
||||
}
|
||||
return `${m[1]}?format=${format}&name=orig`;
|
||||
} else if ((m = url.match(/^(https?:\/\/\w+\.twimg\.com\/.+)(\?.+)$/i))) {
|
||||
const pars = getQueryParams(url);
|
||||
if (!pars.format || !pars.name) {
|
||||
return url;
|
||||
}
|
||||
if (pars.name === 'orig') {
|
||||
return url;
|
||||
}
|
||||
return m[1] + '?format=' + pars.format + '&name=orig';
|
||||
} else {
|
||||
return url;
|
||||
}
|
||||
};
|
||||
const replaceBreak = (text) => text.replace(/<br><br>|<br>/g, ' ');
|
||||
const formatText = (text) =>
|
||||
text
|
||||
.replace(/https:\/\/t\.co(.*)/g, '')
|
||||
.trim()
|
||||
.replace(/\n/g, '<br>');
|
||||
const formatTextToPlain = (text) => text.replace(/https:\/\/t\.co(.*)/g, '').trim();
|
||||
|
||||
const ProcessFeed = (ctx, { data = [] }, params = {}) => {
|
||||
// undefined and strings like "exclude_rts_replies" is also safely parsed, so no if branch is needed
|
||||
const routeParams = querystring.parse(ctx.params.routeParams);
|
||||
const routeParams = new URLSearchParams(ctx.params.routeParams);
|
||||
|
||||
const mergedParams = {
|
||||
readable: fallback(params.readable, queryToBoolean(routeParams.readable), false),
|
||||
authorNameBold: fallback(params.authorNameBold, queryToBoolean(routeParams.authorNameBold), false),
|
||||
showAuthorInTitle: fallback(params.showAuthorInTitle, queryToBoolean(routeParams.showAuthorInTitle), false),
|
||||
showAuthorInDesc: fallback(params.showAuthorInDesc, queryToBoolean(routeParams.showAuthorInDesc), false),
|
||||
showQuotedAuthorAvatarInDesc: fallback(params.showQuotedAuthorAvatarInDesc, queryToBoolean(routeParams.showQuotedAuthorAvatarInDesc), false),
|
||||
showAuthorAvatarInDesc: fallback(params.showAuthorAvatarInDesc, queryToBoolean(routeParams.showAuthorAvatarInDesc), false),
|
||||
showEmojiForRetweetAndReply: fallback(params.showEmojiForRetweetAndReply, queryToBoolean(routeParams.showEmojiForRetweetAndReply), false),
|
||||
showRetweetTextInTitle: fallback(params.showRetweetTextInTitle, queryToBoolean(routeParams.showRetweetTextInTitle), true),
|
||||
addLinkForPics: fallback(params.addLinkForPics, queryToBoolean(routeParams.addLinkForPics), false),
|
||||
showTimestampInDescription: fallback(params.showTimestampInDescription, queryToBoolean(routeParams.showTimestampInDescription), false),
|
||||
showQuotedInTitle: fallback(params.showQuotedInTitle, queryToBoolean(routeParams.showQuotedInTitle), false),
|
||||
readable: fallback(params.readable, queryToBoolean(routeParams.get('readable')), false),
|
||||
authorNameBold: fallback(params.authorNameBold, queryToBoolean(routeParams.get('authorNameBold')), false),
|
||||
showAuthorInTitle: fallback(params.showAuthorInTitle, queryToBoolean(routeParams.get('showAuthorInTitle')), false),
|
||||
showAuthorInDesc: fallback(params.showAuthorInDesc, queryToBoolean(routeParams.get('showAuthorInDesc')), false),
|
||||
showQuotedAuthorAvatarInDesc: fallback(params.showQuotedAuthorAvatarInDesc, queryToBoolean(routeParams.get('showQuotedAuthorAvatarInDesc')), false),
|
||||
showAuthorAvatarInDesc: fallback(params.showAuthorAvatarInDesc, queryToBoolean(routeParams.get('showAuthorAvatarInDesc')), false),
|
||||
showEmojiForRetweetAndReply: fallback(params.showEmojiForRetweetAndReply, queryToBoolean(routeParams.get('showEmojiForRetweetAndReply')), false),
|
||||
showRetweetTextInTitle: fallback(params.showRetweetTextInTitle, queryToBoolean(routeParams.get('showRetweetTextInTitle')), true),
|
||||
addLinkForPics: fallback(params.addLinkForPics, queryToBoolean(routeParams.get('addLinkForPics')), false),
|
||||
showTimestampInDescription: fallback(params.showTimestampInDescription, queryToBoolean(routeParams.get('showTimestampInDescription')), false),
|
||||
showQuotedInTitle: fallback(params.showQuotedInTitle, queryToBoolean(routeParams.get('showQuotedInTitle')), false),
|
||||
|
||||
widthOfPics: fallback(params.widthOfPics, queryToInteger(routeParams.widthOfPics), -1),
|
||||
heightOfPics: fallback(params.heightOfPics, queryToInteger(routeParams.heightOfPics), -1),
|
||||
sizeOfAuthorAvatar: fallback(params.sizeOfAuthorAvatar, queryToInteger(routeParams.sizeOfAuthorAvatar), 48),
|
||||
sizeOfQuotedAuthorAvatar: fallback(params.sizeOfQuotedAuthorAvatar, queryToInteger(routeParams.sizeOfQuotedAuthorAvatar), 24),
|
||||
widthOfPics: fallback(params.widthOfPics, queryToInteger(routeParams.get('widthOfPics')), -1),
|
||||
heightOfPics: fallback(params.heightOfPics, queryToInteger(routeParams.get('heightOfPics')), -1),
|
||||
sizeOfAuthorAvatar: fallback(params.sizeOfAuthorAvatar, queryToInteger(routeParams.get('sizeOfAuthorAvatar')), 48),
|
||||
sizeOfQuotedAuthorAvatar: fallback(params.sizeOfQuotedAuthorAvatar, queryToInteger(routeParams.get('sizeOfQuotedAuthorAvatar')), 24),
|
||||
};
|
||||
|
||||
params = mergedParams;
|
||||
@@ -48,37 +79,6 @@ const ProcessFeed = (ctx, { data = [] }, params = {}) => {
|
||||
sizeOfQuotedAuthorAvatar,
|
||||
} = params;
|
||||
|
||||
const getQueryParams = (url) => URL.parse(url, true).query;
|
||||
const getOriginalImg = (url) => {
|
||||
// https://greasyfork.org/zh-CN/scripts/2312-resize-image-on-open-image-in-new-tab/code#n150
|
||||
let m = null;
|
||||
if ((m = url.match(/^(https?:\/\/\w+\.twimg\.com\/media\/[^/:]+)\.(jpg|jpeg|gif|png|bmp|webp)(:\w+)?$/i))) {
|
||||
let format = m[2];
|
||||
if (m[2] === 'jpeg') {
|
||||
format = 'jpg';
|
||||
}
|
||||
return `${m[1]}?format=${format}&name=orig`;
|
||||
} else if ((m = url.match(/^(https?:\/\/\w+\.twimg\.com\/.+)(\?.+)$/i))) {
|
||||
const pars = getQueryParams(url);
|
||||
if (!pars.format || !pars.name) {
|
||||
return url;
|
||||
}
|
||||
if (pars.name === 'orig') {
|
||||
return url;
|
||||
}
|
||||
return m[1] + '?format=' + pars.format + '&name=orig';
|
||||
} else {
|
||||
return url;
|
||||
}
|
||||
};
|
||||
|
||||
const replaceBreak = (text) => text.replace(/<br><br>|<br>/g, ' ');
|
||||
const formatText = (text) =>
|
||||
text
|
||||
.replace(/https:\/\/t\.co(.*)/g, '')
|
||||
.trim()
|
||||
.replace(/\n/g, '<br>');
|
||||
const formatTextToPlain = (text) => text.replace(/https:\/\/t\.co(.*)/g, '').trim();
|
||||
const formatVideo = (media, extraAttrs = '') => {
|
||||
let content = '';
|
||||
const video = media.video_info.variants.reduce((video, item) => {
|
||||
@@ -146,6 +146,7 @@ const ProcessFeed = (ctx, { data = [] }, params = {}) => {
|
||||
}
|
||||
return img;
|
||||
};
|
||||
|
||||
const generatePicsPrefix = (item) => {
|
||||
// When author avatar is shown, generate invisible <img> for inner images at the beginning of HTML
|
||||
// to please some RSS readers
|
||||
@@ -170,6 +171,7 @@ const ProcessFeed = (ctx, { data = [] }, params = {}) => {
|
||||
});
|
||||
return picsPrefix;
|
||||
};
|
||||
|
||||
const formatUrl = (item) => {
|
||||
let url = '';
|
||||
item.entities.urls &&
|
||||
@@ -246,7 +248,7 @@ const ProcessFeed = (ctx, { data = [] }, params = {}) => {
|
||||
quote += `<br><small>Link: <a href='https://twitter.com/${author.screen_name}/status/${quoteData.id_str}' target='_blank' rel='noopener noreferrer'>https://twitter.com/${author.screen_name}/status/${quoteData.id_str}</a></small>`;
|
||||
}
|
||||
if (showTimestampInDescription) {
|
||||
quote += '<br><small>' + new Date(quoteData.created_at).toLocaleString();
|
||||
quote += '<br><small>' + parseDate(quoteData.created_at);
|
||||
quote += `</small>`;
|
||||
if (readable) {
|
||||
quote += `<br clear='both' /><div style='clear: both'></div>`;
|
||||
@@ -368,7 +370,7 @@ const ProcessFeed = (ctx, { data = [] }, params = {}) => {
|
||||
}
|
||||
|
||||
if (showTimestampInDescription) {
|
||||
description += `<small>${new Date(item.created_at).toLocaleString()}</small>`;
|
||||
description += `<small>${parseDate(item.created_at)}</small>`;
|
||||
}
|
||||
|
||||
const authorName = originalItem.user.name;
|
||||
@@ -376,13 +378,14 @@ const ProcessFeed = (ctx, { data = [] }, params = {}) => {
|
||||
title,
|
||||
author: authorName,
|
||||
description,
|
||||
pubDate: new Date(item.created_at).toUTCString(),
|
||||
pubDate: parseDate(item.created_at),
|
||||
link: `https://twitter.com/${item.user.screen_name}/status/${item.id_str}`,
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
let getTwit = () => null;
|
||||
let getAppClient = () => null;
|
||||
|
||||
if (config.twitter.consumer_key && config.twitter.consumer_secret) {
|
||||
const consumer_keys = config.twitter.consumer_key.split(',');
|
||||
const consumer_secrets = config.twitter.consumer_secret.split(',');
|
||||
@@ -390,25 +393,24 @@ if (config.twitter.consumer_key && config.twitter.consumer_secret) {
|
||||
let count = 0;
|
||||
let index = -1;
|
||||
|
||||
consumer_keys.forEach((consumer_key, index) => {
|
||||
const consumer_secret = consumer_secrets[index];
|
||||
consumer_keys.forEach((consumer_key, i) => {
|
||||
const consumer_secret = consumer_secrets[i];
|
||||
if (consumer_key && consumer_secret) {
|
||||
T[index] = new Twit({
|
||||
consumer_key,
|
||||
consumer_secret,
|
||||
app_only_auth: true,
|
||||
});
|
||||
count = index + 1;
|
||||
T[i] = new TwitterApi({
|
||||
appKey: consumer_key,
|
||||
appSecret: consumer_secret,
|
||||
}).readOnly;
|
||||
count = i + 1;
|
||||
}
|
||||
});
|
||||
|
||||
getTwit = () => {
|
||||
getAppClient = () => {
|
||||
index++;
|
||||
return T[index % count];
|
||||
return T[index % count].appLogin();
|
||||
};
|
||||
}
|
||||
|
||||
function parseRouteParams(routeParams) {
|
||||
const parseRouteParams = (routeParams) => {
|
||||
let exclude_replies, include_rts;
|
||||
let count = undefined;
|
||||
if (routeParams === 'exclude_rts_replies' || routeParams === 'exclude_replies_rts') {
|
||||
@@ -421,16 +423,16 @@ function parseRouteParams(routeParams) {
|
||||
exclude_replies = false;
|
||||
include_rts = false;
|
||||
} else {
|
||||
const parsed = querystring.parse(routeParams);
|
||||
count = fallback(undefined, queryToInteger(parsed.count), undefined);
|
||||
exclude_replies = fallback(undefined, queryToBoolean(parsed.excludeReplies), false);
|
||||
include_rts = fallback(undefined, queryToBoolean(parsed.includeRts), true);
|
||||
const parsed = new URLSearchParams(routeParams);
|
||||
count = fallback(undefined, queryToInteger(parsed.get('count')), undefined);
|
||||
exclude_replies = fallback(undefined, queryToBoolean(parsed.get('excludeReplies')), false);
|
||||
include_rts = fallback(undefined, queryToBoolean(parsed.get('includeRts')), true);
|
||||
}
|
||||
return { count, exclude_replies, include_rts };
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
ProcessFeed,
|
||||
getTwit,
|
||||
getAppClient,
|
||||
parseRouteParams,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user