summaryrefslogtreecommitdiff
path: root/input_gadgets.c
diff options
context:
space:
mode:
authorErich Eckner <git@eckner.net>2018-10-22 14:19:25 +0200
committerErich Eckner <git@eckner.net>2018-10-22 14:19:25 +0200
commitae75b15e6241232c2adb3ae3b53819d3a4dd638d (patch)
tree4a4c738c2bfa8fff0ac2dbe0a90444120bb566c5 /input_gadgets.c
parent9cbc55a81c76dd3bfb480b9f280f9958ce6ef592 (diff)
downloadanzeige-ae75b15e6241232c2adb3ae3b53819d3a4dd638d.tar.xz
split of logical parts into separate files
Diffstat (limited to 'input_gadgets.c')
-rw-r--r--input_gadgets.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/input_gadgets.c b/input_gadgets.c
new file mode 100644
index 0000000..35dc766
--- /dev/null
+++ b/input_gadgets.c
@@ -0,0 +1,57 @@
+#include <string.h>
+#include <stdlib.h>
+#include <curl/curl.h>
+
+struct MemoryStruct {
+ char *memory;
+ size_t size;
+};
+
+static size_t
+WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
+{
+ size_t realsize = size * nmemb;
+ struct MemoryStruct *mem = (struct MemoryStruct *)userp;
+
+ char *ptr = realloc(mem->memory, mem->size + realsize + 1);
+ if(ptr == NULL) {
+ /* out of memory! */
+ printf("not enough memory (realloc returned NULL)\n");
+ return 0;
+ }
+
+ mem->memory = ptr;
+ memcpy(&(mem->memory[mem->size]), contents, realsize);
+ mem->size += realsize;
+ mem->memory[mem->size] = 0;
+
+ return realsize;
+}
+
+int gadgets_retrieve_temperature() {
+ CURL *curl_handle;
+ CURLcode res;
+ struct MemoryStruct chunk;
+
+ chunk.memory = malloc(1);
+ chunk.size = 0;
+
+ curl_global_init(CURL_GLOBAL_ALL);
+
+ curl_handle = curl_easy_init();
+ if (!curl_handle) {
+ perror("Failed to init curl");
+ return -1;
+ }
+ 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);
+ curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
+ 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);
+ return 0;
+}