-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
156 lines (138 loc) · 4.87 KB
/
functions.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
const axios = require("axios");
const { JSDOM } = require("jsdom");
const fs = require("fs")
const objhash = require("object-hash")
const TIME_INTERVAL = 1000 * 60 * 5;
const URL = "https://ktu.edu.in/eu/core/announcements.htm";
// const URL = "https://www.google.com/"
const createObj = (dateElement, notificationElement) => {
const myObj = {}
dateElement.forEach((element, index) => {
const arr = element.textContent.split(" ")
const year = arr[5]
const month = arr[1]
const date = arr[2]
const link = "https://ktu.edu.in/eu/core/announcements.htm"
// const link = notificationElement[index].querySelector("a") ? notificationElement[index].querySelector("a").href : null
myObj[index] = {
link:link,
year,
month,
date,
notification:sanitizeString(notificationElement[index].textContent)
}
})
return myObj
}
const sanitizeString = (string1) => {
string1 = string1.replace(/(\r\n|\n|\r|\t|\t\n)/gm,"")
string1 = string1.split(" ")
newList = []
for(let i = 0; i < string1.length; i++){
if(string1[i] !== ""){
newList.push(string1[i])
}
}
return newList.join(" ")
}
const findNotification = (month, year, arrayOfElements) => {
const obj = []
for(const element in arrayOfElements){
if(arrayOfElements[element].month == month && arrayOfElements[element].year == year){
const object = arrayOfElements[element]
const sentence = {
title:`***${object.date} ${object.month} ${object.year}***`,
url:`https://ktu.edu.in/${object.link}`,
description:"```" + object.notification +"```"
}
obj.push(sentence)
}
}
return obj
}
const checkIfChanged = (hash) => {
const data = fs.readFileSync('hash.txt', 'utf8')
return data === hash ? false : true
}
const logXML = async (functionToBeExecuted) => {
console.log(`LOGXML STARTED .....`)
const response = await axios.get(URL)
const dom = new JSDOM(response.data)
const dateElement = dom.window.document.querySelectorAll("td[width = '9%'] b");
const notificationElement = dom.window.document.querySelectorAll("tr li");
const arrayOfElements = createObj(dateElement, notificationElement)
var datahash = objhash(arrayOfElements)
console.log(datahash)
var oldHash = fs.readFileSync('hash.txt', 'utf8')
if(oldHash == datahash){
console.log("Nothing Changed");
} else {
fs.writeFile('hash.txt',datahash, (err) => {
if(err) throw err
console.log("done")
})
var data = JSON.stringify(arrayOfElements)
fs.writeFile('data.json', data, function (err) {
if (err) throw err;
console.log('Saved!');
functionToBeExecuted(arrayOfElements)
});
}
// if(checkIfChanged(datahash)){
// fs.writeFile('hash.txt',datahash, (err) => {
// if(err) throw err
// console.log("done")
// })
// var data = JSON.stringify(arrayOfElements)
// fs.writeFile('data.json', data, function (err) {
// if (err) throw err;
// console.log('Saved!');
// functionToBeExecuted(arrayOfElements)
// });
// } else {
// console.log("Nothing Changed");
// }
};
const sendIntervalNotification = async (functionToBeExecuted) => {
console.log(`Searching....`);
const loopingFunction = () => logXML(functionToBeExecuted)
setInterval(loopingFunction, TIME_INTERVAL)
}
// const functionToBeExecuted = (data) => console.log(`Hello`);
// sendIntervalNotification(functionToBeExecuted)
const checkByDetails = async (month, year, functionToBeExecuted) => {
fs.readFile('data.json', 'utf8', (err, data) => {
let arrayOfElements = JSON.parse(data)
let obj = findNotification(month, year, arrayOfElements)
functionToBeExecuted(obj)
})
}
const getFirstTenObjects = (functionToBeExecuted) =>{
fs.readFile('data.json', 'utf8', (err, data) => {
let arrayOfElements = JSON.parse(data)
let arrayToBeSent = []
for(let element in arrayOfElements){
if (element < 10){
arrayToBeSent.push(arrayOfElements[element])
}
}
functionToBeExecuted(arrayToBeSent)
})
}
const getFirstNObjects = (numberOfElements, functionToBeExecuted) =>{
fs.readFile('data.json', 'utf8', (err, data) => {
let arrayOfElements = JSON.parse(data)
let arrayToBeSent = []
for(let element in arrayOfElements){
if (element < numberOfElements){
arrayToBeSent.push(arrayOfElements[element])
}
}
functionToBeExecuted(arrayToBeSent)
})
}
// checkByDetails("Aug","2020" )
module.exports = {sendIntervalNotification, checkByDetails, getFirstTenObjects, getFirstNObjects}
// setTimeout(() => {
// checkByDetails("Aug", "2020")
// }, 1000)