-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
202 lines (184 loc) · 6.68 KB
/
index.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
const https = require('https')
const Discord = require('discord.js')
const schedule = require('node-schedule')
const cheerio = require('cheerio')
const client = new Discord.Client();
const fs = require('fs');
client.on('ready', () => {
console.log(`Logged in succesfully as ${client.user.tag}`);
var today = new Date(Date.now());
fillQuests(today);
})
/**
* Gets from an https url
* @param {String} url Url to get (https)
* @param {Function(String)} callback Function to call with data
* @param {Function(String, String)} [error=()=>{throw `error getting ${url}`}] Error callback
*/
function getData(url, callback, error) {
https.get(url, (res) => {
var rawData = '';
res.on('data', (chunk) => {
rawData += chunk;
});
res.on('end', () => {
callback();
});
res.on('error', () => {
if (error) {
error(url, rawData);
} else {
throw `error getting ${url}`;
}
});
})
}
/**
*
* @param {Date} startDate Start date
*/
function fillQuests(startDate) {
getData(
'https://pso2.com/news/urgent-quests',
(rawData) => {
const $ = cheerio.load(rawData)
page = $("li.sr:nth-child(1) > div:nth-child(2) > div:nth-child(3) > a:nth-child(4)").attr('onclick').replace(/\(ShowDetails\('/gi, '').replace(/'.*/gi, '')
url = "https://pso2.com/news/urgent-quests/" + page
console.log(url)
getData(
url,
(rawData) => {
const $ = cheerio.load(rawData)
var iframes = $("iframe")
var src = ''
for (let i = 0; i < iframes.length; i++) {
const iframe = iframes[i];
if (iframe.attribs['src'].includes("calendar.google.com")) {
src = iframe.attribs['src']
break
}
}
if (src == '') {
throw 'calendar not found'
}
src = src.replace(/.*&src=/gi, '').replace(/&.*/gi, '')
var calendarId = Buffer.from(src, 'base64');
var endDate = new Date(startDate.getTime())
endDate.setMonth(endDate.getMonth() + 1)
getData(
`https://clients6.google.com/calendar/v3/calendars/pso2.schedule@gmail.com/events?calendarId=${calendarId}&singleEvents=true&timeZone=Europe%2FMadrid&maxAttendees=1&maxResults=250&sanitizeHtml=true&timeMin=${startDate.getFullYear()}-${startDate.getMonth() + 1}-${startDate.getDate()}T00%3A00%3A00%2B02%3A00&timeMax=${endDate.getFullYear()}-${endDate.getMonth() + 1}-${endDate.getDate()}T00%3A00%3A00%2B02%3A00&key=AIzaSyBNlYH01_9Hc5S1J9vuFmu2nUqBZJNAXxs`,
(rawData) => {
schheduleCalendar(rawData);
});
});
});
}
/**
*
* @param {String} txt
*/
function schheduleCalendar(txt) {
fs.writeFile('./res.json', rawData, 'utf8')
const cal = JSON.parse(rawData);
var i = 0;
for (i = 0; i < cal.items.length; i++) {
const event = cal.items[i];
var date = new Date(Date.parse(event.start.dateTime));
var remind = new Date(date.getTime()).setMinutes(date.getMinutes() - 30);
if (event["description"]) {
var description = String(event["description"])
.replace(/<[^>]*>|\n|\r|\ \ /gi, (x) => {
switch (x[1]) {
case '/':
switch (x[2]) {
case 'p':
return '\n';
case 'b':
return '**';
case 'a':
return '';
default:
return x;
}
case 'a':
case 'p':
return '';
case 'b':
switch (x[2]) {
case 'r':
return '\n';
default:
return '**';
}
case '\n':
case '\r':
return '';
default:
return x;
}
});
description = description.replace(/\s\s|[\n|\s]+$/gi, '');
console.log((i + 1) + " " + description);
schedule.scheduleJob(remind, () => {
sendquest(date, event["summary"], description);
});
} else {
console.log((i + 1) + " NO DESCRIPTION");
schedule.scheduleJob(remind, () => {
sendquest(date, event["summary"]);
});
}
}
console.log(`Scheduled ${i} events`);
var resetTime = Date.parse(cal.items[cal.items.length - 1].end.dateTime);
schedule.scheduleJob(resetTime, () => {
fillQuests(resetTime);
});
console.log(`Will fetch calendar again at ${cal.items[cal.items.length - 1].end.dateTime}`);
}
/**
*
* @param {Date} date
* @returns {String}
*/
function emojiHour(date) {
const emojiConvert = {
0: '0️⃣',
1: '1️⃣',
2: '2️⃣',
3: '3️⃣',
4: '4️⃣',
5: '5️⃣',
6: '6️⃣',
7: '7️⃣',
8: '8️⃣',
9: '9️⃣'
};
const hour = date.getHours();
const minute = date.getMinutes();
return `${emojiConvert[parseInt(hour / 10)]}${emojiConvert[hour % 10]}💠${emojiConvert[parseInt(minute / 10)]}${emojiConvert[minute % 10]}`;
}
/**
*
* @param {Date} date
* @param {String} title
* @param {String} [description='']
*/
function sendquest(date, title, description) {
if (description) {
description = `\n${description}`;
} else {
description = '';
}
client.channels.fetch(process.env.channelId).then((channel) => {
channel.send(`${emojiHour(date)}\n**${title}**${description}`)
.then(() => {
console.log(`sent event ${title}`)
})
.catch(console.error);
}).catch(console.error)
}
client.login(process.env.BOT_TOKEN)
.catch(() => {
console.error("Error login in, check secrethub");
});