rsshub
Version:
Make RSS Great Again!
221 lines (217 loc) • 6.83 kB
JavaScript
import { n as init_esm_shims, t as __dirname } from "./esm-shims-CzJ_djXG.mjs";
import "./config-C37vj7VH.mjs";
import "./dist-BInvbO1W.mjs";
import "./logger-Czu8UMNd.mjs";
import "./ofetch-BIyrKU3Y.mjs";
import "./helpers-DxBp0Pty.mjs";
import { t as art } from "./render-BQo6B4tL.mjs";
import { t as got_default } from "./got-KxxWdaxq.mjs";
import path from "node:path";
import dayjs from "dayjs";
import customParseFormat from "dayjs/plugin/customParseFormat.js";
import crypto from "node:crypto";
import { load } from "cheerio";
import cityTimezones from "city-timezones";
import timezone from "dayjs/plugin/timezone.js";
import utc from "dayjs/plugin/utc.js";
//#region lib/routes/japanpost/utils.ts
init_esm_shims();
dayjs.extend(customParseFormat);
dayjs.extend(utc);
dayjs.extend(timezone);
const utils = {
expandOdd: (c) => {
c.prototype.odd = function() {
const odds = [];
this.each((index, item) => {
if (index % 2 === 1) odds.push(item);
});
return c(odds);
};
},
expandEven: (c) => {
c.prototype.even = function() {
const evens = [];
this.each((index, item) => {
if (index % 2 === 0) evens.push(item);
});
return c(evens);
};
},
expandReverse: (c) => {
c.prototype.reverse = function() {
const reverses = [];
this.each((index, item) => {
reverses.push(item);
});
reverses.reverse();
return c(reverses);
};
},
generateGuid: (t) => {
const hash = crypto.createHash("sha512");
hash.update(t);
return hash.digest("hex").toUpperCase();
},
parseDatetime: (t, o, r, tz, l) => {
const formatJaDate = "YYYY/MM/DD";
const formatJaDateTime = "YYYY/MM/DD HH:mm";
const formatEnDate = "MM/DD/YYYY";
const formatEnDateTime = "MM/DD/YYYY HH:mm";
let customFormat;
switch (l) {
case "ja":
customFormat = dayjs(t, formatJaDate, true).isValid() ? formatJaDate : dayjs(t, formatJaDateTime, true).isValid() ? formatJaDateTime : void 0;
break;
case "en":
customFormat = dayjs(t, formatEnDate, true).isValid() ? formatEnDate : dayjs(t, formatEnDateTime, true).isValid() ? formatEnDateTime : void 0;
break;
}
if (o) {
const packageInJPKeywords = [["郵便局"], [
"都",
"道",
"府",
"県"
]];
if (packageInJPKeywords[0].some((i) => o.includes(i)) || packageInJPKeywords[1].some((i) => r.includes(i))) tz = "Asia/Tokyo";
else {
const oS = o.replace(" EMS", "").replace(" INT", "");
try {
try {
tz = cityTimezones.lookupViaCity(oS)[0].timezone;
} catch {
tz = cityTimezones.lookupViaCity(r)[0].timezone;
}
} catch {}
}
}
return customFormat ? [dayjs.tz(t, customFormat, tz).valueOf(), tz] : [new Date(t).getTime(), tz];
}
};
var utils_default = utils;
//#endregion
//#region lib/routes/japanpost/track.ts
let baseTitle = "日本郵便";
const baseUrl = "https://trackings.post.japanpost.jp/services/srv/search/direct?";
async function track(ctx) {
const reqCode = ctx.req.param("reqCode");
const reqReqCode = "reqCodeNo1=" + reqCode;
let locale = "ja";
if (ctx.req.param("locale") === "en") {
locale = "en";
baseTitle = "Japanpost";
}
const reqLocale = "&locale=" + locale;
const link = baseUrl + reqReqCode + reqLocale;
const $ = load((await got_default({
method: "get",
url: link
})).data);
utils_default.expandEven($);
utils_default.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.toArray().map((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()
};
});
if (!list.length) {
const resErrorText = $(".tableType01").eq(0).find("tr").eq(2).find("td").eq(1).text().trim();
throw new Error(resErrorText);
}
const listEven = list.even();
const listOdd = list.odd();
const packageType = $(".tableType01").eq(0).find("tr").eq(1).find("td").eq(1).text().trim();
const packageService = $(".tableType01").eq(0).find("tr").eq(1).find("td").eq(2).text().trim();
const serviceText = locale === "ja" ? "付加サービス:" : "Additional services: ";
let lastItemTimeStamp;
let tz;
return {
title: `${baseTitle} ${reqCode} ${packageType}`,
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.toArray().map((item, index) => {
const itemTd = $(item).find("td");
const packageStatus = itemTd.eq(1).text().trim();
const packageRegion = itemTd.eq(4).text().trim();
const packageOffice = itemTd.eq(3).text().trim();
const packageOfficeZipCode = listOdd.eq(index).find("td").eq(0).text().trim();
const itemTitle = `${packageStatus} ${packageOffice} ${packageRegion}`;
const packageTrackRecord = itemTd.eq(2).text().trim();
const itemDescription = art(path.join(__dirname, "templates/track_item_desc-8ae6c583.art"), {
packageStatus,
packageTrackRecord,
packageOfficeZipCode,
packageOffice,
packageRegion,
index,
officeItemList,
serviceText,
packageService
});
const itemPubDateText = itemTd.eq(0).text().trim();
const itemGuid = utils_default.generateGuid(reqCode + itemTitle + itemDescription + itemPubDateText);
let thisItemTimeStamp;
[thisItemTimeStamp, tz] = utils_default.parseDatetime(itemPubDateText, packageOffice, packageRegion, tz, locale);
if (lastItemTimeStamp && thisItemTimeStamp <= lastItemTimeStamp) thisItemTimeStamp = lastItemTimeStamp + 1e3;
lastItemTimeStamp = thisItemTimeStamp;
return {
title: itemTitle,
description: itemDescription,
pubDate: new Date(thisItemTimeStamp),
link,
guid: itemGuid.slice(0, 32)
};
})
};
}
//#endregion
//#region lib/routes/japanpost/router.ts
const route = {
name: "Track & Trace Service",
path: "/track/:reqCode/:locale?",
example: "/japanpost/track/EJ123456789JP/en",
url: "trackings.post.japanpost.jp/services/srv/search/",
handler: track,
categories: ["other"],
maintainers: ["tuzi3040"],
parameters: {
reqCode: "Package Number",
locale: "Language, default to japanese `ja`"
},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportRadar: false,
supportBT: false,
supportPodcast: false,
supportScihub: false
},
description: `| Japanese | English |
| -------- | ------- |
| ja | en |`,
zh: {
name: "邮件追踪查询",
description: `| 日语 | 英语 |
| ---- | ---- |
| ja | en |`
},
ja: {
name: "郵便追跡サービス",
description: `| 日本語 | 英語 |
| ---- | ---- |
| ja | en |`
}
};
//#endregion
export { route };