feat(route): Start on support for generic discourse forums. (#13063)

* Start on support for generic discourse forums.

* Add doc.

* Update doc.

* Add exception.

* Update doc.

* Update bbs.md

* test: add discourse config

* docs: fix typo

* fix: guard condition

---------
This commit is contained in:
Andvari
2023-08-23 21:19:45 +08:00
committed by GitHub
parent 8e4f814ea6
commit 657c67ec37
8 changed files with 74 additions and 1 deletions

View File

@@ -10,6 +10,7 @@ const calculateValue = () => {
const email_config = {}; const email_config = {};
const discuz_cookies = {}; const discuz_cookies = {};
const medium_cookies = {}; const medium_cookies = {};
const discourse_config = {};
for (const name in envs) { for (const name in envs) {
if (name.startsWith('BILIBILI_COOKIE_')) { if (name.startsWith('BILIBILI_COOKIE_')) {
@@ -27,6 +28,9 @@ const calculateValue = () => {
} else if (name.startsWith('MEDIUM_COOKIE_')) { } else if (name.startsWith('MEDIUM_COOKIE_')) {
const username = name.slice(14).toLowerCase(); const username = name.slice(14).toLowerCase();
medium_cookies[username] = envs[name]; medium_cookies[username] = envs[name];
} else if (name.startsWith('DISCOURSE_CONFIG_')) {
const id = name.slice('DISCOURSE_CONFIG_'.length);
discourse_config[id] = JSON.parse(envs[name]);
} }
} }
@@ -136,6 +140,9 @@ const calculateValue = () => {
discord: { discord: {
authorization: envs.DISCORD_AUTHORIZATION, authorization: envs.DISCORD_AUTHORIZATION,
}, },
discourse: {
config: discourse_config,
},
discuz: { discuz: {
cookies: discuz_cookies, cookies: discuz_cookies,
}, },

View File

@@ -3,7 +3,8 @@ const fs = require('fs');
const toSource = require('tosource'); const toSource = require('tosource');
const { join } = require('path'); const { join } = require('path');
const allowNamespace = ['ehentai', 'test']; // Namespaces that do not require radar.js
const allowNamespace = ['discourse', 'ehentai', 'test'];
// Check if a radar.js file is exist under each folder of dirname // Check if a radar.js file is exist under each folder of dirname
for (const dir of fs.readdirSync(dirname)) { for (const dir of fs.readdirSync(dirname)) {
const dirPath = join(dirname, dir); const dirPath = join(dirname, dir);

View File

@@ -0,0 +1,3 @@
module.exports = {
'/:configId/posts': ['dzx-dzx'],
};

28
lib/v2/discourse/posts.js Normal file
View File

@@ -0,0 +1,28 @@
const config = require('@/config').value;
const got = require('@/utils/got');
const RSSParser = require('@/utils/rss-parser');
module.exports = async (ctx) => {
if (!config.discourse.config[ctx.params.configId]) {
throw Error('Discourse RSS is disabled due to the lack of <a href="https://docs.rsshub.app/install">relevant config</a>');
}
const { link, key } = config.discourse.config[ctx.params.configId];
const feed = await RSSParser.parseString(
(
await got(`${link}/posts.rss`, {
headers: {
'User-Api-Key': key,
},
})
).data
);
feed.items = feed.items.map((e) => ({
description: e.content,
author: e.creator,
...e,
}));
ctx.state.data = { item: feed.items, ...feed };
};

View File

@@ -0,0 +1,3 @@
module.exports = (router) => {
router.get('/:configId/posts', require('./posts'));
};

View File

@@ -73,6 +73,20 @@ describe('config', () => {
delete process.env.MEDIUM_COOKIE_34; delete process.env.MEDIUM_COOKIE_34;
}); });
it('discourse config', () => {
process.env.DISCOURSE_CONFIG_12 = JSON.stringify({ a: 1 });
process.env.DISCOURSE_CONFIG_34 = JSON.stringify({ b: 2 });
const config = require('../lib/config').value;
expect(config.discourse.config).toMatchObject({
12: { a: 1 },
34: { b: 2 },
});
delete process.env.DISCOURSE_CONFIG_12;
delete process.env.DISCOURSE_CONFIG_34;
});
it('no random ua', () => { it('no random ua', () => {
process.env.NO_RANDOM_UA = true; process.env.NO_RANDOM_UA = true;

View File

@@ -792,6 +792,11 @@ See docs of the specified route and `lib/config.js` for detailed information.
- `DISCORD_AUTHORIZATION`: Discord authorization token, can be found in the header of XHR requests after logging in Discord web client - `DISCORD_AUTHORIZATION`: Discord authorization token, can be found in the header of XHR requests after logging in Discord web client
- Discourse
- `DISCOURSE_CONFIG_{id}`: `id` could be arbitrary number or string, while the value should be the format of `{"link":link,"key":key}`, where:
- `link` is the link to the forum.
- `key` is the access key for the forum API, which you can refer to [this snippet](https://pastebin.com/YbLCgdWW) to obtain one. Ensure that this key is granted sufficient permission.
- Discuz cookie - Discuz cookie
- `DISCUZ_COOKIE_{cid}`: Cookie of a forum powered by Discuz, cid can be anything from 00 to 99. When visiting a Discuz route, use cid to specify this cookie. - `DISCUZ_COOKIE_{cid}`: Cookie of a forum powered by Discuz, cid can be anything from 00 to 99. When visiting a Discuz route, use cid to specify this cookie.

View File

@@ -140,6 +140,18 @@
<Route author="HenryQW" example="/dcard/funny/popular" path="/dcard/:section/:type?" paramsDesc={['板塊名稱URL 中獲得', '排序popular 熱門latest 最新,默認為 latest']} radar="1" rssbud="1" anticrawler="1" puppeteer="1"/> <Route author="HenryQW" example="/dcard/funny/popular" path="/dcard/:section/:type?" paramsDesc={['板塊名稱URL 中獲得', '排序popular 熱門latest 最新,默認為 latest']} radar="1" rssbud="1" anticrawler="1" puppeteer="1"/>
## Discourse {#discourse}
:::caution
You need to set the environment variable `DISCOURSE_CONFIG_{id}` before using it. Please refer to Configuration section in the Deploy page of the documentation.
:::
### Latest posts {#discourse-latest-posts}
<Route author="dzx-dzx" example="/discourse/0/posts" path="/discuz/:configId/posts" paramsDesc={['Environment variable configuration id, see above']} selfhost="1"/>
## Discuz {#discuz} ## Discuz {#discuz}
### General Subforum - Auto detection {#discuz-general-subforum---auto-detection} ### General Subforum - Auto detection {#discuz-general-subforum---auto-detection}