diff options
author | Xiao Pan <gky44px1999@gmail.com> | 2024-05-28 00:57:11 -0700 |
---|---|---|
committer | Xiao Pan <gky44px1999@gmail.com> | 2024-05-28 00:57:11 -0700 |
commit | 76d8c19870ae727dc2ac88dd5ab19818cc03d3e6 (patch) | |
tree | 47deac96df114278811b71e8f37a163651a63e93 /dynotify.c | |
parent | 28507c5f644e7b4737a389577dbd5a7443f538f9 (diff) |
add dynotify
Diffstat (limited to 'dynotify.c')
-rw-r--r-- | dynotify.c | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/dynotify.c b/dynotify.c new file mode 100644 index 0000000..9ee06ce --- /dev/null +++ b/dynotify.c @@ -0,0 +1,130 @@ +// curl: +// man curl_easy_init CURLOPT_URL curl_easy_setopt CURLOPT_WRITEFUNCTION +// https://curl.se/libcurl/ +// https://curl.se/libcurl/c/ +// https://curl.se/libcurl/c/libcurl-tutorial.html +// https://stackoverflow.com/a/2329792 + +// json-c: +// pacman -Qs json c +// https://github.com/json-c/json-c#using +// https://github.com/json-c/json-c/blob/master/tests/test2.c +// https://github.com/rbtylee/tutorial-jsonc/blob/master/tutorial/legacy.md +// https://github.com/rbtylee/tutorial-jsonc/blob/master/tutorial/parsing2.md + +// libnotify: +// https://gitlab.gnome.org/GNOME/libnotify/-/blob/master/tests/test-basic.c?ref_type=heads +// https://gnome.pages.gitlab.gnome.org/libnotify/index.html + +// compile, maybe wrong: +// https://stackoverflow.com/a/39120702 +// cc -Wall -lcurl -ljson-c $(pkg-config --cflags --libs libnotify) ./dynotify.c +// or: +// cc -Wall $(pkg-config --cflags --libs libnotify libcurl json-c) ./dynotify.c +// misc: +// /usr/lib/pkgconfig/libnotify.pc + +#include<stdlib.h> +#include<string.h> +#include<stdbool.h> +#include<unistd.h> +// MIT +#include<curl/curl.h> +// MIT +#include<json-c/json.h> +// LGPL2 +#include<libnotify/notify.h> + +#define DYROOM_ID_LEN 9 + +struct memory { + char *response; + size_t size; +}; + +struct dyroom { + // utf-8 most chinese characters 3 bytes, my longest msg currently is 5 chinese characters, 5*3=15, +1 for \0 + // but gcc will not error when use 15, maybe the complier auto add one more element? + const char msg[16]; + const char id[DYROOM_ID_LEN]; + bool up; +}; + +// /home/xyz/archive/programs/private_archive_codes/c/c_primer_plus/practices/Chapter14/14-1monthStruct.c +struct dyroom room[5]={ + {"阿冉开播","4605243",false}, + {"甜汤开播","2766480",false}, + {"少君开播","9640128",false}, + {"鸽子她开播","236288",false}, + {"郑正开播","10883525",false} +}; + +// copy from `man CURLOPT_WRITEFUNCTION` +static size_t cb(char *data, size_t size, size_t nmemb, void *clientp) +{ + size_t realsize = size * nmemb; + struct memory *mem = (struct memory *)clientp; + + char *ptr = realloc(mem->response, mem->size + realsize + 1); + if(!ptr) + return 0; /* out of memory! */ + + mem->response = ptr; + memcpy(&(mem->response[mem->size]), data, realsize); + mem->size += realsize; + mem->response[mem->size] = 0; + + return realsize; +} + +int main (void) +{ + // https://curl.se/libcurl/c/libcurl-errors.html + //CURLcode res; + // `man curl_easy_init`: "It is encouraged to reuse easy handles for repeated transfers.", so I choose to reuse it + CURL *curl = curl_easy_init(); + NotifyNotification *n; + notify_init ("dynotify"); + + if(curl) { + for(;;) + { + for(int i=0;i<5;i++) + { + // manpage assign 0 to struct, it seems because C11 when cast 0 to void* is same as NULL? see: + // https://www.geeksforgeeks.org/null-pointer-in-c/ + struct memory chunk = {0}; + char url[29+DYROOM_ID_LEN]="https://www.douyu.com/betard/"; + strcat(url,room[i].id); + + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cb); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); + //res = curl_easy_perform(curl); + curl_easy_perform(curl); + //printf("%s\n",chunk.response); + + if (json_object_get_int(json_object_object_get(json_object_object_get(json_tokener_parse(chunk.response),"room"),"show_status")) == 1) + { + if(!room[i].up) + { + n = notify_notification_new (room[i].msg, NULL, NULL); + notify_notification_set_urgency(n, NOTIFY_URGENCY_CRITICAL); + notify_notification_show (n, NULL); + + room[i].up=true; + } + } + else if(room[i].up) + room[i].up=false; + + free(chunk.response); + } + sleep(60); + } + curl_easy_cleanup(curl); + } + + g_object_unref (G_OBJECT (n)); + return 0; +} |