2022-08-21 16:54:42 +00:00
# include <HTTPClient.h>
2023-05-29 15:52:23 +00:00
String textBeginString = " @ " ;
String textEndString = " @ " ;
String lehrTextBeginString = " @@ " ;
String sourceBeginString = " @ " ;
String sourceEndString = " @ " ;
String url = " https://www.losungen.de/fileadmin/media-losungen/kalender/kalender_daten.php " ;
2022-08-21 16:54:42 +00:00
2023-05-29 15:52:23 +00:00
String getHTML ( String url , String post_params ) {
2022-08-21 16:54:42 +00:00
String result = " " ;
if ( WiFi . status ( ) = = WL_CONNECTED ) {
HTTPClient http ;
http . begin ( url ) ; // initialize the http instance
2023-05-29 15:52:23 +00:00
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
2022-08-21 16:54:42 +00:00
if ( httpCode > 0 ) {
// HTTP header has been send and Server response header has been handled
2023-05-29 15:52:23 +00:00
Serial . printf ( " [HTTP] POST %d: %s \n " , httpCode , url . c_str ( ) ) ;
2022-08-21 16:54:42 +00:00
// file found at server
if ( httpCode = = HTTP_CODE_OK ) {
result = http . getString ( ) ;
}
} else {
2023-05-29 15:52:23 +00:00
Serial . printf ( " [HTTP] POST: %s \n " , http . errorToString ( httpCode ) . c_str ( ) ) ;
2022-08-21 16:54:42 +00:00
}
http . end ( ) ;
}
return result ;
}
2022-08-21 17:21:56 +00:00
String getHTML ( ) { // gets the date and year from time.h library
struct tm timeinfo ;
char dayInMonth [ 3 ] ;
char month [ 3 ] ;
char year [ 5 ] ;
if ( ! getLocalTime ( & timeinfo ) ) {
Serial . println ( " Failed to obtain time " ) ;
return " " ;
}
strftime ( dayInMonth , 3 , " %d " , & timeinfo ) ;
2023-05-29 15:52:23 +00:00
sprintf ( month , " %d " , timeinfo . tm_mon ) ; // the month number has to be in range 0-11!
2022-08-21 17:21:56 +00:00
strftime ( year , 5 , " %Y " , & timeinfo ) ;
2023-05-29 15:52:23 +00:00
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 ) ;
2022-08-21 17:21:56 +00:00
}
2022-08-21 16:54:42 +00:00
String getLosungFromHTML ( String html ) {
int losungBeginIndex = html . indexOf ( textBeginString ) + textBeginString . length ( ) ;
2023-05-29 15:52:23 +00:00
int losungEndIndex = html . indexOf ( textEndString , losungBeginIndex ) ;
2022-08-21 16:54:42 +00:00
return html . substring ( losungBeginIndex , losungEndIndex ) ;
}
2023-05-29 15:52:23 +00:00
String getLosungSourceFromHTML ( String html ) { // get the source of losung in given html
2022-08-21 16:54:42 +00:00
String losung = getLosungFromHTML ( html ) ;
int losungIndex = html . indexOf ( losung ) ;
2023-05-29 15:52:23 +00:00
int losungSourceBeginIndex = html . indexOf ( sourceBeginString , losungIndex ) + sourceBeginString . length ( ) ;
int losungSourceEndIndex = html . indexOf ( sourceEndString , losungIndex + losung . length ( ) + sourceBeginString . length ( ) ) ;
2022-08-21 16:54:42 +00:00
2023-05-29 15:52:23 +00:00
return html . substring ( losungSourceBeginIndex , losungSourceEndIndex ) ;
2022-08-21 16:54:42 +00:00
}
2023-05-29 15:52:23 +00:00
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
2022-08-21 16:54:42 +00:00
2023-05-29 15:52:23 +00:00
return html . substring ( lehrtextBeginIndex , lehrtextEndIndex ) ;
2022-08-21 16:54:42 +00:00
}
String getLehrtextSourceFromHTML ( String html ) { // get the source of losung in given html
2023-05-29 15:52:23 +00:00
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
2022-08-21 16:54:42 +00:00
return html . substring ( lehrtextSourceBeginIndex , lehrtextSourceEndIndex ) ;
}