From cb0706b1677c72a2b47f76a7bff1c592b8f72fc4 Mon Sep 17 00:00:00 2001 From: Raven-tu <80778915raventu@gmail.com> Date: Fri, 14 Oct 2022 20:01:56 +0800 Subject: [PATCH] =?UTF-8?q?fix(route):=20=E4=BF=AE=E5=A4=8D=E5=BF=AB?= =?UTF-8?q?=E9=80=92100=E5=BF=AB=E9=80=92=E8=AE=A2=E5=8D=95=E8=BF=BD?= =?UTF-8?q?=E8=B8=AA=E8=B7=AF=E7=94=B1=20(#11076)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix:修复快递100查询服务 * Update utils.js Extend the cookie cache time. * refactor: migrate to v2 --- assets/radar-rules.js | 18 ------ lib/radar-rules.js | 23 -------- lib/router.js | 4 +- lib/{routes => v2}/kuaidi100/index.js | 0 lib/v2/kuaidi100/maintainer.js | 4 ++ lib/v2/kuaidi100/radar.js | 25 ++++++++ lib/v2/kuaidi100/router.js | 4 ++ .../kuaidi100/supported_company.js | 1 - lib/{routes => v2}/kuaidi100/utils.js | 57 +++++++++++++++---- 9 files changed, 81 insertions(+), 55 deletions(-) rename lib/{routes => v2}/kuaidi100/index.js (100%) create mode 100644 lib/v2/kuaidi100/maintainer.js create mode 100644 lib/v2/kuaidi100/radar.js create mode 100644 lib/v2/kuaidi100/router.js rename lib/{routes => v2}/kuaidi100/supported_company.js (91%) rename lib/{routes => v2}/kuaidi100/utils.js (73%) diff --git a/assets/radar-rules.js b/assets/radar-rules.js index 627147be9..4cee2c498 100644 --- a/assets/radar-rules.js +++ b/assets/radar-rules.js @@ -309,24 +309,6 @@ ], grs: [{ title: '研究生院通知', docs: 'https://docs.rsshub.app/university.html#yun-nan-da-xue', source: '/*', target: '' }], }, - 'kuaidi100.com': { - _name: '快递100', - '.': [ - { - title: '快递追踪', - docs: 'https://docs.rsshub.app/other.html#kuai-di-100', - source: '/', - target: (params, url, document) => { - const postid = document && document.querySelector('#postid').value; - const com = document && document.querySelector('#selectComBtn').childNodes[1].attributes[1].value; - if (com && com !== 'default' && postid) { - return `/kuaidi100/track/${com}/${postid}`; - } - }, - }, - { title: '支持的快递公司列表', docs: 'https://docs.rsshub.app/other.html#kuai-di-100', source: '/', target: '/kuaidi100/company' }, - ], - }, 'japanpost.jp': { _name: '日本郵便', 'trackings.post': [ diff --git a/lib/radar-rules.js b/lib/radar-rules.js index d020b6b0b..75cc10f2b 100644 --- a/lib/radar-rules.js +++ b/lib/radar-rules.js @@ -752,29 +752,6 @@ module.exports = { }, ], }, - 'kuaidi100.com': { - _name: '快递100', - '.': [ - { - title: '快递追踪', - docs: 'https://docs.rsshub.app/other.html#kuai-di-100', - source: '/', - target: (params, url, document) => { - const postid = document && document.querySelector('#postid').value; - const com = document && document.querySelector('#selectComBtn').childNodes[1].attributes[1].value; - if (com && com !== 'default' && postid) { - return `/kuaidi100/track/${com}/${postid}`; - } - }, - }, - { - title: '支持的快递公司列表', - docs: 'https://docs.rsshub.app/other.html#kuai-di-100', - source: '/', - target: '/kuaidi100/company', - }, - ], - }, 'hrbeu.edu.cn': { _name: '哈尔滨工程大学', yjsy: [ diff --git a/lib/router.js b/lib/router.js index b11275943..2b540e209 100644 --- a/lib/router.js +++ b/lib/router.js @@ -2474,8 +2474,8 @@ router.get('/haohaozhu/discover/:keyword?', lazyloadRouteHandler('./routes/haoha // router.get('/neu/news/:type', lazyloadRouteHandler('./routes/universities/neu/news')); // 快递100 -router.get('/kuaidi100/track/:number/:id/:phone?', lazyloadRouteHandler('./routes/kuaidi100/index')); -router.get('/kuaidi100/company', lazyloadRouteHandler('./routes/kuaidi100/supported_company')); +// router.get('/kuaidi100/track/:number/:id/:phone?', lazyloadRouteHandler('./routes/kuaidi100/index')); +// router.get('/kuaidi100/company', lazyloadRouteHandler('./routes/kuaidi100/supported_company')); // 稻草人书屋 router.get('/dcrsw/:name/:count?', lazyloadRouteHandler('./routes/novel/dcrsw')); diff --git a/lib/routes/kuaidi100/index.js b/lib/v2/kuaidi100/index.js similarity index 100% rename from lib/routes/kuaidi100/index.js rename to lib/v2/kuaidi100/index.js diff --git a/lib/v2/kuaidi100/maintainer.js b/lib/v2/kuaidi100/maintainer.js new file mode 100644 index 000000000..26265fd37 --- /dev/null +++ b/lib/v2/kuaidi100/maintainer.js @@ -0,0 +1,4 @@ +module.exports = { + '/company': ['NeverBehave'], + '/track/:number/:id/:phone?': ['NeverBehave'], +}; diff --git a/lib/v2/kuaidi100/radar.js b/lib/v2/kuaidi100/radar.js new file mode 100644 index 000000000..37720875c --- /dev/null +++ b/lib/v2/kuaidi100/radar.js @@ -0,0 +1,25 @@ +module.exports = { + 'kuaidi100.com': { + _name: '快递 100', + '.': [ + { + title: '快递订单追踪', + docs: 'https://docs.rsshub.app/other.html#kuai-di-100', + source: '/', + target: (params, url, document) => { + const postid = document && document.querySelector('#postid').value; + const com = document && document.querySelector('#selectComBtn').childNodes[1].attributes[1].value; + if (com && com !== 'default' && postid) { + return `/kuaidi100/track/${com}/${postid}`; + } + }, + }, + { + title: '支持的快递公司列表', + docs: 'https://docs.rsshub.app/other.html#kuai-di-100', + source: '/', + target: '/kuaidi100/company', + }, + ], + }, +}; diff --git a/lib/v2/kuaidi100/router.js b/lib/v2/kuaidi100/router.js new file mode 100644 index 000000000..8efbf7e9b --- /dev/null +++ b/lib/v2/kuaidi100/router.js @@ -0,0 +1,4 @@ +module.exports = (router) => { + router.get('/company', require('./supported_company')); + router.get('/track/:number/:id/:phone?', require('./index')); +}; diff --git a/lib/routes/kuaidi100/supported_company.js b/lib/v2/kuaidi100/supported_company.js similarity index 91% rename from lib/routes/kuaidi100/supported_company.js rename to lib/v2/kuaidi100/supported_company.js index b661bf6a4..8553d235b 100644 --- a/lib/routes/kuaidi100/supported_company.js +++ b/lib/v2/kuaidi100/supported_company.js @@ -10,7 +10,6 @@ module.exports = async (ctx) => { title: item.name, description: item.number, category: item.comTypeName, - pubDate: new Date().toUTCString(), link: item.siteUrl, })), }; diff --git a/lib/routes/kuaidi100/utils.js b/lib/v2/kuaidi100/utils.js similarity index 73% rename from lib/routes/kuaidi100/utils.js rename to lib/v2/kuaidi100/utils.js index 485814240..a3f0139a2 100644 --- a/lib/routes/kuaidi100/utils.js +++ b/lib/v2/kuaidi100/utils.js @@ -1,6 +1,10 @@ const got = require('@/utils/got'); const wwwid_key = 'kuaidi100-wwwid'; const csrf_key = 'kuaidi100-csrf'; +const globacsrftoken_key = 'kuaidi100-globacsrftoken'; +const dasddocTitl_key = 'kuaidi100-dasddocTitl'; +const dasddocReferrer_key = 'kuaidi100-dasddocReferrer'; +const dasddocHref_key = 'kuaidi100-dasddocHref'; const query_count = 'kuaidi100-cookie-count'; const max_query_count = 30; @@ -9,7 +13,11 @@ async function getCookie(ctx) { shouldUpdateCookie(ctx); let wwwid = await ctx.cache.get(wwwid_key); let csrf = await ctx.cache.get(csrf_key); - if (!wwwid || !csrf) { + let globacsrftoken = await ctx.cache.get(globacsrftoken_key); + let dasddocTitl = await ctx.cache.get(dasddocTitl_key); + let dasddocReferrer = await ctx.cache.get(dasddocReferrer_key); + let dasddocHref = await ctx.cache.get(dasddocHref_key); + if (!wwwid || !csrf || !dasddocTitl || !dasddocReferrer || !dasddocHref) { const indexResponse = await got({ method: 'get', url: 'https://www.kuaidi100.com/?from=appstore', @@ -25,12 +33,24 @@ async function getCookie(ctx) { wwwid = e.split(';')[0]; } else if (e.indexOf('csrftoken') === 0) { csrf = e.split(';')[0]; + } else if (e.indexOf('globacsrftoken') === 0) { + globacsrftoken = e.split(';')[0]; + } else if (e.indexOf('dasddocTitle') >= 0) { + dasddocTitl = e.split(';')[0]; + } else if (e.indexOf('dasddocReferrer') >= 0) { + dasddocReferrer = e.split(';')[0]; + } else if (e.indexOf('dasddocHref') >= 0) { + dasddocHref = e.split(';')[0]; } } } ctx.cache.set(wwwid_key, wwwid, 600); ctx.cache.set(csrf_key, csrf, 600); + ctx.cache.set(globacsrftoken_key, globacsrftoken, 600); + ctx.cache.set(dasddocTitl_key, dasddocTitl, 600); + ctx.cache.set(dasddocReferrer_key, dasddocReferrer, 600); + ctx.cache.set(dasddocHref_key, dasddocHref, 600); // We have acquired new cookie. It may due to cache timeout. // Force update counter and don't wait it finished. shouldUpdateCookie(ctx, true); @@ -39,6 +59,10 @@ async function getCookie(ctx) { return { wwwid, csrf, + globacsrftoken, + dasddocTitl, + dasddocReferrer, + dasddocHref, }; } @@ -61,11 +85,10 @@ async function getCookie(ctx) { "version": 776 } */ -async function getCompanyList(ctx) { +function getCompanyList(ctx) { // Using date as cache key and it will automatically expired by 1d const key = `kuaidi100-company-name-${new Date().toISOString().split('T')[0]}`; - let list = await ctx.cache.get(key); - if (!list) { + return ctx.cache.tryGet(key, async () => { const cookie = await getCookie(ctx); const wwwid = cookie.wwwid; const companyResponse = await got({ @@ -76,7 +99,7 @@ async function getCompanyList(ctx) { Cookie: wwwid, }, }); - list = companyResponse.body; + let list = companyResponse.body; // Parsing the js file try { @@ -87,11 +110,8 @@ async function getCompanyList(ctx) { throw new Error('无法正确获取快递公司列表:请稍后重试'); } - ctx.cache.set(key, list); - } else { - list = JSON.parse(list); - } - return list; + return list; + }); } function shouldUpdateCookie(ctx, forcedUpdate = false) { @@ -186,6 +206,17 @@ module.exports = { getQuery: async (ctx, number, id, phone) => { const query_key = `kuaidi100-query-${number}-${id}`; let query = await ctx.cache.get(query_key); + // 组装日期与单号 日期为提前两个月 + const timestamp = Number.parseInt(new Date().getTime() / 1000); + const lastTowMonth = new Date().getTime() - 60 * 60 * 24 * 60 * 1000; + const dateString = new Date(lastTowMonth).toLocaleDateString('ZH').split('/').join(''); + const queryMeta = encodeURIComponent( + JSON.stringify({ + date: dateString, + nums: [id], + }) + ); + if (!query) { const cookie = await getCookie(ctx); const queryResponse = await got({ @@ -193,7 +224,11 @@ module.exports = { url: `https://www.kuaidi100.com/query?type=${number}&postid=${id}&temp=${Math.random()}&phone=${phone ? phone : ''}`, headers: { Referer: 'https://www.kuaidi100.com/', - Cookie: `${cookie.csrf}; ${cookie.wwwid}`, + 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,ja;q=0.7', + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36', + Cookie: `${cookie.globacsrftoken}; ${cookie.csrf}; ${cookie.wwwid}; ${cookie.dasddocHref}; ${cookie.dasddocReferrer}; ${ + cookie.dasddocTitl + }; addcom=${number}; addnu=${id}; snt_query_meta=${queryMeta}; sortStatus=0; Hm_lpvt_22ea01af58ba2be0fec7c11b25e88e6c=${timestamp}; Hm_lvt_22ea01af58ba2be0fec7c11b25e88e6c=${timestamp - 1642}`, }, });