diff --git a/assets/radar-rules.js b/assets/radar-rules.js index c7ab64a7e6..bb0034b05f 100644 --- a/assets/radar-rules.js +++ b/assets/radar-rules.js @@ -171,23 +171,6 @@ ], grs: [{ title: '研究生院通知', docs: 'https://docs.rsshub.app/routes/university#yun-nan-da-xue', source: '/*', target: '' }], }, - 'japanpost.jp': { - _name: '日本郵便', - 'trackings.post': [ - { - title: '郵便・荷物の追跡', - docs: 'https://docs.rsshub.app/routes/other#ri-ben-you-bian-you-bian-zhui-ji-サービス', - source: '/services/srv/search/direct', - target: (params, url) => { - const reqCode = new URL(url).searchParams.get('reqCodeNo1').toUpperCase(); - const locale = new URL(url).searchParams.get('locale').toLowerCase(); - if ((reqCode.search(/^(?:\d{11,12}|[A-Z]{2}\d{9}[A-Z]{2})$/) === 0 && locale === 'ja') || locale === 'en') { - return `/japanpost/track/${reqCode}/${locale}`; - } - }, - }, - ], - }, // 'biquge5200.com': { www: [{ title: 'biquge5200.com', docs: 'https://docs.rsshub.app/routes/reading#bi-qu-ge-biquge5200-com', source: '/:id', target: '/novel/biquge/:id' }] }, // 'biquge.info': { www: [{ title: 'biquge.info', docs: 'https://docs.rsshub.app/routes/reading#bi-qu-ge-biquge-info', source: '/:id', target: '/novel/biqugeinfo/:id' }] }, 'matters.news': { diff --git a/lib/radar-rules.js b/lib/radar-rules.js index 43e5206b3c..00e5d00f0b 100644 --- a/lib/radar-rules.js +++ b/lib/radar-rules.js @@ -565,23 +565,6 @@ module.exports = { }, ], }, - 'japanpost.jp': { - _name: '日本郵便', - 'trackings.post': [ - { - title: '郵便・荷物の追跡', - docs: 'https://docs.rsshub.app/routes/other#ri-ben-you-bian-you-bian-zhui-ji-サービス', - source: '/services/srv/search/direct', - target: (params, url) => { - const reqCode = new URL(url).searchParams.get('reqCodeNo1').toUpperCase(); - const locale = new URL(url).searchParams.get('locale').toLowerCase(); - if ((reqCode.search(/^(?:\d{11,12}|[A-Z]{2}\d{9}[A-Z]{2})$/) === 0 && locale === 'ja') || locale === 'en') { - return `/japanpost/track/${reqCode}/${locale}`; - } - }, - }, - ], - }, 'csc.edu.cn': { _name: '国家留学网', www: [ diff --git a/lib/router.js b/lib/router.js index 9ef5ac9502..6d1ce134a6 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1142,9 +1142,6 @@ router.get('/leboncoin/ad/:query', lazyloadRouteHandler('./routes/leboncoin/ad.j // DHL router.get('/dhl/:id', lazyloadRouteHandler('./routes/dhl/shipment-tracking')); -// Japanpost -router.get('/japanpost/track/:reqCode/:locale?', lazyloadRouteHandler('./routes/japanpost/track')); - // Hanime router.get('/hanime/video', lazyloadRouteHandler('./routes/hanime/video')); diff --git a/lib/v2/japanpost/maintainer.js b/lib/v2/japanpost/maintainer.js new file mode 100644 index 0000000000..5d5f1e4066 --- /dev/null +++ b/lib/v2/japanpost/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/track/:reqCode/:locale?': ['tuzi3040'], +}; diff --git a/lib/v2/japanpost/radar.js b/lib/v2/japanpost/radar.js new file mode 100644 index 0000000000..d28ade2ab0 --- /dev/null +++ b/lib/v2/japanpost/radar.js @@ -0,0 +1,19 @@ +module.exports = { + 'japanpost.jp': { + _name: '日本郵便', + 'trackings.post': [ + { + title: '郵便・荷物の追跡', + docs: 'https://docs.rsshub.app/routes/other#japanpost-ri-ben-you-bian', + source: '/services/srv/search/direct', + target: (params, url) => { + const reqCode = new URL(url).searchParams.get('reqCodeNo1').toUpperCase(); + const locale = new URL(url).searchParams.get('locale').toLowerCase(); + if ((reqCode.search(/^(?:\d{11,12}|[A-Z]{2}\d{9}[A-Z]{2})$/) === 0 && locale === 'ja') || locale === 'en') { + return `/japanpost/track/${reqCode}/${locale}`; + } + }, + }, + ], + }, +}; diff --git a/lib/v2/japanpost/router.js b/lib/v2/japanpost/router.js new file mode 100644 index 0000000000..a5a9c5d099 --- /dev/null +++ b/lib/v2/japanpost/router.js @@ -0,0 +1,3 @@ +module.exports = (router) => { + router.get('/track/:reqCode/:locale?', require('./track')); +}; diff --git a/lib/v2/japanpost/templates/track_item_desc.art b/lib/v2/japanpost/templates/track_item_desc.art new file mode 100644 index 0000000000..4b44d5d807 --- /dev/null +++ b/lib/v2/japanpost/templates/track_item_desc.art @@ -0,0 +1,14 @@ +{{ packageStatus }}
+{{ if packageTrackRecord }}{{ packageTrackRecord }}
{{ /if }} +{{ if packageOfficeZipCode }}{{ packageOfficeZipCode }} {{ /if }}{{ if packageOffice }}{{ packageOffice }} {{ /if }}{{ packageRegion }} +{{ if index === 0 }} + {{ if officeItemList }} +
+ {{ each officeItemList }} +
{{ $value.officeType }} {{@ $value.officeName }} {{ $value.officeTel }} + {{ /each }} + {{ /if }} + {{ if packageService }} +
{{ serviceText }}{{ packageService }} + {{ /if }} +{{ /if }} diff --git a/lib/routes/japanpost/track.js b/lib/v2/japanpost/track.js similarity index 68% rename from lib/routes/japanpost/track.js rename to lib/v2/japanpost/track.js index 9824cd391a..bd405177dc 100644 --- a/lib/routes/japanpost/track.js +++ b/lib/v2/japanpost/track.js @@ -1,13 +1,12 @@ const got = require('@/utils/got'); +const { art } = require('@/utils/render'); const cheerio = require('cheerio'); +const path = require('path'); const utils = require('./utils'); let baseTitle = '日本郵便'; const baseUrl = 'https://trackings.post.japanpost.jp/services/srv/search/direct?'; -utils.expandEven(); -utils.expandOdd(); - module.exports = async (ctx) => { const reqCode = ctx.params.reqCode; const reqReqCode = 'reqCodeNo1=' + reqCode; @@ -27,8 +26,25 @@ module.exports = async (ctx) => { }); const $ = cheerio.load(response.data); + utils.expandEven($); + utils.expandOdd($); + const list = $('.tableType01').eq(1).find('tr').slice(2); const officeList = $('.tableType03').eq(0).find('tr').slice(1); + let officeItemList; + + if (officeList.length) { + officeItemList = officeList + .map((i, e) => { + const eTd = $(e).find('td'); + return { + officeType: eTd.eq(0).text().trim(), + officeName: eTd.eq(1).html().trim(), + officeTel: eTd.eq(2).html().trim(), + }; + }) + .get(); + } if (!list.length) { const resErrorText = $('.tableType01').eq(0).find('tr').eq(2).find('td').eq(1).text().trim(); @@ -50,6 +66,8 @@ module.exports = async (ctx) => { link, description: `${baseTitle} ${reqCode} ${packageType}`, language: locale, + icon: 'https://www.post.japanpost.jp/favicon.ico', + logo: 'https://www.post.japanpost.jp/favicon.ico', item: listEven .map((index, item) => { const itemTd = $(item).find('td'); @@ -59,32 +77,17 @@ module.exports = async (ctx) => { const packageOfficeZipCode = listOdd.eq(index).find('td').eq(0).text().trim(); const itemTitle = `${packageStatus} ${packageOffice} ${packageRegion}`; const packageTrackRecord = itemTd.eq(2).text().trim(); - let itemDescription = `${packageStatus}
`; - itemDescription += packageTrackRecord ? `${packageTrackRecord}
` : ''; - itemDescription += packageOfficeZipCode ? `${packageOfficeZipCode} ` : ''; - itemDescription += packageOffice ? `${packageOffice} ` : ''; - itemDescription += packageRegion; - - if (index === 0) { - if (officeList.length) { - const officeItemList = officeList - .map((i, e) => { - const eTd = $(e).find('td'); - let officeItem = eTd.eq(0).text().trim(); - officeItem += ` ${eTd.eq(1).html().trim()}`; - officeItem += ` ${eTd.eq(2).html().trim()}`; - return officeItem; - }) - .get(); - itemDescription += '
'; - for (const i of officeItemList) { - itemDescription += `
${i}`; - } - } - if (packageService) { - itemDescription += '
' + serviceText + packageService; - } - } + const itemDescription = art(path.join(__dirname, 'templates/track_item_desc.art'), { + packageStatus, + packageTrackRecord, + packageOfficeZipCode, + packageOffice, + packageRegion, + index, + officeItemList, + serviceText, + packageService, + }); const itemPubDateText = itemTd.eq(0).text().trim(); const itemGuid = utils.generateGuid(reqCode + itemTitle + itemDescription + itemPubDateText); diff --git a/lib/routes/japanpost/utils.js b/lib/v2/japanpost/utils.js similarity index 85% rename from lib/routes/japanpost/utils.js rename to lib/v2/japanpost/utils.js index 3bf16d6070..b4b4a1ae08 100644 --- a/lib/routes/japanpost/utils.js +++ b/lib/v2/japanpost/utils.js @@ -1,4 +1,3 @@ -const cheerio = require('cheerio'); const crypto = require('crypto'); const cityTimezones = require('city-timezones'); @@ -11,36 +10,36 @@ dayjs.extend(utc); dayjs.extend(timezone); const utils = { - expandOdd: () => { - cheerio.prototype.odd = function () { + expandOdd: (c) => { + c.prototype.odd = function () { const odds = []; this.each((index, item) => { if (index % 2 === 1) { odds.push(item); } }); - return cheerio(odds); + return c(odds); }; }, - expandEven: () => { - cheerio.prototype.even = function () { + expandEven: (c) => { + c.prototype.even = function () { const evens = []; this.each((index, item) => { if (index % 2 === 0) { evens.push(item); } }); - return cheerio(evens); + return c(evens); }; }, - expandReverse: () => { - cheerio.prototype.reverse = function () { + expandReverse: (c) => { + c.prototype.reverse = function () { const reverses = []; this.each((index, item) => { reverses.push(item); }); reverses.reverse(); - return cheerio(reverses); + return c(reverses); }; }, generateGuid: (t) => { @@ -66,7 +65,7 @@ const utils = { } if (o) { - const packageInJPKeywords = [['郵便局'], ['都​', '道', '府', '県']]; + const packageInJPKeywords = [['郵便局'], ['都', '道', '府', '県']]; if (packageInJPKeywords[0].some((i) => o.includes(i)) || packageInJPKeywords[1].some((i) => r.includes(i))) { tz = 'Asia/Tokyo'; } else {