X-Git-Url: https://git.wpitchoune.net/gitweb/?p=prss.git;a=blobdiff_plain;f=src%2Fttrss.c;h=97f28038b234cd18d1ccecb8605372e0de159b7b;hp=78884c1e37ba807391f6e6abc0e7ae8d376a4cd8;hb=a0736fce0f13ccf8ae96f00b5ed65af316c7571a;hpb=a7e89c999775c195f8b7759f4c283fcedb041a9e diff --git a/src/ttrss.c b/src/ttrss.c index 78884c1..97f2803 100644 --- a/src/ttrss.c +++ b/src/ttrss.c @@ -22,33 +22,54 @@ #include -#include "phttp.h" +#include "http.h" #include "ttrss.h" #include "url.h" static char *session_id; static char *session_url; +static char *session_user; +static char *session_pwd; -static struct json_object *create_op(const char *op) +void ws_request_add_att_str(json_object *rq, const char *k, const char *str) { - struct json_object *j; + json_object_object_add(rq, k, json_object_new_string(str)); +} - j = json_object_new_object(); - json_object_object_add(j, "op", json_object_new_string(op)); +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) +{ + struct json_object *rq; - if (session_id && strcmp(op, "login")) - json_object_object_add(j, - "sid", - json_object_new_string(session_id)); + rq = json_object_new_object(); - return j; + ws_request_add_att_str(rq, "op", op); + + if (session_id) + ws_request_add_att_str(rq, "sid", session_id); + + return rq; } -void ttrss_login(const char *url, const char *user, const char *password) +void ws_init(const char *url, const char *user, const char *pwd) { - struct json_object *content, *rp, *error, *sid, *rq; char *tmp; + if (session_id) + session_id = NULL; + + 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); @@ -57,100 +78,154 @@ void ttrss_login(const char *url, const char *user, const char *password) 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"); +} - 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)); +struct json_object *ws_execute(struct json_object *rq) +{ + struct json_object *rp, *content; - rp = post_json_object(session_url, rq); - json_object_put(rq); + rp = http_json_get(session_url, rq); - content = json_object_object_get(rp, "content"); - if (!content) { - fprintf(stderr, "Login failed: no content"); - return ; - } + if (rp) { + content = ws_reply_get_content(rp); - error = json_object_object_get(content, "error"); - if (error) { - fprintf(stderr, "Login failed"); - return ; + if (content && !json_object_object_get(rp, "error")) { + json_object_get(content); + json_object_put(rp); + return content; + } + + json_object_put(rp); } - sid = json_object_object_get(content, "session_id"); + return NULL; +} - if (session_id) { - free(session_id); - session_id = NULL; - } +int ws_get_api_version() +{ + struct json_object *rp, *rq; + int v; - session_id = strdup(json_object_get_string(sid)); + rq = ws_request_new("getApiLevel"); - printf("Session id: %s\n", session_id); + rp = ws_execute(rq); - json_object_put(rp); + json_object_put(rq); + + if (rp) { + v = json_object_get_int(json_object_object_get(rp, "level")); + + json_object_put(rp); + } else { + v = 0; + } + + return v; } -struct feed **ttrss_get_feeds() +char *ws_login() { - struct json_object *rp, *rq, *content, *jfeed, *j; - int i, n; - struct feed **feeds, *feed; + struct json_object *rq, *rp, *j; + char *str; - rq = create_op("getFeeds"); + rq = ws_request_new("login"); + ws_request_add_att_str(rq, "user", session_user); + ws_request_add_att_str(rq, "password", session_pwd); - rp = post_json_object(session_url, rq); + rp = ws_execute(rq); json_object_put(rq); - content = json_object_object_get(rp, "content"); - - if (content) { - n = json_object_array_length(content); + if (rp) { + j = json_object_object_get(rp, "session_id"); + str = strdup(json_object_get_string(j)); - 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)); + json_object_put(rp); + } else { + str = NULL; + } - j = json_object_object_get(jfeed, "title"); - feed->title = strdup(json_object_get_string(j)); + return str; +} - j = json_object_object_get(jfeed, "feed_url"); - feed->url = strdup(json_object_get_string(j)); +int ws_open_session() +{ + int version, result; - j = json_object_object_get(jfeed, "id"); - feed->id = json_object_get_int(j); + if (session_id) + free(session_id); - feed->headlines = ttrss_get_headlines(feed->id); + session_id = ws_login(); - feeds[i] = feed; + 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; } - feeds[n] = NULL; } else { - feeds = NULL; + result = 0; } - json_object_put(rp); + return result; +} - return feeds; +const char *ttrss_get_headline_content(struct headline *h) +{ + struct json_object *rp, *rq, *content, *array, *item; + + printf("get_headlines %d\n", h->id); + + if (!h->content) { + rq = ws_request_new("getArticle"); + json_object_object_add(rq, "article_id", + json_object_new_int(h->id)); + + rp = http_json_get(session_url, rq); + + json_object_put(rq); + + array = json_object_object_get(rp, "content"); + + if (!array) + goto release; + + item = json_object_array_get_idx(array, 0); + + if (!item) + goto release; + + content = json_object_object_get(item, "content"); + + h->content = strdup(json_object_get_string(content)); + + release: + json_object_put(rp); + } + return h->content; } -struct headline **ttrss_get_headlines(int feed_id) +static struct headline **get_headlines(int feed_id) { struct json_object *rp, *rq, *content, *jheadline, *j; int i, n; struct headline **headlines, *h; - rq = create_op("getHeadlines"); + printf("get_headlines %d\n", feed_id); + + rq = ws_request_new("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); + rp = http_json_get(session_url, rq); json_object_put(rq); @@ -164,14 +239,20 @@ struct headline **ttrss_get_headlines(int feed_id) h = malloc(sizeof(struct headline)); + j = json_object_object_get(jheadline, "id"); + h->id = json_object_get_int(j); + 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, "link"); + h->url = strdup(json_object_get_string(j)); + + h->excerpt = NULL; + h->content = NULL; - 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); headlines[i] = h; } @@ -182,5 +263,119 @@ struct headline **ttrss_get_headlines(int feed_id) json_object_put(rp); + printf("get_headlines %d end\n", feed_id); + return headlines; } + +static int feeds_length(struct feed **list) +{ + int n; + + if (!list) + return 0; + + n = 0; + while(*list) { + n++; + list++; + } + + return n; +} + +static struct feed **feeds_add(struct feed **feeds, struct feed *feed) +{ + 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; +} + +struct feed **ttrss_get_feeds() +{ + 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; +} + +struct headline **ttrss_get_headlines(struct feed *f) +{ + if (!f->headlines) + f->headlines = get_headlines(f->id); + + return f->headlines; +} + +void ttrss_set_article_unread(int id, int unread) +{ + struct json_object *rp, *rq; + + printf("ttrss_set_article_unread %d %d\n", id, unread); + + 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)); + + rp = http_json_get(session_url, rq); + + json_object_put(rq); + json_object_put(rp); +}