X-Git-Url: https://git.wpitchoune.net/gitweb/?p=prss.git;a=blobdiff_plain;f=src%2Fttrss.c;h=d77007af5e98afa1ca03c1df69e245508d02229f;hp=99b8bc416eadb617372d305656300ab3a00d0888;hb=03759c1d9512afb87624c268481d5c9c499e8275;hpb=3b14915e76fc7a05512e0523afd39556c0a14567 diff --git a/src/ttrss.c b/src/ttrss.c index 99b8bc4..d77007a 100644 --- a/src/ttrss.c +++ b/src/ttrss.c @@ -18,344 +18,108 @@ */ #include +#include #include +#include +#include +#include +#include #include #include "http.h" -#include "ttrss.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 char *session_user; -static char *session_pwd; - static struct feed **data; - -void ws_request_add_att_str(json_object *rq, const char *k, const char *str) -{ - json_object_object_add(rq, k, json_object_new_string(str)); -} - -void ws_request_add_att_int(json_object *rq, const char *k, int v) -{ - json_object_object_add(rq, k, json_object_new_int(v)); -} - -struct json_object *ws_request_new(const char *op) +char *ttrss_get_headline_content(struct headline *h) { - struct json_object *rq; - - rq = json_object_new_object(); - - ws_request_add_att_str(rq, "op", op); - - if (session_id) - ws_request_add_att_str(rq, "sid", session_id); - - return rq; -} - -void ws_init(const char *url, const char *user, const char *pwd) -{ - char *tmp; - - if (session_id) - session_id = NULL; + char *content; - if (session_user) - free(session_user); - session_user = strdup(user); - - if (session_pwd) - free(session_pwd); - session_pwd = strdup(pwd); - - 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); -} - -struct json_object *ws_reply_get_content(struct json_object *rp) -{ - return json_object_object_get(rp, "content"); -} - -struct json_object *ws_execute(struct json_object *rq) -{ - struct json_object *rp, *content; - - rp = http_json_get(session_url, rq); - - if (rp) { - content = ws_reply_get_content(rp); - - if (content && !json_object_object_get(rp, "error")) { - json_object_get(content); - json_object_put(rp); - return content; - } - - json_object_put(rp); - } + content = cache_get(h); - return NULL; -} - -int ws_get_api_version() -{ - struct json_object *rp, *rq; - int v; - - rq = ws_request_new("getApiLevel"); - - rp = ws_execute(rq); - - json_object_put(rq); - - if (rp) { - v = json_object_get_int(json_object_object_get(rp, "level")); - - json_object_put(rp); + if (content) { + log_debug("ttrss_get_headline_content: cache hit"); } else { - v = 0; + log_debug("ttrss_get_headline_content: cache miss"); + content = ws_get_article_content(h->id); + cache_put(h->id, content); } - return v; + return content; } -char *ws_login() +struct feed **ttrss_get_feeds() { - struct json_object *rq, *rp, *j; - char *str; - - rq = ws_request_new("login"); - ws_request_add_att_str(rq, "user", session_user); - ws_request_add_att_str(rq, "password", session_pwd); - - rp = ws_execute(rq); - json_object_put(rq); - - if (rp) { - j = json_object_object_get(rp, "session_id"); - str = strdup(json_object_get_string(j)); - - json_object_put(rp); - } else { - str = NULL; - } + data = ws_update_feeds(data); - return str; + return data; } -int ws_open_session() +struct headline **ttrss_feed_get_headlines(struct feed *f) { - int version, result; - - if (session_id) - free(session_id); - - session_id = ws_login(); - - if (session_id) { - version = ws_get_api_version(); - printf("API version: %d\n", version); - - if (version > 0) { - result = 1; - } else { - free(session_id); - session_id = NULL; - result = 0; - } - } else { - result = 0; - } + if (!f->headlines) + ws_update_headlines(f); - return result; + return f->headlines; } -char *ws_get_article_content(int id) +void ttrss_set_article_unread(int id, int unread) { - struct json_object *rp, *rq, *content, *item; - char *str; - - rq = ws_request_new("getArticle"); - ws_request_add_att_int(rq, "article_id", id); - - rp = ws_execute(rq); - - json_object_put(rq); + log_debug("ttrss_set_article_unread(%d,%d)", id, unread); - str = NULL; + ws_async_set_article_unread(id, unread); - if (rp) { - item = json_object_array_get_idx(rp, 0); - - if (item) { - content = json_object_object_get(item, "content"); - str = strdup(json_object_get_string(content)); - } - - json_object_put(rp); - } - - return str; + log_debug("ttrss_set_article_unread(%d,%d)", id, unread); } -int ws_update_headlines(struct feed *feed) +void ttrss_set_config(const char *url, const char *user, const char *pwd) { - struct json_object *rp, *rq, *jheadline, *j; - int i, n, err, hid; - struct headline *h, **tmp; - - rq = ws_request_new("getHeadlines"); - ws_request_add_att_int(rq, "feed_id", feed->id); - - rp = ws_execute(rq); - - json_object_put(rq); - - if (rp) { - n = json_object_array_length(rp); - for (i = 0; i < n; i++) { - jheadline = json_object_array_get_idx(rp, i); - - j = json_object_object_get(jheadline, "id"); - hid = json_object_get_int(j); - h = feed_get_headline(feed, hid); - - if (!h) { - h = malloc(sizeof(struct headline)); - h->id = hid; - h->excerpt = NULL; - h->content = NULL; - h->title = NULL; - h->url = NULL; - - tmp = headlines_add(feed->headlines, h); - if (feed->headlines) - free(feed->headlines); - feed->headlines = tmp; - } - - if (!h->title) { - j = json_object_object_get(jheadline, "title"); - h->title = strdup(json_object_get_string(j)); - } - - if (!h->url) { - j = json_object_object_get(jheadline, "link"); - h->url = strdup(json_object_get_string(j)); - } - - j = json_object_object_get(jheadline, "unread"); - h->unread = json_object_get_boolean(j); - } - err = 0; - } else { - err = 1; - } - - json_object_put(rp); - return !err; + feeds_free(data); + data = NULL; + ws_set_config(url, user, pwd); } -const char *ttrss_get_headline_content(struct headline *h) +struct feed *ttrss_get_feed(int id) { - if (!h->content) - h->content = ws_get_article_content(h->id); - - return h->content; + 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 = ws_request_new("getFeeds"); - - rp = http_json_get(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); - - feed = malloc(sizeof(struct feed)); - - j = json_object_object_get(jfeed, "title"); - feed->title = strdup(json_object_get_string(j)); - - 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); - - j = json_object_object_get(jfeed, "unread"); - feed->unread = json_object_get_int(j); - - feed->headlines = NULL; - - tmp = feeds_add(feeds, feed); - free(feeds); - feeds = tmp; - } - } else { - feeds = NULL; - } - - json_object_put(rp); - - printf("ttrss_get_feeds ended\n"); - - return feeds; + return feeds_get_headline(data, id); } -struct headline **ttrss_get_headlines(struct feed *f) +static void get_article_content_cbk(int id, const char *content) { - if (!f->headlines) - ws_update_headlines(f); + printf("get_article_content_cbk %d\n", id); - return f->headlines; + if (content) + cache_put(id, content); } -void ttrss_set_article_unread(int id, int unread) +void ttrs_download_headline_content(struct feed **feeds) { - struct json_object *rp, *rq; + struct feed **fcur; + struct headline **hcur; - printf("ttrss_set_article_unread %d %d\n", id, unread); + log_debug("ttrs_download_headline_content"); - rq = ws_request_new("updateArticle"); - json_object_object_add(rq, "article_ids", json_object_new_int(id)); - json_object_object_add(rq, "field", json_object_new_int(2)); - json_object_object_add(rq, "mode", json_object_new_int(unread)); + for (fcur = feeds; *fcur; fcur++) { + hcur = ttrss_feed_get_headlines(*fcur); - rp = http_json_get(session_url, rq); - - json_object_put(rq); - json_object_put(rp); -} + log_debug("ttrs_download_headline_content(): %s", + (*fcur)->title); -void ttrss_set_config(const char *url, const char *user, const char *pwd) -{ - feeds_free(data); - data = NULL; - ws_init(url, user, pwd); + while (hcur && *hcur) { + if (!cache_exists(*hcur)) { + ws_async_get_article_content + ((*hcur)->id, get_article_content_cbk); + } + hcur++; + } + } }