diff --git a/docs/README.md b/docs/README.md index cd86b51782..01887b243b 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2538,3 +2538,13 @@ GitHub 官方也提供了一些 RSS: 路由: `/ifanr/appso` 参数: 无 + +## Apple + +### 更换和维修扩展计划 + +举例: + +路由: `/apple/exchange_repair` + +参数: 无 diff --git a/router.js b/router.js index f9cbf38cfd..45575e7228 100755 --- a/router.js +++ b/router.js @@ -498,4 +498,7 @@ router.get('/ifanr/appso', require('./routes/ifanr/appso')); // 果壳网 router.get('/guokr/scientific', require('./routes/guokr/scientific')); +// Apple +router.get('/apple/exchange_repair', require('./routes/apple/exchange_repair')); + module.exports = router; diff --git a/routes/apple/exchange_repair.js b/routes/apple/exchange_repair.js new file mode 100644 index 0000000000..583a2f323b --- /dev/null +++ b/routes/apple/exchange_repair.js @@ -0,0 +1,56 @@ +const axios = require('../../utils/axios'); +const cheerio = require('cheerio'); +const url = require('url'); + +const host = 'https://www.apple.com/'; + +module.exports = async (ctx) => { + const link = url.resolve(host, 'cn/support/exchange_repair/'); + const response = await axios.get(link); + + const $ = cheerio.load(response.data); + + const list = $('tr td a') + .map((i, e) => $(e).attr('href')) + .get(); + + const datelist = $('tr td p') + .map((i, e) => + $(e) + .text() + .trim() + ) + .get(); + + const out = await Promise.all( + list.map(async (itemUrl, index) => { + itemUrl = url.resolve(host, itemUrl); + const cache = await ctx.cache.get(itemUrl); + if (cache) { + return Promise.resolve(JSON.parse(cache)); + } + + const response = await axios.get(itemUrl); + const $ = cheerio.load(response.data); + + const single = { + title: $('h1').text(), + link: itemUrl, + author: 'Apple Inc.', + description: $('#welcome') + .html() + .replace(/src="\//g, `src="${url.resolve(host, '.')}`), + pubDate: new Date(datelist[index].replace(/(\d+) 年 (\d+) 月 (\d+) 日/, '$1-$2-$3')), + }; + + ctx.cache.set(itemUrl, JSON.stringify(single), 24 * 60 * 60); + return Promise.resolve(single); + }) + ); + + ctx.state.data = { + title: 'Apple 支持 -- 更换和维修扩展计划', + link, + item: out, + }; +};