Updated text's parsing (losungen.de changed its way of distributing files / the "API" design\!)
This commit is contained in:
parent
2b56d05cb6
commit
65dad99900
@ -1,27 +1,31 @@
|
||||
#include <HTTPClient.h>
|
||||
|
||||
String textBeginString = "<b>";
|
||||
String textEndString = "</b><br>";
|
||||
String sourceBeginString = textEndString; // because source begins where text ends
|
||||
String sourceEndString = "</font></p>";
|
||||
String textBeginString = "@";
|
||||
String textEndString = "@";
|
||||
String lehrTextBeginString = "@@";
|
||||
String sourceBeginString = "@";
|
||||
String sourceEndString = "@";
|
||||
String url = "https://www.losungen.de/fileadmin/media-losungen/kalender/kalender_daten.php";
|
||||
|
||||
String getHTML(String url) {
|
||||
String getHTML(String url, String post_params) {
|
||||
String result = "";
|
||||
if(WiFi.status() == WL_CONNECTED) {
|
||||
HTTPClient http;
|
||||
http.begin(url); // initialize the http instance
|
||||
int httpCode = http.GET(); // get the page; httpCode will be negative on error
|
||||
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); // add the header; needed for the POST request
|
||||
Serial.printf("[HTTP] POST %s | DATA: %s\n", url.c_str(), post_params.c_str());
|
||||
int httpCode = http.POST(post_params); // get the page; httpCode will be negative on error
|
||||
|
||||
if(httpCode > 0) {
|
||||
// HTTP header has been send and Server response header has been handled
|
||||
Serial.printf("[HTTP] GET %d: %s\n", httpCode, url.c_str());
|
||||
Serial.printf("[HTTP] POST %d: %s\n", httpCode, url.c_str());
|
||||
|
||||
// file found at server
|
||||
if(httpCode == HTTP_CODE_OK) {
|
||||
result = http.getString();
|
||||
}
|
||||
} else {
|
||||
Serial.printf("[HTTP] GET: %s\n", http.errorToString(httpCode).c_str());
|
||||
Serial.printf("[HTTP] POST: %s\n", http.errorToString(httpCode).c_str());
|
||||
}
|
||||
|
||||
http.end();
|
||||
@ -39,40 +43,41 @@ String getHTML() { // gets the date and year from time.h library
|
||||
return "";
|
||||
}
|
||||
strftime(dayInMonth,3, "%d", &timeinfo);
|
||||
strftime(month,3, "%m", &timeinfo);
|
||||
sprintf(month,"%d",timeinfo.tm_mon); // the month number has to be in range 0-11!
|
||||
strftime(year,5, "%Y", &timeinfo);
|
||||
|
||||
return getHTML(String("https://www.losungen.de/fileadmin/media-losungen/heute/") + String(year) + String("/") + String(month) + String(dayInMonth) + String(".html"));
|
||||
String post_params = "jahr=" + String(year) + "&monat=" + String(month) + "&tag=" + String(dayInMonth); // create post parameters of the form "jahr=2023&monat=2&tag=2"
|
||||
return getHTML(url, post_params);
|
||||
}
|
||||
|
||||
String getLosungFromHTML(String html) {
|
||||
int losungBeginIndex = html.indexOf(textBeginString)+textBeginString.length();
|
||||
int losungEndIndex = html.indexOf(textEndString);
|
||||
int losungEndIndex = html.indexOf(textEndString,losungBeginIndex);
|
||||
|
||||
return html.substring(losungBeginIndex, losungEndIndex);
|
||||
}
|
||||
|
||||
String getLehrtextFromHTML(String html) {
|
||||
String getLosungSourceFromHTML(String html) { // get the source of losung in given html
|
||||
String losung = getLosungFromHTML(html);
|
||||
int losungIndex = html.indexOf(losung);
|
||||
int lehrtextBeginIndex = html.indexOf(textBeginString, losungIndex)+textBeginString.length();
|
||||
int lehrtextEndIndex = html.indexOf(textEndString, losungIndex+losung.length()+textEndString.length()); // calculating is needed for better performance and to get the second index, not the first
|
||||
|
||||
return html.substring(lehrtextBeginIndex, lehrtextEndIndex);
|
||||
}
|
||||
|
||||
String getLosungSourceFromHTML(String html) { // get the source of losung in given html
|
||||
int losungSourceBeginIndex = html.indexOf(sourceBeginString)+sourceBeginString.length();
|
||||
int losungSourceEndIndex = html.indexOf(sourceEndString);
|
||||
int losungSourceBeginIndex = html.indexOf(sourceBeginString, losungIndex)+sourceBeginString.length();
|
||||
int losungSourceEndIndex = html.indexOf(sourceEndString, losungIndex+losung.length()+sourceBeginString.length());
|
||||
|
||||
return html.substring(losungSourceBeginIndex, losungSourceEndIndex);
|
||||
}
|
||||
|
||||
String getLehrtextFromHTML(String html) {
|
||||
int lehrtextBeginIndex = html.indexOf(lehrTextBeginString)+lehrTextBeginString.length();
|
||||
int lehrtextEndIndex = html.indexOf(textEndString, lehrtextBeginIndex); // calculating is needed for better performance and to get the next index, not the current/previous
|
||||
|
||||
return html.substring(lehrtextBeginIndex, lehrtextEndIndex);
|
||||
}
|
||||
|
||||
String getLehrtextSourceFromHTML(String html) { // get the source of losung in given html
|
||||
String losungSource = getLosungSourceFromHTML(html);
|
||||
int losungSourceIndex = html.indexOf(losungSource);
|
||||
int lehrtextSourceBeginIndex = html.indexOf(sourceBeginString, losungSourceIndex)+sourceBeginString.length();
|
||||
int lehrtextSourceEndIndex = html.indexOf(sourceEndString, losungSourceIndex+losungSource.length()+sourceEndString.length()); // calculating is needed for better performance and to get the second index, not the first
|
||||
String lehrtext = getLehrtextFromHTML(html);
|
||||
int lehrtextIndex = html.indexOf(lehrtext);
|
||||
int lehrtextSourceBeginIndex = html.indexOf(sourceBeginString, lehrtextIndex)+sourceBeginString.length();
|
||||
int lehrtextSourceEndIndex = html.indexOf(sourceEndString, lehrtextSourceBeginIndex); // calculating is needed for better performance and to get the next index, not the current/previous
|
||||
|
||||
return html.substring(lehrtextSourceBeginIndex, lehrtextSourceEndIndex);
|
||||
}
|
Loading…
Reference in New Issue
Block a user