-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
99 lines (92 loc) · 3.38 KB
/
app.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
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
const fetch = require('node-fetch');
const jsonfile = require('jsonfile')
const YAML = require('yamljs');
const github = require('octonode');
const config = require('config');
let token = config.get('token');
let client = github.client(token);
let ghrepo = client.repo('PDIS/web-jekyll');
let more_url = '';
let topics = [];
let posts = [];
let file = '/var/discourse/api/tracks.json'
let getIDs = async (more_url) => {
if (more_url == '') {
query = "http://talk.pdis.nat.gov.tw/c/pdis-site/how-we-work-track.json";
} else {
query = "http://talk.pdis.nat.gov.tw" + more_url.replace(/\?page/, '.json?page');
}
let response = await fetch(query);
let data = await response.json()
more_url = data.topic_list.more_topics_url || ''
let topics_tmp = data.topic_list.topics
// topics_tmp.splice(0,1) // remove first post (duplicated)
topics_tmp.map(t => topics.push(t.id))
if (more_url != '') { // recursively getIDs
let ids = await getIDs(more_url);
}
}
let getPosts = async () => { // 取得單篇PO文
// * remove duplicated post
topics = topics.filter((topic, i) => topics.indexOf(topic) === i)
// * remove "definition" post
topics = topics.filter((topic, i) => topic != '73')
for (let id of topics) {
try {
let response = await fetch('http://talk.pdis.nat.gov.tw/t/' + id + ".json?include_raw=1")
let data = await response.json()
let post = {};
post['id'] = data['id']
post['title'] = data['title'];
post['date'] = await new Date(data['created_at'].toString()).toISOString().substring(0, 10);
post['tags'] = data['tags'];
let raw = data['post_stream']['posts'][0]['raw'];
post['content'] = YAML.parse(raw)['content'];
posts.push(post);
}
catch(e) {
console.error(e)
}
}
}
let updateFile = () => {
jsonfile.writeFile(file, posts, function (err) {
console.error(err)
})
// * trigger GitHub Actions workflow API
fetch('https://api.github.com/repos/PDIS/web-jekyll/actions/workflows/github-pages-deploy.yml/dispatches', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/vnd.github.v3+json',
'Authorization': 'token ' + token
},
body: '{"ref": "master"}'
})
.then(res => {
if (res.status == 204) {
console.log('done')
} else {
console.log(res.statusText)
}
});
}
let gitcommit = () => {
let stringdata = JSON.stringify(posts);
ghrepo.contents('_data/tracks.json', function (err, data, headers) {
console.error("error: " + err);
if (typeof data == 'undefined' || typeof data === null) {
ghrepo.createContents('_data/tracks.json', 'update tracks.json', stringdata, function (err, data, headers) {
console.error("error: " + err);
console.error("data: " + JSON.stringify(data));
});
} else {
let sha = data.sha;
ghrepo.updateContents('_data/tracks.json', 'update tracks.json', stringdata, sha, function (err, data, headers) {
console.error("error: " + err);
});
}
});
}
// getIDs(more_url).then(getPosts).then(gitcommit)
getIDs(more_url).then(getPosts).then(updateFile)