1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
#include "rss_html_common.h"
#include "../depends/cJSON.h"
#include "fileutils.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
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();
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 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_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) {
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 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;
}
|