Repository navigation
Expand file tree
/
Copy pathcode
More file actions
229 lines (168 loc) · 5.28 KB
/
Copy pathcode
File metadata and controls
229 lines (168 loc) · 5.28 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
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
222
223
224
225
226
227
228
229
#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
#include <time.h>
#include "esp_sleep.h"
// ================= WIFI =================
const char* ssid = "HUAWEI-2.4G-Mfy4";
const char* password = "VabeC8a3";
// ================= URL =================
const char* url =
"https://urniki.easistent.com/urniki/5a42718f4a77f1f87d5f03d0634b3c6eaef7312c/razredi/679032/dijak/9032618";
#define uS_TO_S 1000000ULL
// ================= TIME =================
String getTodayDate() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) return "";
char buf[11];
strftime(buf, sizeof(buf), "%Y-%m-%d", &timeinfo);
return String(buf);
}
// ================= SCRAPER =================
void fetchTodaySubjects(String date) {
WiFiClientSecure client;
client.setInsecure();
client.setTimeout(15);
HTTPClient http;
http.begin(client, url);
http.setTimeout(20000);
http.addHeader("User-Agent", "Mozilla/5.0");
http.addHeader("Accept", "*/*");
http.addHeader("Cache-Control", "no-cache");
int httpCode = http.GET();
Serial.println("HTTP code: " + String(httpCode));
if (httpCode <= 0) {
Serial.println("HTTP failed");
http.end();
return;
}
WiFiClient* stream = http.getStreamPtr();
if (!stream) {
Serial.println("Stream null");
http.end();
return;
}
String buf = "";
buf.reserve(32768);
int subjectCount = 0;
bool headerPrinted = false;
while (http.connected() || stream->available()) {
while (stream->available()) {
buf += (char)stream->read();
if (buf.length() > 32000) buf = buf.substring(16000);
}
String searchTd = "ednevnik-seznam_ur_teden-td-";
int pos = 0;
while ((pos = buf.indexOf(searchTd, pos)) != -1) {
int dashAfterNum = buf.indexOf("-", pos + searchTd.length());
if (dashAfterNum == -1) { pos++; continue; }
String afterPrefix = buf.substring(pos + searchTd.length(), dashAfterNum);
String dateInId = buf.substring(dashAfterNum + 1, dashAfterNum + 11);
if (dateInId != date) { pos++; continue; }
int tdEnd = buf.indexOf("</td>", pos);
if (tdEnd == -1) break;
String cell = buf.substring(pos, tdEnd);
if (cell.indexOf("class=\"hidden") != -1) {
buf = buf.substring(tdEnd + 5);
pos = 0;
continue;
}
int periodNum = afterPrefix.toInt();
String periodLabel = (periodNum == 0) ? "Predura" : String(periodNum) + ". ura";
String subject = "";
int titlePos = cell.indexOf("ednevnik-title");
while (titlePos != -1) {
int spanPos = cell.indexOf("<span", titlePos);
if (spanPos == -1) break;
int textStart = cell.indexOf(">", spanPos) + 1;
int textEnd = cell.indexOf("<", textStart);
if (textStart > 0 && textEnd > textStart) {
String text = cell.substring(textStart, textEnd);
text.trim();
if (text.length() > 0 && text.length() <= 6) {
subject = text;
break;
}
}
titlePos = cell.indexOf("ednevnik-title", titlePos + 1);
}
String status = "";
if (cell.indexOf("odpadla") != -1) status = " (ODPADLA)";
else if (cell.indexOf("nadomescanje") != -1) status = " (NADOMESCANJE)";
if (subject.length() > 0) {
if (!headerPrinted) {
Serial.println("\n--- TODAY (" + date + ") ---");
headerPrinted = true;
}
Serial.println(periodLabel + ": " + subject + status);
subjectCount++;
}
buf = buf.substring(tdEnd + 5);
pos = 0;
}
}
http.end();
Serial.println("\n--- DONE (" + String(subjectCount) + ") ---");
}
// ================= UPDATE =================
void updateSchedule() {
String today = getTodayDate();
if (today == "") {
Serial.println("No time yet!");
return;
}
fetchTodaySubjects(today);
}
// ================= WIFI =================
void connectWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi OK");
}
// ================= TIME SYNC =================
void syncTime() {
configTime(3600, 3600, "pool.ntp.org");
Serial.print("Syncing time");
struct tm timeinfo;
while (!getLocalTime(&timeinfo)) {
delay(500);
Serial.print(".");
}
Serial.println("\nTime OK");
}
// ================= SLEEP UNTIL 09:00 =================
void goToSleep() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Time fail, sleeping 1h");
esp_sleep_enable_timer_wakeup(3600ULL * uS_TO_S);
esp_deep_sleep_start();
}
int nowSeconds = timeinfo.tm_hour * 3600 +
timeinfo.tm_min * 60 +
timeinfo.tm_sec;
int targetSeconds = 7 * 3600; // 09:00
int sleepSeconds = targetSeconds - nowSeconds;
if (sleepSeconds <= 0) {
sleepSeconds += 24 * 3600;
}
Serial.println("Sleeping for " + String(sleepSeconds) + " seconds");
esp_sleep_enable_timer_wakeup((uint64_t)sleepSeconds * uS_TO_S);
esp_deep_sleep_start();
}
// ================= SETUP =================
void setup() {
Serial.begin(115200);
delay(500);
Serial.println("\nBOOT");
connectWiFi();
syncTime();
Serial.println("Running update...");
updateSchedule();
goToSleep();
}
// ================= LOOP =================
void loop() {}