diff --git a/docs/other.md b/docs/other.md index b77d0fd543..efba9a15fe 100644 --- a/docs/other.md +++ b/docs/other.md @@ -33,7 +33,7 @@ pageClass: routes ### 更换和维修扩展计划 - + ### App Store/Mac App Store diff --git a/lib/routes/apple/exchange_repair.js b/lib/routes/apple/exchange_repair.js index 9ada838507..279c728a8f 100644 --- a/lib/routes/apple/exchange_repair.js +++ b/lib/routes/apple/exchange_repair.js @@ -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, };