From eabf2c7eaf7c0a04af7948a77b56fd58a9c6ebd0 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Fri, 29 Nov 2019 18:16:00 +0800 Subject: [PATCH] feat: twitter keyword RSS, close #156 --- assets/radar-rules.js | 14 ++++++++++---- docs/en/social-media.md | 4 ++++ docs/social-media.md | 4 ++++ lib/router.js | 1 + lib/routes/twitter/keyword.js | 24 ++++++++++++++++++++++++ 5 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 lib/routes/twitter/keyword.js diff --git a/assets/radar-rules.js b/assets/radar-rules.js index 064583cfb2b436..1ff95538df4d37 100644 --- a/assets/radar-rules.js +++ b/assets/radar-rules.js @@ -110,28 +110,34 @@ docs: 'https://docs.rsshub.app/social-media.html#twitter', source: '/:id', target: '/twitter/user/:id', - verification: (params) => params.id !== 'home' && params.id !== 'explore' && params.id !== 'notifications' && params.id !== 'messages' && params.id !== 'explore', + verification: (params) => params.id !== 'home' && params.id !== 'explore' && params.id !== 'notifications' && params.id !== 'messages' && params.id !== 'explore' && params.id !== 'search', }, { title: '用户关注时间线', docs: 'https://docs.rsshub.app/social-media.html#twitter', source: '/:id', target: '/twitter/followings/:id', - verification: (params) => params.id !== 'home' && params.id !== 'explore' && params.id !== 'notifications' && params.id !== 'messages' && params.id !== 'explore', + verification: (params) => params.id !== 'home' && params.id !== 'explore' && params.id !== 'notifications' && params.id !== 'messages' && params.id !== 'explore' && params.id !== 'search', }, { title: '用户喜欢列表', docs: 'https://docs.rsshub.app/social-media.html#twitter', source: '/:id', target: '/twitter/likes/:id', - verification: (params) => params.id !== 'home' && params.id !== 'explore' && params.id !== 'notifications' && params.id !== 'messages' && params.id !== 'explore', + verification: (params) => params.id !== 'home' && params.id !== 'explore' && params.id !== 'notifications' && params.id !== 'messages' && params.id !== 'explore' && params.id !== 'search', }, { title: '列表时间线', docs: 'https://docs.rsshub.app/social-media.html#twitter', source: '/:id/lists/:name', target: '/twitter/list/:id/:name', - verification: (params) => params.id !== 'home' && params.id !== 'explore' && params.id !== 'notifications' && params.id !== 'messages' && params.id !== 'explore', + verification: (params) => params.id !== 'home' && params.id !== 'explore' && params.id !== 'notifications' && params.id !== 'messages' && params.id !== 'explore' && params.id !== 'search', + }, + { + title: '关键词', + docs: 'https://docs.rsshub.app/social-media.html#twitter', + source: '/search', + target: (params, url) => `/twitter/keyword/${new URL(url).searchParams.get('q')}`, }, ], }, diff --git a/docs/en/social-media.md b/docs/en/social-media.md index 0b286fbbe7d4c4..5b2090ee31c147 100644 --- a/docs/en/social-media.md +++ b/docs/en/social-media.md @@ -88,6 +88,10 @@ This route requires Twitter token's corresponding id, therefore it's only availb +### Keyword + + + ## Youtube ### User diff --git a/docs/social-media.md b/docs/social-media.md index 4eebb2df88cd0e..bd9b0314cd27f3 100644 --- a/docs/social-media.md +++ b/docs/social-media.md @@ -380,6 +380,10 @@ Tiny Tiny RSS 会给所有 iframe 元素添加 `sandbox="allow-scripts"` 属性 +### 关键词 + + + ## VueVlog ### 用户 diff --git a/lib/router.js b/lib/router.js index b03de77b90cb58..0cfa5ea683b6e4 100644 --- a/lib/router.js +++ b/lib/router.js @@ -196,6 +196,7 @@ router.get('/twitter/user/:id', require('./routes/twitter/user')); router.get('/twitter/list/:id/:name', require('./routes/twitter/list')); router.get('/twitter/likes/:id', require('./routes/twitter/likes')); router.get('/twitter/followings/:id', require('./routes/twitter/followings')); +router.get('/twitter/keyword/:keyword', require('./routes/twitter/keyword')); // Instagram router.get('/instagram/user/:id', require('./routes/instagram/user')); diff --git a/lib/routes/twitter/keyword.js b/lib/routes/twitter/keyword.js new file mode 100644 index 00000000000000..cdc28952d19ddd --- /dev/null +++ b/lib/routes/twitter/keyword.js @@ -0,0 +1,24 @@ +const utils = require('./utils'); +const config = require('@/config').value; + +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 relevant config'; + } + const keyword = ctx.params.keyword; + const result = await utils.getTwit().get('search/tweets', { + q: encodeURIComponent(keyword), + count: 50, + tweet_mode: 'extended', + result_type: 'recent', + }); + const data = result.data; + + ctx.state.data = { + title: `Twitter keyWOrd - ${keyword}`, + link: `https://twitter.com/search?q=${encodeURIComponent(keyword)}`, + item: utils.ProcessFeed({ + data: data.statuses, + }), + }; +};