From: Jean-Philippe Orsini Date: Thu, 25 Apr 2013 08:25:41 +0000 (+0000) Subject: (no commit message) X-Git-Url: https://git.wpitchoune.net/gitweb/?p=prss.git;a=commitdiff_plain;h=ba047027e5c6b1f6e2ec6ae5846a37f5c44dc412 --- diff --git a/src/ttrss_ws.c b/src/ttrss_ws.c new file mode 100644 index 0000000..3b979c8 --- /dev/null +++ b/src/ttrss_ws.c @@ -0,0 +1,315 @@ +/* + * Copyright (C) 2010-2013 jeanfi@gmail.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#include +#include + +#include + +#include "http.h" +#include "ttrss_ws.h" +#include "url.h" + +static char *session_id; +static char *session_url; +static char *session_user; +static char *session_pwd; + +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) +{ + 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_url && !strcmp(session_url, url) + && session_user && !strcmp(session_user, user) + && session_pwd && !strcmp(session_pwd, pwd)) + return ; + + free(session_id); + session_id = NULL; + + free(session_user); + session_user = strdup(user); + + free(session_pwd); + session_pwd = strdup(pwd); + + tmp = url_normalize(url); + free(session_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); + } + + 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); + } else { + v = 0; + } + + return v; +} + +char *ws_login() +{ + 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; + } + + return str; +} + +int ws_open_session() +{ + 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; + } + + return result; +} + +char *ws_get_article_content(int id) +{ + 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); + + 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 str; +} + +int ws_update_headlines(struct feed *feed) +{ + struct json_object *rp, *rq, *jheadline, *j; + int i, n, err, hid; + struct headline *h, **tmp; + const char *title, *url; + + 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) { + j = json_object_object_get(jheadline, "title"); + title = json_object_get_string(j); + + j = json_object_object_get(jheadline, "link"); + url = json_object_get_string(j); + + h = headline_new(hid, url, title); + + tmp = headlines_add(feed->headlines, h); + if (feed->headlines) + free(feed->headlines); + feed->headlines = tmp; + } + + 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; +} + +struct feed **ws_update_feeds(struct feed **feeds) +{ + struct json_object *rp, *rq, *jfeed, *j; + int i, n, id; + struct feed *feed, **tmp; + const char *title, *url; + + printf("ttrss_get_feeds\n"); + + rq = ws_request_new("getFeeds"); + + rp = ws_execute(rq); + json_object_put(rq); + + if (rp) { + n = json_object_array_length(rp); + + for (i = 0; i < n; i++) { + jfeed = json_object_array_get_idx(rp, i); + + j = json_object_object_get(jfeed, "id"); + id = json_object_get_int(j); + + feed = feeds_get_feed(feeds, id); + + if (!feed) { + j = json_object_object_get(jfeed, "title"); + title = json_object_get_string(j); + + j = json_object_object_get(jfeed, "feed_url"); + url = json_object_get_string(j); + + feed = feed_new(id, url, title); + + tmp = feeds_add(feeds, feed); + free(feeds); + feeds = tmp; + } else { + printf("found!\n"); + } + + j = json_object_object_get(jfeed, "unread"); + feed->unread = json_object_get_int(j); + } + } else { + feeds_free(feeds); + feeds = NULL; + } + + json_object_put(rp); + + printf("ttrss_get_feeds ended\n"); + + return feeds; +}