feat(route): migrate japanpost to v2 (#13181)

* feat(route): migrate japanpost to v2

fix(route): japanpost: fix cheerio custom methods
fix(route): japanpost: fix manual timezone parse

* Apply suggestions from code review

* Apply suggestions from code review

---------
This commit is contained in:
tuzi3040
2023-09-02 03:08:16 +08:00
committed by GitHub
parent d6a7f6d021
commit c4c947d237
9 changed files with 81 additions and 77 deletions
-17
View File
@@ -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': {
-17
View File
@@ -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: [
-3
View File
@@ -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'));
+3
View File
@@ -0,0 +1,3 @@
module.exports = {
'/track/:reqCode/:locale?': ['tuzi3040'],
};
+19
View File
@@ -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}`;
}
},
},
],
},
};
+3
View File
@@ -0,0 +1,3 @@
module.exports = (router) => {
router.get('/track/:reqCode/:locale?', require('./track'));
};
@@ -0,0 +1,14 @@
{{ packageStatus }}<br>
{{ if packageTrackRecord }}{{ packageTrackRecord }}<br>{{ /if }}
{{ if packageOfficeZipCode }}{{ packageOfficeZipCode }} {{ /if }}{{ if packageOffice }}{{ packageOffice }} {{ /if }}{{ packageRegion }}
{{ if index === 0 }}
{{ if officeItemList }}
<br>
{{ each officeItemList }}
<br>{{ $value.officeType }} {{@ $value.officeName }} {{ $value.officeTel }}
{{ /each }}
{{ /if }}
{{ if packageService }}
<br>{{ serviceText }}{{ packageService }}
{{ /if }}
{{ /if }}
@@ -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}<br>`;
itemDescription += packageTrackRecord ? `${packageTrackRecord}<br>` : '';
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 += '<br>';
for (const i of officeItemList) {
itemDescription += `<br>${i}`;
}
}
if (packageService) {
itemDescription += '<br>' + 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);
@@ -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 {