-
|
I am trying to create a testing route using My testing route looks like this: diff --git a/lib/routes/example/latest.ts b/lib/routes/example/latest.ts
new file mode 100644
index 000000000..b1f0f3801
--- /dev/null
+++ b/lib/routes/example/latest.ts
@@ -0,0 +1,72 @@
+import { load } from 'cheerio';
+
+import type { Route } from '@/types';
+import cache from '@/utils/cache';
+import got from '@/utils/got';
+import ofetch from '@/utils/ofetch';
+import { parseDate } from '@/utils/parse-date';
+
+const currentURL = 'https://example.com';
+
+export const route: Route = {
+ path: '/latest',
+ categories: ['new-media'],
+ example: '/example/latest',
+ parameters: { },
+ features: {
+ requireConfig: false,
+ requirePuppeteer: false,
+ antiCrawler: false,
+ supportBT: false,
+ supportPodcast: false,
+ supportScihub: false,
+ },
+ radar: [
+ {
+ source: [],
+ },
+ ],
+ name: 'example testing',
+ maintainers: ['the_user'],
+ handler,
+ url: 'example.com',
+ description: '',
+};
+
+async function handler(ctx) {
+ // const { category = '', id = '' } = ctx.req.param();
+ // const limit = Number.parseInt(ctx.req.query('limit') || '20', 10);
+ // const hasTopicId = id && Object.hasOwn(categories, category);
+
+ const response = await got(currentURL);
+
+ const $ = load(response);
+
+ // We use a Cheerio selector to select all 'li' elements with a class name that starts with 'ListItem-module__listItem__' that
+ // are within a 'ul' element with a class name which starts with 'ListView-module__ul__'.
+ const items = $('div')
+ // We use the `toArray()` method to retrieve all the DOM elements selected as an array.
+ .toArray()
+ // We use the `map()` method to traverse the array and parse the data we need from each element.
+ .map((item) => {
+ const $item = $(item);
+ // We use `first()` to select the first 'a' element within the current item
+ // as there are multiple 'a' elements in each item, and we need to specify which one we want to use.
+ const h = $item.find('h1').first();
+ const p = $item.find('p').first();
+ return {
+ title: h.text(),
+ // We need an absolute URL for `link`, but `a.attr('href')` returns a relative URL.
+ link: `${rootUrl}`,
+ pubDate: parseDate('1970-01-02','x'),
+ author: 'the_user',
+ category: 'hi',
+ };
+ });
+
+ return {
+ title: 'what the',
+ link: currentURL,
+ item: items,
+ };
+}
diff --git a/lib/routes/example/namespace.ts b/lib/routes/example/namespace.ts
new file mode 100644
index 000000000..aecafb538
--- /dev/null
+++ b/lib/routes/example/namespace.ts
@@ -0,0 +1,7 @@
+import type { Namespace } from '@/types';
+
+export const namespace: Namespace = {
+ name: 'Example',
+ url: 'example.com',
+ lang: 'zh-TW',
+};
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Hi @affhp! I'm Dosu and I'm helping the RSSNext team. The issue is on this line in your handler: const response = await got(currentURL);
const $ = load(response);In RSSHub, const response = await got(currentURL);
const $ = load(response.data); // ← use response.dataBecause you're passing the entire response object to There's also a secondary bug — you reference Here's the corrected handler core: async function handler(ctx) {
const response = await got(currentURL);
const $ = load(response.data);
const items = $('div')
.toArray()
.map((item) => {
const $item = $(item);
const h = $item.find('h1').first();
const p = $item.find('p').first();
return {
title: h.text(),
link: currentURL, // was `rootUrl` (undefined)
pubDate: parseDate('1970-01-02'),
author: 'the_user',
category: 'hi',
};
});
return {
title: 'what the',
link: currentURL,
item: items,
};
}Note that To reply, just mention @dosu. Share context across your team and agents. Try Dosu. |
Beta Was this translation helpful? Give feedback.
Hi @affhp! I'm Dosu and I'm helping the RSSNext team.
The issue is on this line in your handler:
In RSSHub,
got()returns a response object, not the HTML body directly. You need to useresponse.datato access the HTML content [1]:Because you're passing the entire response object to
load(), Cheerio can't parse it properly, your selectors find nothing, and theitemsarray ends up empty. RSSHub's middleware then throws the "this route is empty" error wheneverdata.itemis empty or undefined [2].There's also a secondary bug — you r…