X-Git-Url: https://git.wpitchoune.net/gitweb/?p=prss.git;a=blobdiff_plain;f=src%2Fttrss.c;h=d77007af5e98afa1ca03c1df69e245508d02229f;hp=ec67cc8af703219b033557e9996fb8fc1f9276e8;hb=c6fa04234bdb4ad62f0ed0e3f9a619a2ff007e8a;hpb=b71756ccc346fe32ab35ae23259bf0b3bb016d25 diff --git a/src/ttrss.c b/src/ttrss.c index ec67cc8..d77007a 100644 --- a/src/ttrss.c +++ b/src/ttrss.c @@ -18,226 +18,108 @@ */ #include +#include #include +#include +#include +#include +#include #include -#include "phttp.h" -#include "ttrss.h" +#include "http.h" +#include "io.h" +#include "log.h" +#include "ttrss_cache.h" +#include "ttrss_wsasync.h" #include "url.h" -static char *session_id; -static char *session_url; - -static struct json_object *create_op(const char *op) +static struct feed **data; +char *ttrss_get_headline_content(struct headline *h) { - struct json_object *j; + char *content; - j = json_object_new_object(); - json_object_object_add(j, "op", json_object_new_string(op)); + content = cache_get(h); - if (session_id && strcmp(op, "login")) - json_object_object_add(j, - "sid", - json_object_new_string(session_id)); + if (content) { + log_debug("ttrss_get_headline_content: cache hit"); + } else { + log_debug("ttrss_get_headline_content: cache miss"); + content = ws_get_article_content(h->id); + cache_put(h->id, content); + } - return j; + return content; } -void ttrss_login(const char *url, const char *user, const char *password) +struct feed **ttrss_get_feeds() { - 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); - - - 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)); - - rp = post_json_object(session_url, rq); - json_object_put(rq); - - content = json_object_object_get(rp, "content"); - if (!content) { - fprintf(stderr, "Login failed: no content\n"); - return ; - } - - error = json_object_object_get(content, "error"); - if (error) { - fprintf(stderr, "Login failed\n"); - return ; - } - - sid = json_object_object_get(content, "session_id"); - - if (session_id) { - free(session_id); - session_id = NULL; - } + data = ws_update_feeds(data); - session_id = strdup(json_object_get_string(sid)); - - printf("Session id: %s\n", session_id); - - json_object_put(rp); + return data; } -static struct headline **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; - - printf("get_headlines %d\n", feed_id); - - 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)); - - 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); - headlines = malloc((n+1)*sizeof(struct headline *)); - for (i = 0; i < n; i++) { - jheadline = json_object_array_get_idx(content, i); - - h = malloc(sizeof(struct headline)); - - j = json_object_object_get(jheadline, "title"); - h->title = strdup(json_object_get_string(j)); - - j = json_object_object_get(jheadline, "excerpt"); - h->excerpt = strdup(json_object_get_string(j)); - - j = json_object_object_get(jheadline, "content"); - h->content = strdup(json_object_get_string(j)); - - j = json_object_object_get(jheadline, "unread"); - h->unread = json_object_get_boolean(j); + if (!f->headlines) + ws_update_headlines(f); - headlines[i] = h; - } - headlines[n] = NULL; - } else { - headlines = NULL; - } + return f->headlines; +} - json_object_put(rp); +void ttrss_set_article_unread(int id, int unread) +{ + log_debug("ttrss_set_article_unread(%d,%d)", id, unread); - printf("get_headlines %d end\n", feed_id); + ws_async_set_article_unread(id, unread); - return headlines; + log_debug("ttrss_set_article_unread(%d,%d)", id, unread); } -static int feeds_length(struct feed **list) +void ttrss_set_config(const char *url, const char *user, const char *pwd) { - int n; - - if (!list) - return 0; - - n = 0; - while(*list) { - n++; - list++; - } - - return n; + feeds_free(data); + data = NULL; + ws_set_config(url, user, pwd); } -static struct feed **feeds_add(struct feed **feeds, struct feed *feed) +struct feed *ttrss_get_feed(int id) { - int n; - struct feed **result; - - n = feeds_length(feeds); - - result = malloc((n + 1 + 1) * sizeof(struct feed *)); - - if (feeds) - memcpy(result, feeds, n * sizeof(struct feed *)); - - result[n] = feed; - result[n + 1] = NULL; - - return result; + return feeds_get_feed(data, id); } -struct feed **ttrss_get_feeds() +struct headline *ttrss_get_headline(int id) { - struct json_object *rp, *rq, *content, *jfeed, *j; - int i, n; - struct feed **feeds, *feed, **tmp; - - printf("ttrss_get_feeds\n"); - - 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 = NULL; - for (i = 0; i < n; i++) { - jfeed = json_object_array_get_idx(content, i); + return feeds_get_headline(data, id); +} - feed = malloc(sizeof(struct feed)); +static void get_article_content_cbk(int id, const char *content) +{ + printf("get_article_content_cbk %d\n", id); - j = json_object_object_get(jfeed, "title"); - feed->title = strdup(json_object_get_string(j)); + if (content) + cache_put(id, content); +} - j = json_object_object_get(jfeed, "feed_url"); - feed->url = 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(jfeed, "id"); - feed->id = json_object_get_int(j); + log_debug("ttrs_download_headline_content"); - j = json_object_object_get(jfeed, "unread"); - feed->unread = json_object_get_int(j); + for (fcur = feeds; *fcur; fcur++) { + hcur = ttrss_feed_get_headlines(*fcur); - feed->headlines = NULL; + log_debug("ttrs_download_headline_content(): %s", + (*fcur)->title); - tmp = feeds_add(feeds, feed); - free(feeds); - feeds = tmp; + while (hcur && *hcur) { + if (!cache_exists(*hcur)) { + ws_async_get_article_content + ((*hcur)->id, get_article_content_cbk); + } + hcur++; } - } else { - feeds = NULL; } - - json_object_put(rp); - - printf("ttrss_get_feeds ended\n"); - - return feeds; -} - -struct headline **ttrss_get_headlines(struct feed *f) -{ - if (!f->headlines) - f->headlines = get_headlines(f->id); - - return f->headlines; }