Add Apple Support exchange repair (#611)

https://www.apple.com/cn/support/exchange_repair/
更换和维修扩展计划
This commit is contained in:
METO
2018-09-01 23:36:07 +08:00
committed by DIYgod
parent 7aec628fa8
commit d52fb080d1
3 changed files with 69 additions and 0 deletions
+10
View File
@@ -2538,3 +2538,13 @@ GitHub 官方也提供了一些 RSS:
路由: `/ifanr/appso`
参数: 无
## Apple <Author uid="metowolf"/>
### 更换和维修扩展计划
举例: <https://rsshub.app/apple/exchange_repair>
路由: `/apple/exchange_repair`
参数: 无
+3
View File
@@ -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;
+56
View File
@@ -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,
};
};