mirror of
https://github.com/hpware/news-analyze.git
synced 2025-06-23 15:51:01 +08:00
Get UpdateAt & PublishedAt and remove testing comment.
This commit is contained in:
parent
5dc5018aca
commit
cb34764c27
@ -1,5 +1,25 @@
|
|||||||
import * as cheerio from "cheerio";
|
import * as cheerio from "cheerio";
|
||||||
|
|
||||||
|
function findTime(timeText: string) {
|
||||||
|
const now = new Date();
|
||||||
|
|
||||||
|
const hourMatch = timeText.match(/(\d+)小時前/);
|
||||||
|
const dayMatch = timeText.match(/(\d+)天前/);
|
||||||
|
const minuteMatch = timeText.match(/(\d+)分鐘前/);
|
||||||
|
if (hourMatch) {
|
||||||
|
const hoursAgo = parseInt(hourMatch[1]);
|
||||||
|
return new Date(now.getTime() - hoursAgo * 60 * 60 * 1000);
|
||||||
|
} else if (dayMatch) {
|
||||||
|
const daysAgo = parseInt(dayMatch[1]);
|
||||||
|
return new Date(now.getTime() - daysAgo * 24 * 60 * 60 * 1000);
|
||||||
|
} else if (minuteMatch) {
|
||||||
|
const minutesAgo = parseInt(minuteMatch[1]);
|
||||||
|
return new Date(now.getTime() - minutesAgo * 60 * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
async function lineToday(slug: string) {
|
async function lineToday(slug: string) {
|
||||||
const url = "https://today.line.me/tw/v2/article/" + slug;
|
const url = "https://today.line.me/tw/v2/article/" + slug;
|
||||||
const fetchPageCode = await fetch(url, {
|
const fetchPageCode = await fetch(url, {
|
||||||
@ -46,6 +66,7 @@ async function lineToday(slug: string) {
|
|||||||
.text()
|
.text()
|
||||||
.replaceAll("\n", "")
|
.replaceAll("\n", "")
|
||||||
.replaceAll(" ", "");
|
.replaceAll(" ", "");
|
||||||
|
|
||||||
let author = "";
|
let author = "";
|
||||||
const authorInfo = html("span.entityPublishInfo-meta-info")
|
const authorInfo = html("span.entityPublishInfo-meta-info")
|
||||||
.text()
|
.text()
|
||||||
@ -57,16 +78,26 @@ async function lineToday(slug: string) {
|
|||||||
} else {
|
} else {
|
||||||
author = authorInfo;
|
author = authorInfo;
|
||||||
}
|
}
|
||||||
|
const orgAuthorDateData = html("span.entityPublishInfo-meta-info").text()
|
||||||
|
const updateMatch = orgAuthorDateData.match(/更新於\s*([^•]+)/);
|
||||||
|
const publishMatch = orgAuthorDateData.match(/發布於\s*(.+)$/);
|
||||||
|
let updatedAt: Date | null = null;
|
||||||
|
if (updateMatch) {
|
||||||
|
updatedAt = findTime(updateMatch[1].trim());
|
||||||
|
}
|
||||||
|
let publishedAt: Date | null = null;
|
||||||
|
if (publishMatch) {
|
||||||
|
publishedAt = findTime(publishMatch[1].trim());
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
title: title,
|
title: title,
|
||||||
paragraph: paragraph,
|
paragraph: paragraph,
|
||||||
origin: newsOrgdir,
|
origin: newsOrgdir,
|
||||||
author: author,
|
author: author,
|
||||||
images: images,
|
images: images,
|
||||||
|
updateat: updatedAt,
|
||||||
|
publishedat: publishedAt
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Texting on console only!
|
|
||||||
//console.log(await lineToday("wJyR8Nw"));
|
|
||||||
|
|
||||||
export default lineToday;
|
export default lineToday;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user