From 94c45e3c4d185b3f0d70f0d2d761b72c6561e1b5 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Tue, 14 Jul 2020 06:55:18 +0200 Subject: Implement add_html --- src/rss.c | 174 ++++++-------------------------------------------------------- 1 file changed, 16 insertions(+), 158 deletions(-) (limited to 'src/rss.c') diff --git a/src/rss.c b/src/rss.c index 23ccf89..84fa345 100644 --- a/src/rss.c +++ b/src/rss.c @@ -3,12 +3,12 @@ #include "stringutils.h" #include "fileutils.h" #include "buffer.h" +#include "rss_html_common.h" #include #include #include #include #include -#include "json.h" static int is_alpha_lowercase(char c) { return c >= 'a' && c <= 'z'; @@ -163,150 +163,14 @@ static int rss_parse_callback(char *title, char *link, void *userdata) { return 0; } -static void create_json_string(struct json_string_s *json_result, const char *str, int len) { - json_result->string = str; - json_result->string_size = len; -} - -static void init_json_value_str(struct json_value_s *json_value, struct json_string_s *json_str) { - json_value->payload = json_str; - json_value->type = json_type_string; -} - -static int write_rss_json_to_file(const char *dir, const char *filename, const char *url, const char *updated, const char *start_after, const char *start_after_url) { - struct json_string_s title_json_key; - create_json_string(&title_json_key, "title", 5); - - struct json_string_s title_json_value_str; - create_json_string(&title_json_value_str, start_after, start_after ? strlen(start_after) : 0); - struct json_value_s title_json_value; - init_json_value_str(&title_json_value, &title_json_value_str); - - struct json_string_s time_json_key; - create_json_string(&time_json_key, "time", 4); - - struct json_string_s time_value_str; - create_json_string(&time_value_str, updated, updated ? strlen(updated) : 0); - struct json_value_s time_json_value; - init_json_value_str(&time_json_value, &time_value_str); - - struct json_string_s url_json_key; - create_json_string(&url_json_key, "url", 3); - - struct json_string_s url_value_str; - create_json_string(&url_value_str, start_after_url, start_after_url ? strlen(start_after_url) : 0); - struct json_value_s url_json_value; - init_json_value_str(&url_json_value, &url_value_str); - - struct json_string_s link_json_key; - create_json_string(&link_json_key, "link", 4); - - struct json_string_s link_json_value_str; - create_json_string(&link_json_value_str, url, strlen(url)); - struct json_value_s link_json_value; - init_json_value_str(&link_json_value, &link_json_value_str); - - struct json_string_s updated_json_key; - create_json_string(&updated_json_key, "updated", 7); - - struct json_string_s updated_json_value_str; - create_json_string(&updated_json_value_str, updated, strlen(updated)); - struct json_value_s updated_json_value; - init_json_value_str(&updated_json_value, &updated_json_value_str); - - struct json_string_s downloaded_json_key; - create_json_string(&downloaded_json_key, "downloaded", 10); - - struct json_object_s downloaded_json; - downloaded_json.start = NULL; - downloaded_json.length = 0; - - struct json_value_s downloaded_json_value; - downloaded_json_value.payload = &downloaded_json; - downloaded_json_value.type = json_type_object; - - struct json_object_element_s downloaded_title_element; - downloaded_title_element.name = &title_json_key; - downloaded_title_element.value = &title_json_value; - - struct json_object_element_s downloaded_time_element; - downloaded_time_element.name = &time_json_key; - downloaded_time_element.value = &time_json_value; - - struct json_object_element_s downloaded_url_element; - downloaded_url_element.name = &url_json_key; - downloaded_url_element.value = &url_json_value; - - downloaded_title_element.next = &downloaded_time_element; - downloaded_time_element.next = &downloaded_url_element; - downloaded_url_element.next = NULL; - - struct json_array_s downloaded_json_array; - downloaded_json_array.start = NULL; - downloaded_json_array.length = 0; - struct json_array_element_s downloaded_json_array_element; - - if(start_after) { - downloaded_json.start = &downloaded_title_element; - downloaded_json.length = 3; - - downloaded_json_array_element.value = &downloaded_json_value; - downloaded_json_array_element.next = NULL; - downloaded_json_array.start = &downloaded_json_array_element; - downloaded_json_array.length = 1; - } - - struct json_value_s downloaded_json_array_value; - downloaded_json_array_value.payload = &downloaded_json_array; - downloaded_json_array_value.type = json_type_array; - - struct json_object_s json_root; - json_root.length = 3; - - struct json_object_element_s link_element; - link_element.name = &link_json_key; - link_element.value = &link_json_value; - - struct json_object_element_s updated_element; - updated_element.name = &updated_json_key; - updated_element.value = &updated_json_value; - - struct json_object_element_s downloaded_element; - downloaded_element.name = &downloaded_json_key; - downloaded_element.value = &downloaded_json_array_value; - - link_element.next = &updated_element; - updated_element.next = &downloaded_element; - downloaded_element.next = NULL; - json_root.start = &link_element; - - struct json_value_s json_root_value; - json_root_value.payload = &json_root; - json_root_value.type = json_type_object; - size_t json_body_size = 0; - char *json_body_str = json_write_pretty(&json_root_value, " ", "\n", &json_body_size); - if(!json_body_str || json_body_size == 0) { - fprintf(stderr, "Failed to write json data to rss file %s/%s\n", dir, filename); - return -1; - } - - int result = file_overwrite_in_dir(dir, filename, json_body_str, json_body_size - 1); - free(json_body_str); - return result; -} - int add_rss(const char *name, const char *url, char *rss_config_dir, const char *start_after) { - (void)name; - (void)rss_config_dir; - (void)start_after; int result = 0; Buffer buffer; buffer_init(&buffer); - int res = download_to_buffer(url, &buffer); - if(res != 0) { + result = download_to_buffer(url, &buffer); + if(result != 0) { fprintf(stderr, "Failed to download rss: %s\n", url); - result = res; goto cleanup; } @@ -317,10 +181,9 @@ int add_rss(const char *name, const char *url, char *rss_config_dir, const char /* TODO: What if rss title is longer than this? */ char rss_title[250]; - res = parse_rss(buffer.data, rss_title, sizeof(rss_title), rss_parse_callback, &rss_parse_userdata); - if(res != 0) { + result = parse_rss(buffer.data, rss_title, sizeof(rss_title), rss_parse_callback, &rss_parse_userdata); + if(result != 0) { fprintf(stderr, "Failed to parse rss for url: %s\n", url); - result = res; goto cleanup; } @@ -354,10 +217,9 @@ int add_rss(const char *name, const char *url, char *rss_config_dir, const char goto cleanup; } - res = create_directory_recursive(rss_tracked_dir); - if(res != 0) { - fprintf(stderr, "Failed to create %s, error: %s\n", rss_tracked_dir, strerror(res)); - result = res; + result = create_directory_recursive(rss_tracked_dir); + if(result != 0) { + fprintf(stderr, "Failed to create %s, error: %s\n", rss_tracked_dir, strerror(result)); goto cleanup; } @@ -368,36 +230,32 @@ int add_rss(const char *name, const char *url, char *rss_config_dir, const char char in_progress_filepath[PATH_MAX]; strcpy(in_progress_filepath, rss_tracked_dir); strcat(in_progress_filepath, "/.in_progress"); - res = create_lock_file(in_progress_filepath); - if(res != 0) { + result = create_lock_file(in_progress_filepath); + if(result != 0) { fprintf(stderr, "Failed to create %s/.in_progress\n", rss_tracked_dir); - result = res; goto cleanup; } - res = file_overwrite_in_dir(rss_tracked_dir, "link", url, strlen(url)); - if(res != 0) { + result = file_overwrite_in_dir(rss_tracked_dir, "link", url, strlen(url)); + if(result != 0) { fprintf(stderr, "Failed to create %s/link\n", rss_tracked_dir); remove(rss_tracked_dir); - result = res; goto cleanup; } char updated[32]; sprintf(updated, "%ld", time(NULL)); - res = file_overwrite_in_dir(rss_tracked_dir, "updated", updated, strlen(updated)); - if(res != 0) { + result = file_overwrite_in_dir(rss_tracked_dir, "updated", updated, strlen(updated)); + if(result != 0) { fprintf(stderr, "Failed to create %s/updated\n", rss_tracked_dir); remove(rss_tracked_dir); - result = res; goto cleanup; } - res = write_rss_json_to_file(rss_tracked_dir, "data", url, updated, start_after, rss_parse_userdata.start_after_url); - if(res != 0) { + result = write_plugin_json_to_file(rss_tracked_dir, "data", url, updated, start_after, rss_parse_userdata.start_after_url, NULL); + if(result != 0) { fprintf(stderr, "Failed to create %s/data\n", rss_tracked_dir); remove(rss_tracked_dir); - result = res; goto cleanup; } -- cgit v1.2.3