diff --git a/docs/README.md b/docs/README.md index c9dcce235..5fd1814a0 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1656,3 +1656,17 @@ id, 专辑 id, 可在对应专辑页面的 URL 中找到 路由: `/namoc/announcement` 参数:无 + +## Greasy Fork + +### 脚本更新 + +举例: [https://rsshub.app/greasyfork/zh-CN/bilibili.com](https://rsshub.app/greasyfork/zh-CN/bilibili.com) + +路由: `/greasyfork/:language/:domain?` + +参数: + +language,语言,可在网站右上角找到, `all` 为所有语言 + +domain,按脚本生效域名过滤,可选 diff --git a/router.js b/router.js index 165c34d7f..408468d7b 100755 --- a/router.js +++ b/router.js @@ -381,4 +381,7 @@ router.get('/dongqiudi/daily', require('./routes/dongqiudi/index')); // 维基百科 router.get('/wikipedia/mainland', require('./routes/wikipedia/mainland')); +// Greasy Fork +router.get('/greasyfork/:language/:domain?', require('./routes/greasyfork/scripts')); + module.exports = router; diff --git a/routes/greasyfork/scripts.js b/routes/greasyfork/scripts.js new file mode 100644 index 000000000..d3fefd35c --- /dev/null +++ b/routes/greasyfork/scripts.js @@ -0,0 +1,45 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); +const config = require('../../config'); + +module.exports = async (ctx) => { + const language = ctx.params.language === 'all' ? 'zh-CN' : ctx.params.language; + const domain = ctx.params.domain; + const filter_locale = ctx.params.language === 'all' ? 0 : 1; + const url = domain ? `by-site/${domain}` : ''; + + const res = await axios({ + method: 'get', + url: `https://greasyfork.org/${language}/scripts/${url}`, + params: { + filter_locale: filter_locale, + sort: 'updated', + }, + header: { + 'User-Agent': config.ua, + }, + }); + const data = res.data; + const $ = cheerio.load(data); + const list = $('.script-list').find('h2'); + + ctx.state.data = { + title: $('title') + .first() + .text(), + link: `https://greasyfork.org/${language}/scripts/${url}`, + description: $('meta[name=description]').attr('content'), + item: + list && + list + .map((index, item) => { + item = $(item); + return { + title: item.find('a').text(), + description: item.find('.description').text(), + link: `https://greasyfork.org${item.find('a').attr('href')}`, + }; + }) + .get(), + }; +};