X-Git-Url: https://git.wpitchoune.net/gitweb/?p=prss.git;a=blobdiff_plain;f=src%2Fttrss.c;h=9b762951a8cf618fe0e6e3993fd62a0a0b36c4a4;hp=78884c1e37ba807391f6e6abc0e7ae8d376a4cd8;hb=05d4d920a38f57f2afc0ef349d8f4c96a5852878;hpb=a7e89c999775c195f8b7759f4c283fcedb041a9e diff --git a/src/ttrss.c b/src/ttrss.c index 78884c1..9b76295 100644 --- a/src/ttrss.c +++ b/src/ttrss.c @@ -18,169 +18,125 @@ */ #include +#include #include +#include #include -#include "phttp.h" -#include "ttrss.h" +#include "http.h" +#include "io.h" +#include "log.h" +#include "ttrss_wsasync.h" #include "url.h" -static char *session_id; -static char *session_url; +static struct feed **data; +static char *cache_dir; -static struct json_object *create_op(const char *op) +static const char *get_cache_dir() { - struct json_object *j; + char *home; - j = json_object_new_object(); - json_object_object_add(j, "op", json_object_new_string(op)); + if (!cache_dir) { + home = getenv("HOME"); - if (session_id && strcmp(op, "login")) - json_object_object_add(j, - "sid", - json_object_new_string(session_id)); + if (!home) + return NULL; - return j; + cache_dir = path_append(home, ".prss/cache"); + mkdirs(cache_dir, 0777); + } + + return cache_dir; } -void ttrss_login(const char *url, const char *user, const char *password) +static void file_set_content(const char *path, const char *content) { - struct json_object *content, *rp, *error, *sid, *rq; - char *tmp; - - if (session_url) - free(session_url); - - tmp = url_normalize(url); - session_url = malloc(strlen(tmp) + strlen("/api/") + 1); - strcpy(session_url, tmp); - strcat(session_url, "/api/"); - free(tmp); + FILE *fp; + fp = fopen(path, "w"); + if (fp) { + fwrite(content, 1, strlen(content), fp); + fclose(fp); + } +} - rq = create_op("login"); - json_object_object_add(rq, "user", json_object_new_string(user)); - json_object_object_add(rq, - "password", - json_object_new_string(password)); +char *ttrss_get_headline_content(struct headline *h) +{ + const char *cache_dir; + char *path, *content; - rp = post_json_object(session_url, rq); - json_object_put(rq); + cache_dir = get_cache_dir(); + if (cache_dir) { + path = g_strdup_printf("%s/%d", cache_dir, h->id); - content = json_object_object_get(rp, "content"); - if (!content) { - fprintf(stderr, "Login failed: no content"); - return ; - } + content = file_get_content(path); - error = json_object_object_get(content, "error"); - if (error) { - fprintf(stderr, "Login failed"); - return ; - } + if (!content) { + content = ws_get_article_content(h->id); + file_set_content(path, content); + } - sid = json_object_object_get(content, "session_id"); + g_free(path); - if (session_id) { - free(session_id); - session_id = NULL; + return content; } - session_id = strdup(json_object_get_string(sid)); - - printf("Session id: %s\n", session_id); - - json_object_put(rp); + return NULL; } struct feed **ttrss_get_feeds() { - struct json_object *rp, *rq, *content, *jfeed, *j; - int i, n; - struct feed **feeds, *feed; - - rq = create_op("getFeeds"); - - rp = post_json_object(session_url, rq); - json_object_put(rq); - - content = json_object_object_get(rp, "content"); - - if (content) { - n = json_object_array_length(content); - - feeds = malloc((n+1) * sizeof(struct feed *)); - for (i = 0; i < n; i++) { - jfeed = json_object_array_get_idx(content, i); - - feed = malloc(sizeof(struct feed)); - - j = json_object_object_get(jfeed, "title"); - feed->title = strdup(json_object_get_string(j)); + data = ws_update_feeds(data); - j = json_object_object_get(jfeed, "feed_url"); - feed->url = strdup(json_object_get_string(j)); - - j = json_object_object_get(jfeed, "id"); - feed->id = json_object_get_int(j); - - feed->headlines = ttrss_get_headlines(feed->id); - - feeds[i] = feed; - } - feeds[n] = NULL; - } else { - feeds = NULL; - } - - json_object_put(rp); - - return feeds; + return data; } -struct headline **ttrss_get_headlines(int feed_id) +struct headline **ttrss_feed_get_headlines(struct feed *f) { - struct json_object *rp, *rq, *content, *jheadline, *j; - int i, n; - struct headline **headlines, *h; + if (!f->headlines) + ws_update_headlines(f); - rq = create_op("getHeadlines"); - json_object_object_add(rq, "feed_id", json_object_new_int(feed_id)); - json_object_object_add(rq, "show_excerpt", json_object_new_boolean(1)); - json_object_object_add(rq, "show_content", json_object_new_boolean(1)); + return f->headlines; +} - rp = post_json_object(session_url, rq); +void ttrss_set_article_unread(int id, int unread) +{ + log_debug("ttrss_set_article_unread(%d,%d)", id, unread); - json_object_put(rq); + ws_async_set_article_unread(id, unread); - content = json_object_object_get(rp, "content"); + log_debug("ttrss_set_article_unread(%d,%d)", id, unread); +} - if (content) { - n = json_object_array_length(content); - headlines = malloc((n+1)*sizeof(struct headline *)); - for (i = 0; i < n; i++) { - jheadline = json_object_array_get_idx(content, i); +void ttrss_set_config(const char *url, const char *user, const char *pwd) +{ + feeds_free(data); + data = NULL; + ws_set_config(url, user, pwd); +} - h = malloc(sizeof(struct headline)); +struct feed *ttrss_get_feed(int id) +{ + return feeds_get_feed(data, id); +} - j = json_object_object_get(jheadline, "title"); - h->title = strdup(json_object_get_string(j)); +struct headline *ttrss_get_headline(int id) +{ + return feeds_get_headline(data, id); +} - j = json_object_object_get(jheadline, "excerpt"); - h->excerpt = strdup(json_object_get_string(j)); +void ttrs_download_headline_content(struct feed **feeds) +{ + struct feed **fcur; + struct headline **hcur; - j = json_object_object_get(jheadline, "content"); - h->content = strdup(json_object_get_string(j)); + for (fcur = feeds; *fcur; fcur++) { + hcur = ttrss_feed_get_headlines(*fcur); - headlines[i] = h; + while (hcur && *hcur) { + free(ttrss_get_headline_content(*hcur)); + hcur++; } - headlines[n] = NULL; - } else { - headlines = NULL; } - - json_object_put(rp); - - return headlines; }