2 * Copyright (C) 2010-2013 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #include <json/json.h>
31 static char *session_id;
32 static char *session_url;
33 static char *session_user;
34 static char *session_pwd;
36 void ws_request_add_att_str(json_object *rq, const char *k, const char *str)
38 json_object_object_add(rq, k, json_object_new_string(str));
41 void ws_request_add_att_int(json_object *rq, const char *k, int v)
43 json_object_object_add(rq, k, json_object_new_int(v));
46 struct json_object *ws_request_new(const char *op)
48 struct json_object *rq;
50 rq = json_object_new_object();
52 ws_request_add_att_str(rq, "op", op);
55 ws_request_add_att_str(rq, "sid", session_id);
60 void ws_init(const char *url, const char *user, const char *pwd)
64 if (session_url && !strcmp(session_url, url)
65 && session_user && !strcmp(session_user, user)
66 && session_pwd && !strcmp(session_pwd, pwd))
73 session_user = strdup(user);
76 session_pwd = strdup(pwd);
78 tmp = url_normalize(url);
80 session_url = malloc(strlen(tmp) + strlen("/api/") + 1);
81 strcpy(session_url, tmp);
82 strcat(session_url, "/api/");
86 struct json_object *ws_reply_get_content(struct json_object *rp)
88 return json_object_object_get(rp, "content");
91 struct json_object *ws_execute(struct json_object *rq)
93 struct json_object *rp, *content;
95 rp = http_json_get(session_url, rq);
98 content = ws_reply_get_content(rp);
100 if (content && !json_object_object_get(rp, "error")) {
101 json_object_get(content);
112 int ws_get_api_version()
114 struct json_object *rp, *rq;
117 rq = ws_request_new("getApiLevel");
124 v = json_object_get_int(json_object_object_get(rp, "level"));
136 struct json_object *rq, *rp, *j;
139 rq = ws_request_new("login");
140 ws_request_add_att_str(rq, "user", session_user);
141 ws_request_add_att_str(rq, "password", session_pwd);
148 j = json_object_object_get(rp, "session_id");
151 str = strdup(json_object_get_string(j));
159 int ws_open_session()
166 session_id = ws_login();
169 version = ws_get_api_version();
170 log_debug("API version= %d", version);
186 char *ws_get_article_content(int id)
188 struct json_object *rp, *rq, *content, *item;
191 rq = ws_request_new("getArticle");
192 ws_request_add_att_int(rq, "article_id", id);
201 item = json_object_array_get_idx(rp, 0);
204 content = json_object_object_get(item, "content");
205 str = strdup(json_object_get_string(content));
214 int ws_update_headlines(struct feed *feed)
216 struct json_object *rp, *rq, *jheadline, *j;
218 struct headline *h, **tmp;
219 const char *title, *url;
221 rq = ws_request_new("getHeadlines");
222 ws_request_add_att_int(rq, "feed_id", feed->id);
229 n = json_object_array_length(rp);
230 for (i = 0; i < n; i++) {
231 jheadline = json_object_array_get_idx(rp, i);
233 j = json_object_object_get(jheadline, "id");
234 hid = json_object_get_int(j);
235 h = feed_get_headline(feed, hid);
238 j = json_object_object_get(jheadline, "title");
239 title = json_object_get_string(j);
241 j = json_object_object_get(jheadline, "link");
242 url = json_object_get_string(j);
244 h = headline_new(hid, url, title);
246 tmp = headlines_add(feed->headlines, h);
248 free(feed->headlines);
249 feed->headlines = tmp;
252 j = json_object_object_get(jheadline, "unread");
253 h->unread = json_object_get_boolean(j);
264 struct feed **ws_update_feeds(struct feed **feeds)
266 struct json_object *rp, *rq, *jfeed, *j;
268 struct feed *feed, **tmp;
269 const char *title, *url;
271 log_debug("ttrss_get_feeds()");
273 rq = ws_request_new("getFeeds");
279 n = json_object_array_length(rp);
281 for (i = 0; i < n; i++) {
282 jfeed = json_object_array_get_idx(rp, i);
284 j = json_object_object_get(jfeed, "id");
285 id = json_object_get_int(j);
287 feed = feeds_get_feed(feeds, id);
290 j = json_object_object_get(jfeed, "title");
291 title = json_object_get_string(j);
293 j = json_object_object_get(jfeed, "feed_url");
294 url = json_object_get_string(j);
296 feed = feed_new(id, url, title);
298 tmp = feeds_add(feeds, feed);
303 j = json_object_object_get(jfeed, "unread");
304 feed->unread = json_object_get_int(j);
313 log_debug("ttrss_get_feeds() done");