This repository was archived by the owner on Feb 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
123 lines (108 loc) · 4.04 KB
/
index.js
File metadata and controls
123 lines (108 loc) · 4.04 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const cheerio = require('cheerio')
const fetch = require('node-fetch')
const fs = require('fs')
const yaml = require('js-yaml')
const git = require('./git')
const kubeweekly = async () => {
try {
const data = await fetch('https://kubeweekly.io')
return data.text()
} catch (error) {
return error;
}
}
const getKubeWeeklyContentTitle = (rawHtml) => {
const $ = cheerio.load(rawHtml);
return $('#templateHeader').text()
}
const getContentListTitle = (title) => {
const regexPatternTitle = /kubeweekly(.*)#\d+/gi
const titleContent = title.match(regexPatternTitle)[0].toLowerCase() || null
return titleContent
}
const getFileName = (title) => {
const name = title.replace('#','').split(' ').join('')
return name
}
const getDateTitle = (title) => {
const regexPatternTitle = /\w+\s\d+,\s\d+/si
const dateParse = title.match(regexPatternTitle)
const date = new Date(Date.parse(dateParse)).toLocaleString().slice(0,10)
return date
}
const kubeWeeklyContentParser = (htmlRaw,contentHeadline) => {
const regexElement = /<a href=(.*?) [^>]*>(.*?)<\/a>/g
const regexContent = /<a href="(.*?)" [^>]*>(.*?)<\/a>/
let result = []
const $ = cheerio.load(htmlRaw)
let data = $('table.mcnTextBlock > tbody.mcnTextBlockOuter')
data.toArray().map(el => {
let content = $.html(el)
if(content.includes(contentHeadline)){
content.match(regexElement).map(e => {
let parsedData = regexContent.exec(e);
const contentLink = parsedData[1].replace(/\n/g, " ").trim()
const contentTitle = parsedData[2].replace(/\n/g, " ").trim()
const contentType = contentHeadline.split(' ')[1].toLowerCase()
result.push({
title:contentTitle,
link:contentLink,
type:contentType
})
})
}
})
return result
}
const main = async () => {
try {
let result = []
let resultKubeweeklyContent = []
const headlines =['The Technical','The Editorial','The Headlines']
const kubeWeeklyContent = await kubeweekly();
const kubeWeeklyContentHeadlineRaw = getKubeWeeklyContentTitle(kubeWeeklyContent)
const dateTitle = getDateTitle(kubeWeeklyContentHeadlineRaw)
const contentTitle = getContentListTitle(kubeWeeklyContentHeadlineRaw)
result.push({
title: contentTitle,
date: dateTitle,
source: 'kubeweekly'
})
headlines.map((contentHeadline) => {
kubeWeeklyContentParser(kubeWeeklyContent, contentHeadline)
.map(kc => {
resultKubeweeklyContent.push(kc)
})
})
result.push({data: resultKubeweeklyContent})
return result
} catch (error) {
return error;
}
}
main().then(data => {
git.pull()
let existingContentYaml = yaml.safeLoad(fs.readFileSync('./contentList.yaml','utf8'));
let headerContent = data.shift()
const found = existingContentYaml.contentList.some(el => el.date === headerContent.date);
if(!found){
let content = {
...headerContent,
...data[0]
}
const name = './contents/' + getFileName(headerContent.title) + '.yaml';
existingContentYaml.contentList.push({
title: headerContent.title,
tags: ["#kubereads"],
date: headerContent.date,
status: 'not delivered',
content: name
})
let kubeweeklyContentYAML = yaml.safeDump(content);
let kubeweeklyContentListYAML = yaml.safeDump(existingContentYaml);
fs.writeFileSync(name, kubeweeklyContentYAML, 'utf8');
fs.writeFileSync('./contentList.yaml',kubeweeklyContentListYAML,'utf8')
git.push(headerContent.title)
console.log('kubeweekly.yaml updated')
}else console.log('kubeweekly not updated')
})