fix: apple exchange repair (#2734)

* fix: apple exchange repair

* docs
This commit is contained in:
Cloud
2019-08-01 18:19:38 +08:00
committed by DIYgod
parent 47fbf3c672
commit a16f064908
2 changed files with 14 additions and 39 deletions
+1 -1
View File
@@ -33,7 +33,7 @@ pageClass: routes
### 更换和维修扩展计划
<Route author="metowolf HenryQW" example="/apple/exchange_repair" path="/apple/exchange_repair/:country?" :paramsDesc="['苹果官网 URL 中的国家代码, 默认中国 `cn`']"/>
<Route author="metowolf HenryQW kt286" example="/apple/exchange_repair/zh-cn" path="/apple/exchange_repair/:country?" :paramsDesc="['苹果官网 URL 中的国家代码, 默认美国 中国 `zh-cn`']"/>
### App Store/Mac App Store
+13 -38
View File
@@ -2,59 +2,36 @@ const got = require('@/utils/got');
const cheerio = require('cheerio');
const url = require('url');
const host = 'https://www.apple.com/';
const host = 'https://support.apple.com/';
module.exports = async (ctx) => {
let link;
if (!ctx.params.country) {
ctx.params.country = 'cn';
}
ctx.params.country === 'us' ? (link = url.resolve(host, '/support/exchange_repair/')) : (link = url.resolve(host, `${ctx.params.country}/support/exchange_repair/`));
const country = ctx.params.country || '';
const link = url.resolve(host, `${country}/exchange_repair/`);
const response = await got.get(link);
const $ = cheerio.load(response.data);
const list = $('tr td a')
.map((i, e) => $(e).attr('href'))
.get();
const titlelist = $('tr td a')
.map((i, e) => $(e).text())
.get();
// const datelist = $('tr td p')
// .map((i, e) =>
// $(e)
// .text()
// .trim()
// )
// .get();
const list = $('section.as-columns').get();
const out = await Promise.all(
list.map(async (itemUrl, index) => {
itemUrl = url.resolve(host, itemUrl);
list.map(async (item) => {
const $ = cheerio.load(item);
const $a = $('.icon-chevronright').parent();
const itemUrl = url.resolve(host, $a.attr('href'));
const cache = await ctx.cache.get(itemUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const response = await got.get(itemUrl);
const $ = cheerio.load(response.data);
const description = $('#welcome').html()
? $('#welcome')
.html()
.replace(/src="\//g, `src="${url.resolve(host, '.')}`)
: null;
const $$ = cheerio.load(response.data);
const description = $$('.main').html();
const single = {
title: titlelist[index],
title: $a.text(),
link: itemUrl,
author: 'Apple Inc.',
description: description,
// pubDate: new Date(datelist[index].replace(/(\d+) 年 (\d+) 月 (\d+) 日/, '$1-$2-$3')).toISOString(),
};
ctx.cache.set(itemUrl, JSON.stringify(single));
@@ -62,10 +39,8 @@ module.exports = async (ctx) => {
})
);
const title = ctx.params.country === 'cn' ? 'Apple 支持 -- 更换和维修扩展计划' : `Apple ${ctx.params.country.toUpperCase()} Support -- Exchange and Repair Extension Programs`;
ctx.state.data = {
title,
title: `Apple --- ${$('.main>.richText h1').text()}`,
link,
item: out,
};