generated from nandiheath/gatsby-template-deprecated
-
Notifications
You must be signed in to change notification settings - Fork 43
/
gn.js
49 lines (46 loc) · 1.25 KB
/
gn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* Fetch latest AE waiting time
*
*/
const Parser = require("rss-parser")
exports.fetchGovNews = async () => {
try {
const parser = new Parser({
timeout: 8000,
customFields: {
item: ["description"],
},
})
const [{ items: records_zh }, { items: records_en }] = await Promise.all([
parser.parseURL(
"https://www.news.gov.hk/tc/common/html/topstories.rss.xml"
),
parser.parseURL(
"https://www.news.gov.hk/en/common/html/topstories.rss.xml"
),
])
function getMonth(monthStr) {
return new Date(monthStr + "-1-01").getMonth() + 1
}
const recordsProcessed = records_zh.map((r, ridx) => {
const d = r.pubDate.split(" ")
const month = getMonth(d[2])
const date = [d[3], month < 10 ? "0" + month : month, d[1]].join("-")
return Object.assign(
{
title_zh: r.title,
title_en: records_en[ridx] ? records_en[ridx].title : "",
link_zh: r.link,
link_en: records_en[ridx] ? records_en[ridx].link : "",
date: date,
},
r
)
})
return { records: recordsProcessed }
} catch (e) {
console.log(`Error in fetching Gov News`)
console.log(e)
return { records: [] }
}
}