summaryrefslogtreecommitdiff
path: root/input_gadgets.c
diff options
context:
space:
mode:
authorErich Eckner <git@eckner.net>2018-10-22 14:52:02 +0200
committerErich Eckner <git@eckner.net>2018-10-23 05:46:56 +0200
commitec110edd798c2e0f0d2fc4808678138ce9597bcb (patch)
tree0377f0145a17ebda93b841e0b95fdfa3792ac920 /input_gadgets.c
parentd2f0f303822bb6200b6a17d6b9364909f059ad9c (diff)
downloadanzeige-ec110edd798c2e0f0d2fc4808678138ce9597bcb.tar.xz
kann jetzt Temperatur empfangen
Diffstat (limited to 'input_gadgets.c')
-rw-r--r--input_gadgets.c40
1 files changed, 31 insertions, 9 deletions
diff --git a/input_gadgets.c b/input_gadgets.c
index 35dc766..3bb7113 100644
--- a/input_gadgets.c
+++ b/input_gadgets.c
@@ -1,11 +1,11 @@
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>
+#include <regex.h>
-struct MemoryStruct {
- char *memory;
- size_t size;
-};
+#include "input_gadgets.h"
+
+#define temperatur_regex ">Temperatur</td>\\s*\n\\s*<td [^>]*><strong>([^<]*)</strong>"
static size_t
WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
@@ -28,10 +28,12 @@ WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
return realsize;
}
-int gadgets_retrieve_temperature() {
+int gadgets_retrieve_temperature()
+{
CURL *curl_handle;
CURLcode res;
struct MemoryStruct chunk;
+ int ret_val;
chunk.memory = malloc(1);
chunk.size = 0;
@@ -41,7 +43,7 @@ int gadgets_retrieve_temperature() {
curl_handle = curl_easy_init();
if (!curl_handle) {
perror("Failed to init curl");
- return -1;
+ return EXIT_FAILURE;
}
curl_easy_setopt(curl_handle, CURLOPT_URL, "https://wetter.mb.fh-jena.de/station/datenbank/php_giese/online.php");
curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
@@ -49,9 +51,29 @@ int gadgets_retrieve_temperature() {
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
res = curl_easy_perform(curl_handle);
- if (res != CURLE_OK)
- fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
curl_easy_cleanup(curl_handle);
- printf("%lu bytes retrieved\n", (unsigned long)chunk.size);
+ if (res != CURLE_OK) {
+ fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
+ return EXIT_FAILURE;
+ }
+
+ regex_t re;
+ regmatch_t rm[2];
+ if (regcomp(&re, temperatur_regex, REG_EXTENDED|REG_NEWLINE) != 0)
+ {
+ fprintf(stderr, "Failed to compile regex '%s'\n", temperatur_regex);
+ return EXIT_FAILURE;
+ }
+ if (ret_val = regexec(&re, chunk.memory, 2, rm, 0))
+ {
+ char *reg_err;
+ reg_err = malloc(1024);
+ regerror(ret_val, &re, reg_err, 1024);
+ printf("%d %s\n",ret_val,reg_err);
+ regfree(&re);
+ return -1;
+ }
+ regfree(&re);
+ printf("Text: <<%.*s>>\n", (int)(rm[1].rm_eo - rm[1].rm_so), chunk.memory + rm[1].rm_so);
return 0;
}