-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.js
221 lines (199 loc) · 6.44 KB
/
common.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
const cheerio = require('cheerio');
const axios = require('axios');
const qs = require('qs');
const admin = require('firebase-admin');
const { NOTICE_URL, DB, BASE_URL, TOPIC, TELEGRAM_BOT_API_URL, TELEGRAM_GROUP_ID } = require("./config");
admin.initializeApp({
credential: admin.credential.applicationDefault(),
});
//matches if the given relative url is valid url of notice pdf file or not
//const matchNoticeUrl = val => /NoticePDF\/[0-9]+NOT[0-9]+.pdf/i.test(val) || /NoticePDF\/[0-9]+Notice[0-9]+.pdf/i.test(val)
const matchNoticeUrl = val => /NoticePDF\/.+\.pdf/i.test(val);
exports.matchNoticeUrl = matchNoticeUrl;
const getId = (relUrl) => relUrl.replace(/NOTICEPDF\//i,"").replace(/\.PDF/i,"");
exports.getId = getId;
const parseNotices = (body) => {
//cheerio parses the html and returns a jquery object
//to traverse and manipulate the DOM.
const $ = cheerio.load(body);
//This query returns list of all the <tr> child of <table>
//with id ctl00_ContentPlaceHolder1_GridView1
const trows = $('table#ctl00_ContentPlaceHolder1_GridView1 tr').toArray();
const notices = [];
for(let i = 0,tr,name,date; i < trows.length; i++){
tr = $(trows[i]);
//finds the child <a> of current <tr>
const relUrl = $(tr).find('a').attr('href');
//returns the text of <span> of the first <td> child of current <tr>
name = $((tr).find("td").get(0)).find('span').html();
//returns the text of <span> of the second <td> child of current <tr>
date = $($(tr).find("td").get(1)).find("span").html();
//if the relUrl is not a valid relative URL of notice PDF file
//Then, this tr does not wrap a notice data
if(matchNoticeUrl(relUrl)){
notices.push({
id: getId(relUrl),
url: BASE_URL+relUrl,
relUrl,
name,
date
});
}
}
return notices;
}
exports.parseNotices = parseNotices;
const extractFormData = (body,page) => {
const $ = cheerio.load(body);
const __VIEWSTATE = $("#__VIEWSTATE").attr("value");
const __EVENTVALIDATION = $("#__EVENTVALIDATION").attr("value");
const __VIEWSTATEGENERATOR = $("#__VIEWSTATEGENERATOR").attr("value");
return {
__EVENTTARGET: "ctl00$ContentPlaceHolder1$GridView1",
__EVENTARGUMENT: "Page$"+page,
__VIEWSTATE,
__VIEWSTATEGENERATOR,
__EVENTVALIDATION
}
}
exports.extractFormData = extractFormData;
//Reads data from the heritage website
const getData = async (page=1) => {
//creates a get request and returns the response
var config = {
method: 'get',
url: NOTICE_URL
};
const response = await axios(config);
if(page===1 || page === "1")
return {
notices: parseNotices(response.data)
}
else {
const j = parseInt(page);
let i,r;
for(i = 11, r = response ; i < j; i+=10){
const formData = extractFormData(r.data,i);
const data = qs.stringify(formData);
const config2 = {
method: "post",
url: NOTICE_URL,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
data : data
};
r = await axios(config2);
}
const formData = extractFormData(r.data,page);
const data = qs.stringify(formData);
const config2 = {
method: "post",
url: NOTICE_URL,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
data : data
};
const response2 = await axios(config2);
return {
notices: parseNotices(response2.data)
};
}
}
exports.getData = getData;
const saveNotice = async (notice) => {
await admin.firestore()
.collection(DB.NOTICES)
.doc(notice.id)
.set({
...notice,
recorded: admin.firestore.Timestamp.now()
});
}
exports.saveNotice = saveNotice;
const noticeExists = async (notice) => {
return (await admin.firestore()
.collection(DB.NOTICES)
.doc(notice.id)
.get()).exists;
}
exports.noticeExists = noticeExists;
const notifyToPushSubscribers = async notice => {
const response = await admin.messaging().send({
topic: TOPIC.NEW_NOTICE,
notification: {
title: "New Notice From HITK",
body: notice.name
},
webpush: {
fcmOptions: {
link: process.env.CLIENT_APP_URL
}
}
});
return response;
}
const notifyToTelegramGroup = async notice => {
const response = await axios({
method: "post",
url: TELEGRAM_BOT_API_URL+'/sendDocument',
headers: {
'Content-Type': 'application/json',
},
data : {
chat_id: TELEGRAM_GROUP_ID,
document: notice.url,
caption: notice.name
}
});
return response.data;
}
const notifyNotice = async (notice) => {
try {
const response = await notifyToPushSubscribers(notice);
console.log("Notice sent with message id ",response);
}
catch (error){
console.error('Error sending message:', error);
}
try {
const response = await notifyToTelegramGroup(notice);
console.log("Notice sent to ",response.result.chat.title, " [chat_id: "+response.result.chat.id+", Message id: "+response.result.message_id+"]");
}
catch (error){
console.error('Error sending message to telegram group:', error);
}
}
const recordNotices = async (page=1,notify=false) => {
const data = await getData(page);
const notices = data.notices;
let count = 0;
for(let i=notices.length-1; i >= 0; i--){
const notice = notices[i];
const exists = await noticeExists(notice);
if(!exists){
try {
await saveNotice(notice);
count++;
if(notify){
await notifyNotice(notice);
}
} catch(e){
console.error(e);
}
}
}
console.log("Total Notices Saved: ",count);
}
exports.recordNotices = recordNotices;
const subscribeClientToNewNotices = async (token) => {
try {
const response = await admin.messaging().subscribeToTopic(token,TOPIC.NEW_NOTICE);
console.log("Subscribed to new notice",response);
} catch(e) {
console.error(e);
throw e;
}
}
exports.subscribeClientToNewNotices = subscribeClientToNewNotices;