X-Git-Url: https://git.wpitchoune.net/gitweb/?p=prss.git;a=blobdiff_plain;f=src%2Fttrss.c;h=3649b071efb1f98dc70c5592b08659ca9aa47747;hp=ec67cc8af703219b033557e9996fb8fc1f9276e8;hb=0d88bb9ed245be97f3120dad9c3c1604cd9c4183;hpb=b71756ccc346fe32ab35ae23259bf0b3bb016d25 diff --git a/src/ttrss.c b/src/ttrss.c index ec67cc8..3649b07 100644 --- a/src/ttrss.c +++ b/src/ttrss.c @@ -22,33 +22,104 @@ #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) +static int list_length(void **list) { - struct json_object *j; + int n; + + if (!list) + return 0; + + n = 0; + while(*list) { + n++; + list++; + } + + return n; +} + +static void **list_add(void **list, void *item) +{ + int n; + void **result; + + n = list_length(list); - j = json_object_new_object(); - json_object_object_add(j, "op", json_object_new_string(op)); + result = malloc((n + 1 + 1) * sizeof(void *)); - if (session_id && strcmp(op, "login")) - json_object_object_add(j, - "sid", - json_object_new_string(session_id)); + if (list) + memcpy(result, list, n * sizeof(void *)); - return j; + result[n] = item; + result[n + 1] = NULL; + + return result; +} + + +struct headline *feed_get_headline(struct feed *feed, int id) +{ + struct headline **headlines; + + headlines = feed->headlines; + if (headlines) + while (*headlines) { + if ((*headlines)->id == id) + return *headlines; + headlines++; + } + + return NULL; +} + +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 ttrss_login(const char *url, const char *user, const char *password) +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; + + 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) { - 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,130 +128,197 @@ 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)); - - rp = post_json_object(session_url, rq); - json_object_put(rq); +struct json_object *ws_execute(struct json_object *rq) +{ + struct json_object *rp, *content; - content = json_object_object_get(rp, "content"); - if (!content) { - fprintf(stderr, "Login failed: no content\n"); - return ; - } + rp = http_json_get(session_url, rq); - error = json_object_object_get(content, "error"); - if (error) { - fprintf(stderr, "Login failed\n"); - return ; - } + if (rp) { + content = ws_reply_get_content(rp); - sid = json_object_object_get(content, "session_id"); + if (content && !json_object_object_get(rp, "error")) { + json_object_get(content); + json_object_put(rp); + return content; + } - if (session_id) { - free(session_id); - session_id = NULL; + json_object_put(rp); } - session_id = strdup(json_object_get_string(sid)); - - printf("Session id: %s\n", session_id); - - json_object_put(rp); + return NULL; } -static struct headline **get_headlines(int feed_id) +int ws_get_api_version() { - struct json_object *rp, *rq, *content, *jheadline, *j; - int i, n; - struct headline **headlines, *h; - - printf("get_headlines %d\n", feed_id); + struct json_object *rp, *rq; + int v; - 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)); + rq = ws_request_new("getApiLevel"); - rp = post_json_object(session_url, rq); + rp = ws_execute(rq); json_object_put(rq); - content = json_object_object_get(rp, "content"); + if (rp) { + v = json_object_get_int(json_object_object_get(rp, "level")); - 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); + json_object_put(rp); + } else { + v = 0; + } - h = malloc(sizeof(struct headline)); + return v; +} - j = json_object_object_get(jheadline, "title"); - h->title = strdup(json_object_get_string(j)); +char *ws_login() +{ + struct json_object *rq, *rp, *j; + char *str; - j = json_object_object_get(jheadline, "excerpt"); - h->excerpt = strdup(json_object_get_string(j)); + rq = ws_request_new("login"); + ws_request_add_att_str(rq, "user", session_user); + ws_request_add_att_str(rq, "password", session_pwd); - j = json_object_object_get(jheadline, "content"); - h->content = strdup(json_object_get_string(j)); + rp = ws_execute(rq); + json_object_put(rq); - j = json_object_object_get(jheadline, "unread"); - h->unread = json_object_get_boolean(j); + if (rp) { + j = json_object_object_get(rp, "session_id"); + str = strdup(json_object_get_string(j)); - headlines[i] = h; - } - headlines[n] = NULL; + json_object_put(rp); } else { - headlines = NULL; + str = NULL; } - json_object_put(rp); + return str; +} + +int ws_open_session() +{ + int version, result; + + if (session_id) + free(session_id); - printf("get_headlines %d end\n", feed_id); + session_id = ws_login(); - return headlines; + 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; + } + + return result; } -static int feeds_length(struct feed **list) +char *ws_get_article_content(int id) { - int n; + struct json_object *rp, *rq, *content, *item; + char *str; - if (!list) - return 0; + rq = ws_request_new("getArticle"); + ws_request_add_att_int(rq, "article_id", id); - n = 0; - while(*list) { - n++; - list++; + rp = ws_execute(rq); + + json_object_put(rq); + + str = NULL; + + 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 n; + return str; } -static struct feed **feeds_add(struct feed **feeds, struct feed *feed) +int ws_update_headlines(struct feed *feed) { - int n; - struct feed **result; + struct json_object *rp, *rq, *jheadline, *j; + int i, n, err, hid; + struct headline *h, **tmp; - n = feeds_length(feeds); + rq = ws_request_new("getHeadlines"); + ws_request_add_att_int(rq, "feed_id", feed->id); - result = malloc((n + 1 + 1) * sizeof(struct feed *)); + rp = ws_execute(rq); - if (feeds) - memcpy(result, feeds, n * sizeof(struct feed *)); + json_object_put(rq); - result[n] = feed; - result[n + 1] = NULL; + 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; + + tmp = (struct headline **)list_add((void **)feed->headlines, h); + if (feed->headlines) + free(feed->headlines); + feed->headlines = tmp; + } - return result; + j = json_object_object_get(jheadline, "title"); + h->title = strdup(json_object_get_string(j)); + + 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; +} + +const char *ttrss_get_headline_content(struct headline *h) +{ + if (!h->content) + h->content = ws_get_article_content(h->id); + + return h->content; } + struct feed **ttrss_get_feeds() { struct json_object *rp, *rq, *content, *jfeed, *j; @@ -189,9 +327,9 @@ struct feed **ttrss_get_feeds() printf("ttrss_get_feeds\n"); - rq = create_op("getFeeds"); + rq = ws_request_new("getFeeds"); - rp = post_json_object(session_url, rq); + rp = http_json_get(session_url, rq); json_object_put(rq); content = json_object_object_get(rp, "content"); @@ -219,7 +357,7 @@ struct feed **ttrss_get_feeds() feed->headlines = NULL; - tmp = feeds_add(feeds, feed); + tmp = (struct feed **)list_add((void **)feeds, feed); free(feeds); feeds = tmp; } @@ -237,7 +375,24 @@ struct feed **ttrss_get_feeds() struct headline **ttrss_get_headlines(struct feed *f) { if (!f->headlines) - f->headlines = get_headlines(f->id); + ws_update_headlines(f); 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); +}