Improve date format handling in some routes (#666)

先 merge #665, 分支的时候不小心包括了 #665,
This commit is contained in:
Henry Wang 2018-09-09 14:05:06 +01:00 committed by DIYgod
parent eacf4705e7
commit 6d57a342b6
4 changed files with 15 additions and 31 deletions

View File

@ -2235,7 +2235,7 @@ type, 分类, 缺省为中港台
#### 特价机票 <Author uid="HenryQW"/>
举例: [https://rsshub.app/atfd/us+new york, gb+london/1](https://rsshub.app/atfd/us+new%20york, gb+london/1)
举例: [https://rsshub.app/atfd/us+new york, gb+london/1](https://rsshub.app/atfd/us+new%20york,gb+london/1)
路由: `/atfd/:locations/:nearby?`
@ -2244,7 +2244,7 @@ type, 分类, 缺省为中港台
- locations: 始发地, 由「国家, 参见 ISO 3166-1 国家代码」和「城市」两部分组成:
1. 单个始发地, 例如 「us+new york」, [https://rsshub.app/atfd/us+new york](https://rsshub.app/atfd/us+new%20york)
2. 逗号分隔多个始发地, 例如 「us+new york, gb+london」, [https://rsshub.app/atfd/us+new york, gb+london/](https://rsshub.app/atfd/us+new%20york, gb+london/)
2. 逗号分隔多个始发地, 例如 「us+new york, gb+london」, [https://rsshub.app/atfd/us+new york, gb+london/](https://rsshub.app/atfd/us+new%20york,gb+london/)
ISO 3166-1 国家代码列表请参见 [维基百科 ISO_3166-1](https://zh.wikipedia.org/wiki/ISO_3166-1)

View File

@ -1,4 +1,5 @@
const axios = require('../../utils/axios');
const dayjs = require('dayjs');
module.exports = async (ctx) => {
const locations = ctx.params.locations;
@ -18,8 +19,7 @@ module.exports = async (ctx) => {
host += `/${country}/${city.replace(' ', '_')}`;
});
url = url.slice(0, -1);
url += `]&includeNearbyOrigins=${nearby}`;
url = url.slice(0, -1) + `]&includeNearbyOrigins=${nearby}`;
const response = await axios({
method: 'get',
@ -35,7 +35,7 @@ module.exports = async (ctx) => {
link: host,
description: 'All the Flight Deals',
item: data.map((item) => ({
title: item.title,
title: `[${dayjs(item.dateRanges[0].start).format('YYYY MMM DD')} to ${dayjs(item.dateRanges[0].end).format('YYYY MMM DD')}] ${item.title}`,
description: `<img src="https://alltheflightdeals.com/assets/cities/${item.cityPairs[0].destinationCity.id}.jpg" alt="${item.cityPairs[0].destination.city},${
item.cityPairs[0].destination.countryName
}"> <br><table><tbody><tr><th align="left" style="border: 1px solid black;">From</th><th align="left" style="border: 1px solid black;">To</th><th align="left" style="border: 1px solid black;">Price</th></tr><tr><td style="border: 1px solid black;"><b>${
@ -50,12 +50,11 @@ module.exports = async (ctx) => {
};
};
function titleCase(str) {
return str
const titleCase = (str) =>
str
.toLowerCase()
.split(' ')
.map(function(word) {
return word.replace(word[0], word[0].toUpperCase());
})
.join(' ');
}

View File

@ -1,5 +1,6 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
const dayjs = require('dayjs');
module.exports = async (ctx) => {
const from = ctx.params.from.toUpperCase();
@ -58,24 +59,7 @@ module.exports = async (ctx) => {
};
};
function formatDate(v) {
return new Intl.DateTimeFormat('en-gb', {
year: 'numeric',
weekday: 'short',
month: 'short',
day: 'numeric',
}).format(new Date(v));
}
function getMonthYear(v) {
const date = new Date(v);
return date.toLocaleString('en-gb', {
month: 'long',
year: 'numeric',
});
}
function formatDesc(e) {
const formatDesc = (e) => {
const item = e.attr('href');
let reg = new RegExp('destination=(.*?)&', 'g');
const destination = reg.exec(item)[1];
@ -88,14 +72,15 @@ function formatDesc(e) {
const price = e.find('.price').text();
const title = `${origin} &#9992; ${destination} ${getMonthYear(departureDate)} for ${price}`;
const title = `${origin} &#9992; ${destination} ${dayjs(departureDate).format('YYYY MMM')} for ${price}`;
const description = `<table><tbody><tr><th align="left" style="border: 1px solid black;">From</th><th align="left" style="border: 1px solid black;">To</th><th align="left" style="border: 1px solid black;">Price</th></tr><tr><td style="border: 1px solid black;">
${origin}</td><td style="border: 1px solid black;">${destination}</td><td style="border: 1px solid black;">${price}</td></tr></tbody></table>${formatDate(departureDate)} &#9992; ${formatDate(returnDate)}`;
${origin}</td><td style="border: 1px solid black;">${destination}</td><td style="border: 1px solid black;">${price}</td></tr></tbody></table>${dayjs(departureDate).format('YYYY MMM DD')} &#9992; ${dayjs(returnDate).format(
'YYYY MMM DD'
)}`;
return {
title,
description,
guid: item,
link: item,
};
}
};

View File

@ -28,7 +28,7 @@ module.exports = async (ctx) => {
item: data.map((item) => {
const date = new Date(item.createdAt);
return {
title: `${title} ${dayjs(date).format('YYYY-MM-DD')}`,
title: `${title} ${dayjs(date).format('YYYY MMM DD')}`,
description: item.content.replace(new RegExp('\n', 'g'), '<br />'),
pubDate: date.toUTCString(),
link: `https://web.okjike.com/message-detail/${item.id}/officialMessage`,