diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-06-15 09:28:35 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-06-15 09:28:46 +0200 |
commit | fee89c4afdde4dacee51a763bc4d931320a9d69d (patch) | |
tree | 66a0accb86934e76ad245d6e57dc4a3fb6cfd63f /src/rss_html_common.c | |
parent | 3757fe80b30801119bda41ab0445e915271129fc (diff) |
Add all items starting at start-after to the download list in the data json file. This makes downloading more robust if title/url is changes for any item
Diffstat (limited to 'src/rss_html_common.c')
-rw-r--r-- | src/rss_html_common.c | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/src/rss_html_common.c b/src/rss_html_common.c index c72273c..567671e 100644 --- a/src/rss_html_common.c +++ b/src/rss_html_common.c @@ -7,7 +7,7 @@ #include <time.h> #include <assert.h> -int write_plugin_json_to_file(const char *dir, const char *filename, const char *url, const char *updated, const char *start_after, const char *start_after_url, const char *plugin_name) { +int write_plugin_json_to_file(const char *dir, const char *filename, const char *url, const char *updated, DownloadItemsData *prev_download_items, size_t num_prev_download_items, const char *plugin_name) { int result = 0; cJSON *json_body = cJSON_CreateObject(); @@ -27,17 +27,20 @@ int write_plugin_json_to_file(const char *dir, const char *filename, const char goto cleanup; } - if(start_after) { + time_t time_now = time(NULL); + for(size_t i = 0; i < num_prev_download_items; ++i) { cJSON *downloaded_item_json = cJSON_CreateObject(); if(!downloaded_item_json) { result = -1; goto cleanup; } - cJSON_AddStringToObject(downloaded_item_json, "title", start_after); - cJSON_AddStringToObject(downloaded_item_json, "time", updated); - if(start_after_url) - cJSON_AddStringToObject(downloaded_item_json, "url", start_after_url); + char item_created_timestamp_fake[32]; + snprintf(item_created_timestamp_fake, sizeof(item_created_timestamp_fake), "%ld", time_now - i); + + cJSON_AddStringToObject(downloaded_item_json, "title", prev_download_items[i].title); + cJSON_AddStringToObject(downloaded_item_json, "time", item_created_timestamp_fake); + cJSON_AddStringToObject(downloaded_item_json, "url", prev_download_items[i].link); cJSON_AddItemToArray(downloaded_json, downloaded_item_json); } @@ -57,12 +60,22 @@ int write_plugin_json_to_file(const char *dir, const char *filename, const char return result; } +static long timestamps_get_max(long *timestamps, size_t num_timestamps) { + long max_timestamp = 0; + for(size_t i = 0; i < num_timestamps; ++i) { + long timestamp = timestamps[i]; + if(timestamp > max_timestamp) + max_timestamp = timestamp; + } + return max_timestamp; +} + /* TODO: If this fails in the middle, recover and update the next time somehow */ int tracked_item_update_latest(TrackedItem *tracked_item, char *tracked_dir, DownloadItemsData **download_items, char **filenames, long *timestamps, int num_download_items) { if(num_download_items == 0) return 0; - assert(num_download_items <= MAX_UPDATE_ITEMS); + assert(download_items); assert(timestamps); int tracked_dir_len = strlen(tracked_dir); int result = 0; @@ -72,7 +85,7 @@ int tracked_item_update_latest(TrackedItem *tracked_item, char *tracked_dir, Dow char updated[32]; assert(sizeof(time_t) == sizeof(long)); - sprintf(updated, "%ld", timestamps[num_download_items - 1]); + snprintf(updated, sizeof(updated), "%ld", timestamps_get_max(timestamps, num_download_items)); int updated_len = strlen(updated); result = file_overwrite_in_dir(item_filepath, "updated", updated, updated_len); if(result != 0) { |