X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Fttrss.c;h=64a037563fb4b73239a6a191fac379f24abefef0;hb=48e7c0631ee3371b5cf6503417b4ec1c2b733176;hp=78884c1e37ba807391f6e6abc0e7ae8d376a4cd8;hpb=a7e89c999775c195f8b7759f4c283fcedb041a9e;p=prss.git diff --git a/src/ttrss.c b/src/ttrss.c index 78884c1..64a0375 100644 --- a/src/ttrss.c +++ b/src/ttrss.c @@ -22,165 +22,60 @@ #include -#include "phttp.h" -#include "ttrss.h" +#include "http.h" +#include "ttrss_ws.h" #include "url.h" -static char *session_id; -static char *session_url; +static struct feed **data; -static struct json_object *create_op(const char *op) +const char *ttrss_get_headline_content(struct headline *h) { - struct json_object *j; + if (!h->content) + h->content = ws_get_article_content(h->id); - j = json_object_new_object(); - json_object_object_add(j, "op", json_object_new_string(op)); - - if (session_id && strcmp(op, "login")) - json_object_object_add(j, - "sid", - json_object_new_string(session_id)); - - return j; + return h->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"); - return ; - } - - error = json_object_object_get(content, "error"); - if (error) { - fprintf(stderr, "Login failed"); - return ; - } + data = ws_update_feeds(data); - sid = json_object_object_get(content, "session_id"); - - if (session_id) { - free(session_id); - session_id = NULL; - } - - session_id = strdup(json_object_get_string(sid)); - - printf("Session id: %s\n", session_id); - - json_object_put(rp); + return data; } -struct feed **ttrss_get_feeds() +struct headline **ttrss_feed_get_headlines(struct feed *f) { - 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 (!f->headlines) + ws_update_headlines(f); - 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)); + return f->headlines; +} - j = json_object_object_get(jfeed, "feed_url"); - feed->url = strdup(json_object_get_string(j)); +void ttrss_set_article_unread(int id, int unread) +{ + struct json_object *rp, *rq; - j = json_object_object_get(jfeed, "id"); - feed->id = json_object_get_int(j); + printf("ttrss_set_article_unread %d %d\n", id, unread); - feed->headlines = ttrss_get_headlines(feed->id); + 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)); - feeds[i] = feed; - } - feeds[n] = NULL; - } else { - feeds = NULL; - } + rp = ws_execute(rq); + json_object_put(rq); json_object_put(rp); - - return feeds; } -struct headline **ttrss_get_headlines(int feed_id) +void ttrss_set_config(const char *url, const char *user, const char *pwd) { - struct json_object *rp, *rq, *content, *jheadline, *j; - int i, n; - struct headline **headlines, *h; - - 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)); - - headlines[i] = h; - } - headlines[n] = NULL; - } else { - headlines = NULL; - } - - json_object_put(rp); + feeds_free(data); + data = NULL; + ws_init(url, user, pwd); +} - return headlines; +struct feed *ttrss_get_feed(int id) +{ + return feeds_get_feed(data, id); }