#include "rss_html_common.h"
#include "fallback.h"
#include "../depends/cJSON.h"
#include "fileutils.h"
#include
#include
#include
#include
#include
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, fallback *fall) {
int result = 0;
cJSON *json_body = cJSON_CreateObject();
if(!json_body) {
result = -1;
goto cleanup;
}
cJSON_AddStringToObject(json_body, "link", url);
cJSON_AddStringToObject(json_body, "updated", updated);
if(plugin_name)
cJSON_AddStringToObject(json_body, "plugin", plugin_name);
cJSON *downloaded_json = cJSON_AddArrayToObject(json_body, "downloaded");
if(!downloaded_json) {
result = -1;
goto cleanup;
}
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;
}
char new_url[2048];
snprintf(new_url, sizeof(new_url), "%s", prev_download_items[i].link);
if(fall)
fallback_replace_active_fallback_url_with_source_url(fall, prev_download_items[i].link, new_url, sizeof(new_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", new_url);
cJSON_AddBoolToObject(downloaded_item_json, "filler", 1);
cJSON_AddItemToArray(downloaded_json, downloaded_item_json);
}
char *json_body_str = cJSON_Print(json_body);
if(!json_body_str) {
result = -1;
goto cleanup;
}
result = file_overwrite_in_dir(dir, filename, json_body_str, strlen(json_body_str));
free(json_body_str);
cleanup:
cJSON_Delete(json_body);
if(result != 0)
fprintf(stderr, "Failed to write json data to file %s/%s\n", dir, filename);
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, fallback *fall) {
if(num_download_items == 0)
return 0;
assert(download_items);
assert(timestamps);
int tracked_dir_len = strlen(tracked_dir);
int result = 0;
char *item_filepath = tracked_dir;
strcat(item_filepath, tracked_item->title);
char updated[32];
assert(sizeof(time_t) == sizeof(long));
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) {
fprintf(stderr, "Failed to update %s/updated\n", item_filepath);
goto cleanup;
}
cJSON *updated_field = cJSON_GetObjectItemCaseSensitive(tracked_item->json_data, "updated");
if(updated_field && cJSON_IsString(updated_field)) {
cJSON_SetValuestring(updated_field, updated);
} else {
fprintf(stderr, "Corrupt json for rss item: %s\n", item_filepath);
}
cJSON *downloaded_json = cJSON_GetObjectItemCaseSensitive(tracked_item->json_data, "downloaded");
if(!downloaded_json)
downloaded_json = cJSON_AddArrayToObject(tracked_item->json_data, "downloaded");
if(!downloaded_json || !cJSON_IsArray(downloaded_json)) {
result = -1;
goto cleanup;
}
for(int i = 0; i < num_download_items; ++i) {
cJSON *downloaded_item_json = cJSON_CreateObject();
if(!downloaded_item_json) {
result = -1;
goto cleanup;
}
char new_url[2048];
snprintf(new_url, sizeof(new_url), "%s", download_items[i]->link);
if(fall)
fallback_replace_active_fallback_url_with_source_url(fall, download_items[i]->link, new_url, sizeof(new_url));
char timestamp[32];
snprintf(timestamp, sizeof(timestamp), "%ld", timestamps[i]);
cJSON_AddStringToObject(downloaded_item_json, "title", download_items[i]->title);
cJSON_AddStringToObject(downloaded_item_json, "time", timestamp);
cJSON_AddStringToObject(downloaded_item_json, "url", new_url);
if(filenames)
cJSON_AddStringToObject(downloaded_item_json, "filename", filenames[i]);
cJSON_AddItemToArray(downloaded_json, downloaded_item_json);
}
char *json_body_str = cJSON_Print(tracked_item->json_data);
if(!json_body_str) {
result = -1;
goto cleanup;
}
result = file_overwrite_in_dir(item_filepath, "data", json_body_str, strlen(json_body_str));
free(json_body_str);
cleanup:
if(result != 0)
fprintf(stderr, "Failed to update data for item: %s\n", item_filepath);
tracked_dir[tracked_dir_len] = '\0';
return result;
}