aboutsummaryrefslogtreecommitdiff
path: root/src/rss_html_common.c
blob: c72273cec0b040a32a419eb1c3e897a1706bfc99 (plain)
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
#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, 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;
}