feat: v3 docs
This commit is contained in:
@@ -6,7 +6,7 @@ sidebar_position: 1
|
||||
|
||||
This guide is intended for advanced users who want to know how to create an RSS feed in detail. If you're new to creating RSS feeds, we recommend reading [Create Your Own RSSHub Route](/joinus/new-rss/start-code) first.
|
||||
|
||||
Once you have collected the data you want to include in your RSS feed, you can pass it to `ctx.state.data`. RSSHub's middleware [`template.js`](https://github.com/DIYgod/RSSHub/blob/master/lib/middleware/template.js) will then process the data and render the RSS output in the required format (which is RSS 2.0 by default). In addition to the fields mentioned in [Create your own RSSHub route](/joinus/new-rss/start-code), you can customize your RSS feed further using the following fields.
|
||||
Once you have collected the data you want to include in your RSS feed, you can pass it to `ctx.set('data', obj)`. RSSHub's middleware [`template.js`](https://github.com/DIYgod/RSSHub/blob/master/lib/middleware/template.js) will then process the data and render the RSS output in the required format (which is RSS 2.0 by default). In addition to the fields mentioned in [Create your own RSSHub route](/joinus/new-rss/start-code), you can customize your RSS feed further using the following fields.
|
||||
|
||||
It's important to note that not all fields are applicable to all output formats since RSSHub supports multiple output formats. The table below shows which fields are compatible with different output formats. We use the following symbols to denote compatibility: `A` for Atom, `J` for JSON Feed, `R` for RSS 2.0.
|
||||
|
||||
@@ -72,7 +72,7 @@ RSSHub allows you to create BitTorrent/Magnet feeds, which can be useful for tri
|
||||
Here's an example of how to create a BitTorrent/Magnet feed:
|
||||
|
||||
```js
|
||||
ctx.state.data = {
|
||||
ctx.set('data', {
|
||||
item: [
|
||||
{
|
||||
enclosure_url: '', // This should be the Magnet URI
|
||||
@@ -80,7 +80,7 @@ ctx.state.data = {
|
||||
enclosure_type: 'application/x-bittorrent', // This field should be fixed to 'application/x-bittorrent'
|
||||
},
|
||||
],
|
||||
};
|
||||
});
|
||||
```
|
||||
|
||||
By including these fields in your RSS feed, you'll be able to create BitTorrent/Magnet feeds that can be automatically downloaded by compatible downloaders.
|
||||
@@ -100,13 +100,13 @@ By setting the `supportBT` attribute to `"1"`, you'll be able to update your doc
|
||||
RSSHub supports creating journal feeds that can replace `item.link` with a Sci-hub link if users provide the [common parameter](/parameter#sci-hub-link) `scihub`. To create a journal feed, you'll need to include an **additional** field in your RSS feed:
|
||||
|
||||
```js
|
||||
ctx.state.data = {
|
||||
ctx.set('data', {
|
||||
item: [
|
||||
{
|
||||
doi: '', // This should be the DOI of the item (e.g., '10.47366/sabia.v5n1a3')
|
||||
},
|
||||
],
|
||||
};
|
||||
});
|
||||
```
|
||||
|
||||
By including this `doi` field in your RSS feed, you'll be able to create journal feeds that are compatible with RSSHub's Sci-hub functionality.
|
||||
@@ -126,7 +126,7 @@ By setting the `supportSciHub` attribute to `"1"`, the documentation for your ro
|
||||
RSSHub supports creating podcast feeds that can be used with many podcast players' subscription formats. To create a podcast feed, you'll need to include several **additional** fields in your RSS feed:
|
||||
|
||||
```js
|
||||
ctx.state.data = {
|
||||
ctx.set('data', {
|
||||
itunes_author: '', // This field is **required** and should specify the podcast author's name
|
||||
itunes_category: '', // This field specifies the channel category
|
||||
image: '', // This field specifies the channel's cover image or album art
|
||||
@@ -139,7 +139,7 @@ ctx.state.data = {
|
||||
enclosure_type: '', // This field specifies the MIME type of the audio file (common types are 'audio/mpeg' for .mp3, 'audio/x-m4a' for .m4a, and 'video/mp4' for .mp4)
|
||||
},
|
||||
],
|
||||
};
|
||||
});
|
||||
```
|
||||
|
||||
By including these fields in your RSS feed, you'll be able to create podcast feeds that are compatible with many podcast players.
|
||||
@@ -168,7 +168,7 @@ RSSHub supports creating [Media RSS](https://www.rssboard.org/media-rss) feeds t
|
||||
Here's an example of how to create a [Media RSS](https://www.rssboard.org/media-rss) feed:
|
||||
|
||||
```js
|
||||
ctx.state.data = {
|
||||
ctx.set('data', {
|
||||
item: [
|
||||
{
|
||||
media: {
|
||||
@@ -185,7 +185,7 @@ ctx.state.data = {
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
});
|
||||
```
|
||||
|
||||
By including these fields in your RSS feed, you'll be able to create [Media RSS](https://www.rssboard.org/media-rss) feeds that are compatible with many [Media RSS](https://www.rssboard.org/media-rss) software subscription formats.
|
||||
@@ -197,7 +197,7 @@ RSSHub supports creating Atom feeds that include interactions like upvotes, down
|
||||
Here's an example of how to create an Atom feed with interactions:
|
||||
|
||||
```js
|
||||
ctx.state.data = {
|
||||
ctx.set('data', {
|
||||
item: [
|
||||
{
|
||||
upvotes: 0, // This should be the number of upvotes for this item
|
||||
@@ -205,7 +205,7 @@ ctx.state.data = {
|
||||
comments: 0, // This should be the number of comments for this item
|
||||
},
|
||||
],
|
||||
};
|
||||
});
|
||||
```
|
||||
|
||||
By including these fields in your Atom feed, you'll be able to create Atom feeds with interactions that are compatible with many Atom feed readers.
|
||||
|
||||
@@ -8,28 +8,28 @@ When debugging your code, you can use more than just `console.log` or attaching
|
||||
|
||||
Note: The following methods are only effective when the instance is running with `debugInfo=true`.
|
||||
|
||||
## Using `ctx.state.json`
|
||||
## Using `ctx.set('json', obj)`
|
||||
|
||||
To pass a custom object to ctx.state.json for debugging, follow these steps:
|
||||
To pass a custom object to `ctx.set('json', obj)` for debugging, follow these steps:
|
||||
|
||||
1. Create your custom object.
|
||||
2. Assign your object to `ctx.state.json`.
|
||||
2. Pass your object to `ctx.set('json', obj)`.
|
||||
3. Access the corresponding route + `.debug.json` to view your object. For example, if you want to debug the route `/furstar/characters/:lang?`, you can access the URL: `/furstar/characters/en.debug.json`
|
||||
|
||||
Here's an example of how to use `ctx.state.json` taken from [furstar/index.js](https://github.com/DIYgod/RSSHub/blob/master/lib/v2/furstar/index.js)
|
||||
Here's an example of how to use `ctx.set('json', obj)` taken from [furstar/index.js](https://github.com/DIYgod/RSSHub/blob/master/lib/v2/furstar/index.js)
|
||||
|
||||
```js
|
||||
const info = utils.fetchAllCharacters(res.data, base);
|
||||
|
||||
ctx.state.json = {
|
||||
ctx.set('json', {
|
||||
info,
|
||||
};
|
||||
});
|
||||
```
|
||||
|
||||
In the example above, we're passing the `info` object to `ctx.state.json`, which we can then access using the corresponding route + `.debug.json`.
|
||||
In the example above, we're passing the `info` object to `ctx.set('json', obj)`, which we can then access using the corresponding route + `.debug.json`.
|
||||
|
||||
## debug.html
|
||||
|
||||
In order to quickly test if the `description` in `ctx.state.data` is correct, you can use the `.debug.html` file suffix to obtain the HTML of the corresponding entry. The link can be directly opened in the browser to preview the rendering result.
|
||||
In order to quickly test if the `description` in `ctx.set('data', obj)` is correct, you can use the `.debug.html` file suffix to obtain the HTML of the corresponding entry. The link can be directly opened in the browser to preview the rendering result.
|
||||
|
||||
Usage: Access the corresponding route + `.{index}.debug.html`, where `{index}` is the item number (starting from 0) in your `ctx.state.data.item`. And the data corresponds to the `ctx.state.data.item[index].description` information will be returned as route result.
|
||||
Usage: Access the corresponding route + `.{index}.debug.html`, where `{index}` is the item number (starting from 0) in your `data.item`. And the data corresponds to the `data.item[index].description` information will be returned as route result.
|
||||
|
||||
@@ -29,7 +29,7 @@ We recommend using [day.js](https://github.com/iamkun/dayjs) for date processing
|
||||
The RSSHub utility class includes a wrapper for [day.js](https://github.com/iamkun/dayjs) that allows you to easily parse date strings and obtain a [Date Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Date) in most cases.
|
||||
|
||||
```js
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
|
||||
const pubDate = parseDate('2020/12/30');
|
||||
// OR
|
||||
@@ -45,7 +45,7 @@ You can refer to the [day.js documentation](https://day.js.org/docs/en/parse/str
|
||||
If you need to parse a relative date, use `parseRelativeDate`.
|
||||
|
||||
```js
|
||||
const { parseRelativeDate } = require('@/utils/parse-date');
|
||||
import { parseRelativeDate } from '@/utils/parse-date';
|
||||
|
||||
const pubDate = parseRelativeDate('2 days ago');
|
||||
const pubDate = parseRelativeDate('day before yesterday 15:36');
|
||||
@@ -58,7 +58,7 @@ When parsing dates from websites, it's important to consider time zones. Some we
|
||||
To manually specify the time zone in your code, use the following code:
|
||||
|
||||
```js
|
||||
const timezone = require('@/utils/timezone');
|
||||
import timezone from '@/utils/timezone';
|
||||
|
||||
const pubDate = timezone(parseDate('2020/12/30 13:00'), +1);
|
||||
```
|
||||
|
||||
@@ -100,20 +100,20 @@ RSSHub appends the name of all route namespace folders in front of the actual ro
|
||||
|
||||
### Registering a Route
|
||||
|
||||
To register a route, the `router.js` file should export a method that provides a `@koa/router` object when initializing the route.
|
||||
To register a route, the `router.js` file should export a method that provides a Hoho route handler.
|
||||
|
||||
### Maintainer List
|
||||
|
||||
The `maintainer.js` file should export an object that provides maintainer information related to the route, including:
|
||||
|
||||
- Key: Corresponding path in the `@koa/router` object
|
||||
- Key: Corresponding route path
|
||||
- Value: Array of string, including all maintainers' GitHub ID.
|
||||
|
||||
To generate a list of maintainers, use the following command: `pnpm run build:maintainer`, which will create the list under `assets/build/`.
|
||||
|
||||
:::danger
|
||||
|
||||
The path in the `@koa/router` object should be the same as the `path` in the corresponding documentation before the namespace appended in front of it.
|
||||
The path should be the same as the `path` in the corresponding documentation before the namespace appended in front of it.
|
||||
|
||||
:::
|
||||
|
||||
@@ -151,8 +151,8 @@ Here's an example taken from the [furstar](https://github.com/DIYgod/RSSHub/blob
|
||||
```
|
||||
|
||||
```js
|
||||
const path = require('path');
|
||||
const { art } = require('@/utils/render');
|
||||
import * as path from 'node:path';
|
||||
import { art } from '@/utils/render';
|
||||
const renderAuthor = (author) => art(path.join(__dirname, 'templates/author.art'), author);
|
||||
```
|
||||
|
||||
|
||||
@@ -4,18 +4,20 @@ sidebar_position: 3
|
||||
|
||||
# Using Cache
|
||||
|
||||
All routes have a cache that expires after a short duration. You can change how long the cache lasts by modifying the `CACHE_EXPIRE` value in the `lib/config.js` file using environment variables. However, for interfaces that have less frequently updated content, it's better to specify a longer cache expiration time using `CACHE_CONTENT_EXPIRE` instead.
|
||||
RSSHub have a cache module that expires after a short duration. You can change how long the cache lasts by modifying the `CACHE_EXPIRE` value in the `lib/config.js` file using environment variables. However, for interfaces that have less frequently updated content, it's better to specify a longer cache expiration time using `CACHE_CONTENT_EXPIRE` instead.
|
||||
|
||||
For example, to retrieve the full text of the first comment for each issue, you can make a request to `${baseUrl}/${user}/${repo}/issues/${id}`, since this data is unavailable through `${baseUrl}/${user}/${repo}/issues`. It's recommended to store this data in the cache to avoid making repeated requests to the server.
|
||||
|
||||
Here's an example of how you can use the cache to retrieve the data:
|
||||
|
||||
```js
|
||||
import cache from '@/utils/cache';
|
||||
|
||||
const items = await Promise.all(
|
||||
list.map((item) =>
|
||||
ctx.cache.tryGet(item.link, async () => {
|
||||
cache.tryGet(item.link, async () => {
|
||||
const { data: response } = await got(item.link);
|
||||
const $ = cheerio.load(response);
|
||||
const $ = load(response);
|
||||
|
||||
item.description = $('.comment-body').first().html();
|
||||
|
||||
@@ -25,7 +27,7 @@ Here's an example of how you can use the cache to retrieve the data:
|
||||
);
|
||||
```
|
||||
|
||||
The above code snippet from [Create Your Own RSSHub Route](/joinus/new-rss/start-code#better-reading-experience) shows how to use the cache to get the full text of the first comment of each issue. `ctx.cache.tryGet()` is used to determine if the data is already available within the cache. If it's not, the code retrieves the data and stores it in the cache.
|
||||
The above code snippet from [Create Your Own RSSHub Route](/joinus/new-rss/start-code#better-reading-experience) shows how to use the cache to get the full text of the first comment of each issue. `cache.tryGet()` is used to determine if the data is already available within the cache. If it's not, the code retrieves the data and stores it in the cache.
|
||||
|
||||
The object returned from the previous statement will be reused, and an extra `description` property will be added to it. The returned cache for each `item.link` will be `{ title, link, pubDate, author, category, description }`. The next time the same path is requested, this processed cache will be used instead of making a request to the server and recomputing the data.
|
||||
|
||||
@@ -35,7 +37,7 @@ Any assignments to variables that are declared outside of the `tryGet()` functio
|
||||
|
||||
```js
|
||||
let x = '1';
|
||||
const z = await ctx.cache.tryGet('cache:key', async () => {
|
||||
const z = await cache.tryGet('cache:key', async () => {
|
||||
x = '2';
|
||||
const y = '3';
|
||||
return y;
|
||||
@@ -48,7 +50,7 @@ Any assignments to variables that are declared outside of the `tryGet()` functio
|
||||
|
||||
## API
|
||||
|
||||
### ctx.cache.tryGet(key, getValueFunc [, maxAge [, refresh ]])
|
||||
### cache.tryGet(key, getValueFunc [, maxAge [, refresh ]])
|
||||
|
||||
#### Parameters
|
||||
|
||||
@@ -65,13 +67,13 @@ Any assignments to variables that are declared outside of the `tryGet()` functio
|
||||
|
||||
:::tip
|
||||
|
||||
Below are advanced methods for using cache. You should use `ctx.cache.tryGet()` most of the time.
|
||||
Below are advanced methods for using cache. You should use `cache.tryGet()` most of the time.
|
||||
|
||||
Note that you need to use `JSON.parse()` when retrieving the cache using `ctx.cache.get()`.
|
||||
Note that you need to use `JSON.parse()` when retrieving the cache using `cache.get()`.
|
||||
|
||||
:::
|
||||
|
||||
### ctx.cache.get(key [, refresh ])
|
||||
### cache.get(key [, refresh ])
|
||||
|
||||
#### Parameters
|
||||
|
||||
@@ -80,7 +82,7 @@ Note that you need to use `JSON.parse()` when retrieving the cache using `ctx.ca
|
||||
| key | `string` | *(Required)* The key used to retrieve the cache. You can use `:` as a separator to create a hierarchy. |
|
||||
| refresh | `boolean` | *(Optional)* Whether to renew the cache expiration time when the cache is hit. `true` by default. |
|
||||
|
||||
### ctx.cache.set(key, value [, maxAge ])
|
||||
### cache.set(key, value [, maxAge ])
|
||||
|
||||
#### Parameters
|
||||
|
||||
|
||||
@@ -98,7 +98,7 @@ For example, if you are creating an RSS feed for [GitHub Repo Issues](/routes/pr
|
||||
```js
|
||||
module.exports = (router) => {
|
||||
// highlight-next-line
|
||||
router.get('/issue/:user/:repo?', require('./issue'));
|
||||
router.get('/issue/:user/:repo?', './issue');
|
||||
};
|
||||
```
|
||||
|
||||
@@ -108,26 +108,22 @@ module.exports = (router) => {
|
||||
```js
|
||||
module.exports = function (router) {
|
||||
// highlight-next-line
|
||||
router.get('/issue/:user/:repo?', require('./issue'));
|
||||
router.get('/issue/:user/:repo?', './issue');
|
||||
};
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
</Tabs>
|
||||
|
||||
When registering your new RSS route in `router.js`, you can define the route path and specify the corresponding function to be executed. In the code above, the `router.get()` method is used to specify the HTTP method and the path of the new RSS route. The first parameter of `router.get()` is the route path using [path-to-regexp](https://github.com/pillarjs/path-to-regexp) syntax. The second parameter is the exported function from your new RSS rule, `issue.js`. Note that you can omit the `.js` extension.
|
||||
When registering your new RSS route in `router.js`, you can define the route path and specify the corresponding function to be executed. In the code above, the `router.get()` method is used to specify the HTTP method and the path of the new RSS route. The first parameter of `router.get()` is the route path using [Hono routing](https://hono.dev/api/routing) syntax. The second parameter is the relative path of your new RSS rule, `issue.js`. Note that you can omit the `.js` extension.
|
||||
|
||||
In the example above, `issue` is an exact match, `:user` is a required parameter, and `:repo?` is an optional parameter. The `?` after `:repo` means that the parameter is optional. If the user does not enter repo, it will fall back to whatever is specified in your code (in this case, `RSSHub`).
|
||||
|
||||
Once you have defined the route path, you can retrieve the value of the parameters from the `ctx.params` object. For example, if the user visits `/github/issue/DIYgod/RSSHub`, you can get the value of `user` and `repo` from `ctx.params.user` and `ctx.params.repo`, respectively. For example, if an user visits `/github/issue/DIYgod/RSSHub`, `ctx.params.user` and `ctx.params.repo` which will be `DIYgod` and `RSSHub`.
|
||||
|
||||
**Note that the type of the value will be either `String` or `undefined`**.
|
||||
|
||||
You can use the `*` or `+` symbols to match the rest of the path, like `/some/path/:variable*`. Note that `*` and `+` mean "zero or more" and "one or more", respectively. You can also use patterns like `/some/path/:variable(\\d+)?` or even RegExp.
|
||||
Once you have defined the route path, you can retrieve the value of the parameters from the [`ctx.req.param()`](https://hono.dev/api/request#param) function. For example, if the user visits `/github/issue/DIYgod/RSSHub`, you can get the value of `user` and `repo` from `ctx.req.param('user')` and `ctx.req.param('repo')`, respectively. For example, if an user visits `/github/issue/DIYgod/RSSHub`, `ctx.req.param('user')` and `ctx.req.param('repo')` which will be `DIYgod` and `RSSHub`.
|
||||
|
||||
:::tip
|
||||
|
||||
For more advanced usage of `router`, see the [@koa/router API Reference](https://github.com/koajs/router/blob/master/API.md).
|
||||
For more advanced usage of `router`, see the [Hono Routing API Reference](https://hono.dev/api/routing).
|
||||
|
||||
:::
|
||||
|
||||
|
||||
@@ -28,15 +28,15 @@ Here's the basic code to get you started:
|
||||
|
||||
```js
|
||||
// Import the necessary modules
|
||||
const got = require('@/utils/got'); // a customised got
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
import got from '@/utils/got'; // a customised got
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
// Your logic here
|
||||
|
||||
ctx.state.data = {
|
||||
ctx.set('data', {
|
||||
// Your RSS output here
|
||||
};
|
||||
});
|
||||
};
|
||||
```
|
||||
|
||||
@@ -53,11 +53,11 @@ As mentioned earlier, we need to retrieve the GitHub username and repository nam
|
||||
```js
|
||||
module.exports = async (ctx) => {
|
||||
// highlight-next-line
|
||||
const { user, repo = 'RSSHub' } = ctx.params;
|
||||
const { user, repo = 'RSSHub' } = ctx.req.param();
|
||||
|
||||
ctx.state.data = {
|
||||
ctx.set('data', {
|
||||
// Your RSS output here
|
||||
};
|
||||
});
|
||||
};
|
||||
```
|
||||
|
||||
@@ -67,13 +67,13 @@ module.exports = async (ctx) => {
|
||||
```js
|
||||
module.exports = async (ctx) => {
|
||||
// highlight-start
|
||||
const user = ctx.params.user;
|
||||
const repo = ctx.params.repo ?? 'RSSHub';
|
||||
const user = ctx.req.param('user');
|
||||
const repo = ctx.req.param('repo') ?? 'RSSHub';
|
||||
// highlight-end
|
||||
|
||||
ctx.state.data = {
|
||||
ctx.set('data', {
|
||||
// Your RSS output here
|
||||
};
|
||||
});
|
||||
};
|
||||
```
|
||||
|
||||
@@ -91,7 +91,7 @@ After we have the user input, we can use it to make a request to the API. In mos
|
||||
|
||||
```js
|
||||
module.exports = async (ctx) => {
|
||||
const { user, repo = 'RSSHub' } = ctx.params;
|
||||
const { user, repo = 'RSSHub' } = ctx.req.param();
|
||||
// highlight-start
|
||||
// Send an HTTP GET request to the API
|
||||
// and destruct the data object returned by the request
|
||||
@@ -104,14 +104,14 @@ module.exports = async (ctx) => {
|
||||
},
|
||||
searchParams: {
|
||||
// This allows users to set the number of feed items they want
|
||||
per_page: ctx.query.limit ? parseInt(ctx.query.limit, 10) : 30,
|
||||
per_page: ctx.req.query('limit') ? parseInt(ctx.req.query('limit'), 10) : 30,
|
||||
},
|
||||
});
|
||||
// highlight-end
|
||||
|
||||
ctx.state.data = {
|
||||
ctx.set('data', {
|
||||
// Your RSS output here
|
||||
};
|
||||
});
|
||||
};
|
||||
```
|
||||
|
||||
@@ -120,8 +120,8 @@ module.exports = async (ctx) => {
|
||||
|
||||
```js
|
||||
module.exports = async (ctx) => {
|
||||
const user = ctx.params.user;
|
||||
const repo = ctx.params.repo ?? 'RSSHub';
|
||||
const user = ctx.req.param('user');
|
||||
const repo = ctx.req.param('repo') ?? 'RSSHub';
|
||||
// highlight-start
|
||||
// Send an HTTP GET request to the API
|
||||
const response = await got(`https://api.github.com/repos/${user}/${repo}/issues`, {
|
||||
@@ -129,16 +129,16 @@ module.exports = async (ctx) => {
|
||||
accept: 'application/vnd.github.html+json',
|
||||
},
|
||||
searchParams: {
|
||||
per_page: ctx.query.limit ? parseInt(ctx.query.limit, 10) : 30,
|
||||
per_page: ctx.req.query('limit') ? parseInt(ctx.req.query('limit'), 10) : 30,
|
||||
},
|
||||
});
|
||||
// response.data is the data object returned by the above request
|
||||
const data = response.data;
|
||||
// highlight-end
|
||||
|
||||
ctx.state.data = {
|
||||
ctx.set('data', {
|
||||
// Your RSS output here
|
||||
};
|
||||
});
|
||||
};
|
||||
```
|
||||
|
||||
@@ -149,7 +149,7 @@ module.exports = async (ctx) => {
|
||||
|
||||
Once we have retrieved the data from the API, we need to process it further to generate an RSS feed that conforms to the RSS specification. Specifically, we need to extract the channel title, channel link, item title, item link, item description, and item publication date.
|
||||
|
||||
To do this, we can assign the relevant data to the `ctx.state.data` object, and RSSHub's middleware will take care of the rest.
|
||||
To do this, we can assign the relevant data to the `ctx.set('data', obj)` object, and RSSHub's middleware will take care of the rest.
|
||||
|
||||
Here is the final code that you should have:
|
||||
|
||||
@@ -157,18 +157,18 @@ Here is the final code that you should have:
|
||||
<TabItem value="Final code" label="Final code" default>
|
||||
|
||||
```js
|
||||
const got = require('@/utils/got');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
import got from '@/utils/got';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const { user, repo = 'RSSHub' } = ctx.params;
|
||||
const { user, repo = 'RSSHub' } = ctx.req.param();
|
||||
|
||||
const { data } = await got(`https://api.github.com/repos/${user}/${repo}/issues`, {
|
||||
headers: {
|
||||
accept: 'application/vnd.github.html+json',
|
||||
},
|
||||
searchParams: {
|
||||
per_page: ctx.query.limit ? parseInt(ctx.query.limit, 10) : 30,
|
||||
per_page: ctx.req.query('limit') ? parseInt(ctx.req.query('limit'), 10) : 30,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -191,14 +191,14 @@ module.exports = async (ctx) => {
|
||||
// highlight-end
|
||||
|
||||
// highlight-start
|
||||
ctx.state.data = {
|
||||
ctx.set('data', {
|
||||
// channel title
|
||||
title: `${user}/${repo} issues`,
|
||||
// channel link
|
||||
link: `https://github.com/${user}/${repo}/issues`,
|
||||
// each feed item
|
||||
item: items,
|
||||
};
|
||||
});
|
||||
// highlight-end
|
||||
};
|
||||
```
|
||||
@@ -207,23 +207,23 @@ module.exports = async (ctx) => {
|
||||
<TabItem value="Alternative" label="Alternative">
|
||||
|
||||
```js
|
||||
const got = require('@/utils/got');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
import got from '@/utils/got';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const { user, repo = 'RSSHub' } = ctx.params;
|
||||
const { user, repo = 'RSSHub' } = ctx.req.param();
|
||||
|
||||
const { data } = await got(`https://api.github.com/repos/${user}/${repo}/issues`, {
|
||||
headers: {
|
||||
accept: 'application/vnd.github.html+json',
|
||||
},
|
||||
searchParams: {
|
||||
per_page: ctx.query.limit ? parseInt(ctx.query.limit, 10) : 30,
|
||||
per_page: ctx.req.query('limit') ? parseInt(ctx.req.query('limit'), 10) : 30,
|
||||
},
|
||||
});
|
||||
|
||||
// highlight-start
|
||||
ctx.state.data = {
|
||||
ctx.set('data', {
|
||||
// channel title
|
||||
title: `${user}/${repo} issues`,
|
||||
// channel link
|
||||
@@ -243,7 +243,7 @@ module.exports = async (ctx) => {
|
||||
// item category, if available
|
||||
category: item.labels.map((label) => label.name),
|
||||
}));
|
||||
};
|
||||
});
|
||||
// highlight-end
|
||||
};
|
||||
```
|
||||
@@ -261,16 +261,16 @@ Here's the basic code to get you started:
|
||||
|
||||
```js
|
||||
// Require necessary modules
|
||||
const got = require('@/utils/got'); // a customised got
|
||||
const cheerio = require('cheerio'); // an HTML parser with a jQuery-like API
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
import got from '@/utils/got'; // a customised got
|
||||
import { load } from 'cheerio'; // an HTML parser with a jQuery-like API
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
// Your logic here
|
||||
|
||||
ctx.state.data = {
|
||||
ctx.set('data', {
|
||||
// Your RSS output here
|
||||
};
|
||||
});
|
||||
};
|
||||
```
|
||||
|
||||
@@ -286,12 +286,12 @@ As mentioned before, we want users to enter a GitHub username and a repository n
|
||||
module.exports = async (ctx) => {
|
||||
// highlight-start
|
||||
// Retrieve user and repository name from the URL parameters
|
||||
const { user, repo = 'RSSHub' } = ctx.params;
|
||||
const { user, repo = 'RSSHub' } = ctx.req.param();
|
||||
// highlight-end
|
||||
|
||||
ctx.state.data = {
|
||||
ctx.set('data', {
|
||||
// Your RSS output here
|
||||
};
|
||||
});
|
||||
};
|
||||
```
|
||||
|
||||
@@ -305,12 +305,12 @@ To begin, we'll make an HTTP GET request to the API and load the HTML response i
|
||||
|
||||
```js
|
||||
const baseUrl = 'https://github.com';
|
||||
const { user, repo = 'RSSHub' } = ctx.params;
|
||||
const { user, repo = 'RSSHub' } = ctx.req.param();
|
||||
|
||||
// Note that the ".data" property contains the full HTML source of the target page returned by the request
|
||||
// highlight-start
|
||||
const { data: response } = await got(`${baseUrl}/${user}/${repo}/issues`);
|
||||
const $ = cheerio.load(response);
|
||||
const $ = load(response);
|
||||
// highlight-end
|
||||
```
|
||||
|
||||
@@ -341,30 +341,30 @@ Next, we'll use Cheerio selectors to select the relevant HTML elements, parse th
|
||||
});
|
||||
// highlight-end
|
||||
|
||||
ctx.state.data = {
|
||||
ctx.set('data', {
|
||||
// Your RSS output here
|
||||
};
|
||||
});
|
||||
```
|
||||
|
||||
### Outputting the RSS
|
||||
|
||||
Once we have the data from the web page, we need to further process it to generate RSS in accordance with the RSS specification. Mainly, we need the channel title, channel link, item title, item link, item description, and item publication date.
|
||||
|
||||
Assign them to the `ctx.state.data` object, and RSSHub's middleware will take care of the rest.
|
||||
Pass them to the `ctx.set('data', obj)` object, and RSSHub's middleware will take care of the rest.
|
||||
|
||||
Here's an example code:
|
||||
|
||||
```js
|
||||
const got = require('@/utils/got');
|
||||
const cheerio = require('cheerio');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
import got from '@/utils/got';
|
||||
import { load } from 'cheerio';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const baseUrl = 'https://github.com';
|
||||
const { user, repo = 'RSSHub' } = ctx.params;
|
||||
const { user, repo = 'RSSHub' } = ctx.req.param();
|
||||
|
||||
const { data: response } = await got(`${baseUrl}/${user}/${repo}/issues`);
|
||||
const $ = cheerio.load(response);
|
||||
const $ = load(response);
|
||||
|
||||
const items = $('div.js-navigation-container .flex-auto')
|
||||
.toArray()
|
||||
@@ -384,14 +384,14 @@ module.exports = async (ctx) => {
|
||||
});
|
||||
|
||||
// highlight-start
|
||||
ctx.state.data = {
|
||||
ctx.set('data', {
|
||||
// channel title
|
||||
title: `${user}/${repo} issues`,
|
||||
// channel link
|
||||
link: `${baseUrl}/${user}/${repo}/issues`,
|
||||
// each feed item
|
||||
item: items,
|
||||
};
|
||||
});
|
||||
// highlight-end
|
||||
};
|
||||
```
|
||||
@@ -403,16 +403,16 @@ The previous code provides only part of the information for each feed item. To p
|
||||
Here's the updated code:
|
||||
|
||||
```js
|
||||
const got = require('@/utils/got');
|
||||
const cheerio = require('cheerio');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
import got from '@/utils/got';
|
||||
import { load } from 'cheerio';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const baseUrl = 'https://github.com';
|
||||
const { user, repo = 'RSSHub' } = ctx.params;
|
||||
const { user, repo = 'RSSHub' } = ctx.req.param();
|
||||
|
||||
const { data: response } = await got(`${baseUrl}/${user}/${repo}/issues`);
|
||||
const $ = cheerio.load(response);
|
||||
const $ = load(response);
|
||||
|
||||
// highlight-next-line
|
||||
const list = $('div.js-navigation-container .flex-auto')
|
||||
@@ -435,9 +435,9 @@ module.exports = async (ctx) => {
|
||||
// highlight-start
|
||||
const items = await Promise.all(
|
||||
list.map((item) =>
|
||||
ctx.cache.tryGet(item.link, async () => {
|
||||
cache.tryGet(item.link, async () => {
|
||||
const { data: response } = await got(item.link);
|
||||
const $ = cheerio.load(response);
|
||||
const $ = load(response);
|
||||
|
||||
// Select the first element with the class name 'comment-body'
|
||||
item.description = $('.comment-body').first().html();
|
||||
@@ -450,12 +450,12 @@ module.exports = async (ctx) => {
|
||||
);
|
||||
// highlight-end
|
||||
|
||||
ctx.state.data = {
|
||||
ctx.set('data', {
|
||||
title: `${user}/${repo} issues`,
|
||||
link: `https://github.com/${user}/${repo}/issues`,
|
||||
// highlight-next-line
|
||||
item: items,
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
```
|
||||
@@ -486,10 +486,10 @@ Here's some basic code to get you started:
|
||||
|
||||
```js
|
||||
// Import necessary modules
|
||||
const buildData = require('@/utils/common-config');
|
||||
import buildData from '@/utils/common-config';
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
ctx.state.data = await buildData({
|
||||
ctx.set('data', await buildData({
|
||||
link: '', // The RSS source link
|
||||
url: '', // The data source link
|
||||
// Variables can be used here, such as %xxx% will be parsed into
|
||||
@@ -498,20 +498,20 @@ module.exports = async (ctx) => {
|
||||
params: {
|
||||
title: '', // Additional title
|
||||
},
|
||||
});
|
||||
}));
|
||||
};
|
||||
```
|
||||
|
||||
Our RSS feed currently lacks content. The `item` must be set to add the content. Here's an example:
|
||||
|
||||
```js
|
||||
const buildData = require('@/utils/common-config');
|
||||
import buildData from '@/utils/common-config';
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const { user, repo = 'RSSHub' } = ctx.params;
|
||||
const { user, repo = 'RSSHub' } = ctx.req.param();
|
||||
const link = `https://github.com/${user}/${repo}/issues`;
|
||||
|
||||
ctx.state.data = await buildData({
|
||||
ctx.set('data', await buildData({
|
||||
link,
|
||||
url: link,
|
||||
title: `${user}/${repo} issues`, // you can also use $('head title').text()
|
||||
@@ -529,7 +529,7 @@ module.exports = async (ctx) => {
|
||||
pubDate: `parseDate($('relative-time').attr('datetime'))`,
|
||||
},
|
||||
// highlight-end
|
||||
});
|
||||
}));
|
||||
};
|
||||
```
|
||||
|
||||
@@ -540,17 +540,17 @@ You'll notice that the code is similar to the [Obtaining data from the webpage](
|
||||
To get the full article of each issue, you need to add a few more lines of code. Here is an example:
|
||||
|
||||
```js
|
||||
const buildData = require('@/utils/common-config');
|
||||
import buildData from '@/utils/common-config';
|
||||
// highlight-start
|
||||
const got = require('@/utils/got');
|
||||
const cheerio = require('cheerio');
|
||||
import got from '@/utils/got';
|
||||
import { load } from 'cheerio';
|
||||
// highlight-end
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const { user, repo = 'RSSHub' } = ctx.params;
|
||||
const { user, repo = 'RSSHub' } = ctx.req.param();
|
||||
const link = `https://github.com/${user}/${repo}/issues`;
|
||||
|
||||
ctx.state.data = await buildData({
|
||||
const data = await buildData({
|
||||
link,
|
||||
url: link,
|
||||
title: `${user}/${repo} issues`,
|
||||
@@ -568,15 +568,17 @@ module.exports = async (ctx) => {
|
||||
|
||||
// highlight-start
|
||||
await Promise.all(
|
||||
ctx.state.data.item.map((item) =>
|
||||
ctx.cache.tryGet(item.link, async () => {
|
||||
data.item.map((item) =>
|
||||
cache.tryGet(item.link, async () => {
|
||||
const { data: resonse } = await got(item.link);
|
||||
const $ = cheerio.load(resonse);
|
||||
const $ = load(resonse);
|
||||
item.description = $('.comment-body').first().html();
|
||||
return item;
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
ctx.set('data', data);
|
||||
// highlight-end
|
||||
};
|
||||
```
|
||||
@@ -593,16 +595,16 @@ To get started with puppeteer, create a new file in your code editor and save it
|
||||
|
||||
```js
|
||||
// Require some useful modules
|
||||
const cheerio = require('cheerio'); // an HTML parser with a jQuery-like API
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
const logger = require('@/utils/logger');
|
||||
import { load } from 'cheerio'; // an HTML parser with a jQuery-like API
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
import logger from '@/utils/logger';
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
// Your logic here
|
||||
|
||||
ctx.state.data = {
|
||||
ctx.set('data', {
|
||||
// Your RSS output here
|
||||
};
|
||||
});
|
||||
};
|
||||
```
|
||||
|
||||
@@ -614,17 +616,18 @@ Now, we will be using `puppeteer` instead of `got` to retrieve data from the web
|
||||
<TabItem value="puppeteer" label="puppeteer">
|
||||
|
||||
```js
|
||||
const cheerio = require('cheerio');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
const logger = require('@/utils/logger');
|
||||
import { load } from 'cheerio';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
import logger from '@/utils/logger';
|
||||
import puppeteer from '@/utils/puppeteer';
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const baseUrl = 'https://github.com';
|
||||
const { user, repo = 'RSSHub' } = ctx.params;
|
||||
const { user, repo = 'RSSHub' } = ctx.req.param();
|
||||
|
||||
// highlight-start
|
||||
// require puppeteer utility class and initialise a browser instance
|
||||
const browser = await require('@/utils/puppeteer')();
|
||||
const browser = await puppeteer();
|
||||
// open a new tab
|
||||
const page = await browser.newPage();
|
||||
// intercept all requests
|
||||
@@ -650,7 +653,7 @@ module.exports = async (ctx) => {
|
||||
page.close();
|
||||
// highlight-end
|
||||
|
||||
const $ = cheerio.load(response);
|
||||
const $ = load(response);
|
||||
|
||||
// const item = ...;
|
||||
|
||||
@@ -659,9 +662,9 @@ module.exports = async (ctx) => {
|
||||
browser.close();
|
||||
// highlight-end
|
||||
|
||||
ctx.state.data = {
|
||||
ctx.set('data', {
|
||||
// Your RSS output here
|
||||
};
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
@@ -669,21 +672,21 @@ module.exports = async (ctx) => {
|
||||
<TabItem value="got" label="got">
|
||||
|
||||
```js
|
||||
const got = require('@/utils/got');
|
||||
const cheerio = require('cheerio');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
import got from '@/utils/got';
|
||||
import { load } from 'cheerio';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const baseUrl = 'https://github.com';
|
||||
const { user, repo = 'RSSHub' } = ctx.params;
|
||||
const { user, repo = 'RSSHub' } = ctx.req.param();
|
||||
|
||||
// highlight-next-line
|
||||
const { data: response } = await got(`${baseUrl}/${user}/${repo}/issues`);
|
||||
const $ = cheerio.load(response);
|
||||
const $ = load(response);
|
||||
|
||||
ctx.state.data = {
|
||||
ctx.set('data', {
|
||||
// Your RSS output here
|
||||
};
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
@@ -695,15 +698,16 @@ module.exports = async (ctx) => {
|
||||
Retrieving the full articles of each issue using a new browser page is similar to the [previous section](#better-reading-experience). We can use the following code:
|
||||
|
||||
```js
|
||||
const cheerio = require('cheerio');
|
||||
const { parseDate } = require('@/utils/parse-date');
|
||||
const logger = require('@/utils/logger');
|
||||
import { load } from 'cheerio';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
import logger from '@/utils/logger';
|
||||
import puppeteer from '@/utils/puppeteer';
|
||||
|
||||
module.exports = async (ctx) => {
|
||||
const baseUrl = 'https://github.com';
|
||||
const { user, repo = 'RSSHub' } = ctx.params;
|
||||
const { user, repo = 'RSSHub' } = ctx.req.param();
|
||||
|
||||
const browser = await require('@/utils/puppeteer')();
|
||||
const browser = await puppeteer();
|
||||
const page = await browser.newPage();
|
||||
await page.setRequestInterception(true);
|
||||
page.on('request', (request) => {
|
||||
@@ -718,7 +722,7 @@ module.exports = async (ctx) => {
|
||||
const response = await page.content();
|
||||
page.close();
|
||||
|
||||
const $ = cheerio.load(response);
|
||||
const $ = load(response);
|
||||
|
||||
const list = $('div.js-navigation-container .flex-auto')
|
||||
.toArray()
|
||||
@@ -739,7 +743,7 @@ module.exports = async (ctx) => {
|
||||
|
||||
const items = await Promise.all(
|
||||
list.map((item) =>
|
||||
ctx.cache.tryGet(item.link, async () => {
|
||||
cache.tryGet(item.link, async () => {
|
||||
// highlight-start
|
||||
// reuse the browser instance and open a new tab
|
||||
const page = await browser.newPage();
|
||||
@@ -758,7 +762,7 @@ module.exports = async (ctx) => {
|
||||
page.close();
|
||||
// highlight-end
|
||||
|
||||
const $ = cheerio.load(response);
|
||||
const $ = load(response);
|
||||
|
||||
item.description = $('.comment-body').first().html();
|
||||
|
||||
@@ -772,11 +776,11 @@ module.exports = async (ctx) => {
|
||||
browser.close();
|
||||
// highlight-end
|
||||
|
||||
ctx.state.data = {
|
||||
ctx.set('data', {
|
||||
title: `${user}/${repo} issues`,
|
||||
link: `https://github.com/${user}/${repo}/issues`,
|
||||
item: items,
|
||||
};
|
||||
});
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
@@ -149,7 +149,7 @@ E.g.
|
||||
|
||||
### debug.json
|
||||
|
||||
If the RSSHub instance is running with `debugInfo=true` enabled, suffixing a route with `.debug.json` will result in the value of `ctx.state.json` being returned.
|
||||
If the RSSHub instance is running with `debugInfo=true` enabled, suffixing a route with `.debug.json` will result in the value of `ctx.set('json', obj)` being returned.
|
||||
|
||||
This feature aims to facilitate debugging or developing customized features. A route developer has the freedom to determine whether to adopt it or not, without any format requirement.
|
||||
|
||||
@@ -159,7 +159,7 @@ For example:
|
||||
|
||||
### debug.html
|
||||
|
||||
By adding `.{index}.debug.html` (where `{index}` is a number starting from 0) at the end of the route and running the instance with `debugInfo=true`, RSSHub will return the content set in the plugin's `ctx.state.data.item[index].description`. You can access this page with a browser to quickly view the extracted information.
|
||||
By adding `.{index}.debug.html` (where `{index}` is a number starting from 0) at the end of the route and running the instance with `debugInfo=true`, RSSHub will return the content set in the plugin's `data.item[index].description`. You can access this page with a browser to quickly view the extracted information.
|
||||
|
||||
Example:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user