#include "rss_html_common.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, const char *start_after, const char *start_after_url, const char *plugin_name) {
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;
}
if(start_after) {
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);
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;
}
/* 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(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));
sprintf(updated, "%ld", timestamps[num_download_items - 1]);
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 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", download_items[i]->link);
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;
}