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
23 #include <json/json.h>
29 static char *session_id;
30 static char *session_url;
31 static char *session_user;
32 static char *session_pwd;
34 void ws_request_add_att_str(json_object *rq, const char *k, const char *str)
36 json_object_object_add(rq, k, json_object_new_string(str));
39 void ws_request_add_att_int(json_object *rq, const char *k, int v)
41 json_object_object_add(rq, k, json_object_new_int(v));
44 struct json_object *ws_request_new(const char *op)
46 struct json_object *rq;
48 rq = json_object_new_object();
50 ws_request_add_att_str(rq, "op", op);
53 ws_request_add_att_str(rq, "sid", session_id);
58 void ws_init(const char *url, const char *user, const char *pwd)
62 if (session_url && !strcmp(session_url, url)
63 && session_user && !strcmp(session_user, user)
64 && session_pwd && !strcmp(session_pwd, pwd))
71 session_user = strdup(user);
74 session_pwd = strdup(pwd);
76 tmp = url_normalize(url);
78 session_url = malloc(strlen(tmp) + strlen("/api/") + 1);
79 strcpy(session_url, tmp);
80 strcat(session_url, "/api/");
84 struct json_object *ws_reply_get_content(struct json_object *rp)
86 return json_object_object_get(rp, "content");
89 struct json_object *ws_execute(struct json_object *rq)
91 struct json_object *rp, *content;
93 rp = http_json_get(session_url, rq);
96 content = ws_reply_get_content(rp);
98 if (content && !json_object_object_get(rp, "error")) {
99 json_object_get(content);
110 int ws_get_api_version()
112 struct json_object *rp, *rq;
115 rq = ws_request_new("getApiLevel");
122 v = json_object_get_int(json_object_object_get(rp, "level"));
134 struct json_object *rq, *rp, *j;
137 rq = ws_request_new("login");
138 ws_request_add_att_str(rq, "user", session_user);
139 ws_request_add_att_str(rq, "password", session_pwd);
145 j = json_object_object_get(rp, "session_id");
146 str = strdup(json_object_get_string(j));
156 int ws_open_session()
163 session_id = ws_login();
166 version = ws_get_api_version();
167 printf("API version: %d\n", version);
183 char *ws_get_article_content(int id)
185 struct json_object *rp, *rq, *content, *item;
188 rq = ws_request_new("getArticle");
189 ws_request_add_att_int(rq, "article_id", id);
198 item = json_object_array_get_idx(rp, 0);
201 content = json_object_object_get(item, "content");
202 str = strdup(json_object_get_string(content));
211 int ws_update_headlines(struct feed *feed)
213 struct json_object *rp, *rq, *jheadline, *j;
215 struct headline *h, **tmp;
216 const char *title, *url;
218 rq = ws_request_new("getHeadlines");
219 ws_request_add_att_int(rq, "feed_id", feed->id);
226 n = json_object_array_length(rp);
227 for (i = 0; i < n; i++) {
228 jheadline = json_object_array_get_idx(rp, i);
230 j = json_object_object_get(jheadline, "id");
231 hid = json_object_get_int(j);
232 h = feed_get_headline(feed, hid);
235 j = json_object_object_get(jheadline, "title");
236 title = json_object_get_string(j);
238 j = json_object_object_get(jheadline, "link");
239 url = json_object_get_string(j);
241 h = headline_new(hid, url, title);
243 tmp = headlines_add(feed->headlines, h);
245 free(feed->headlines);
246 feed->headlines = tmp;
249 j = json_object_object_get(jheadline, "unread");
250 h->unread = json_object_get_boolean(j);
261 struct feed **ws_update_feeds(struct feed **feeds)
263 struct json_object *rp, *rq, *jfeed, *j;
265 struct feed *feed, **tmp;
266 const char *title, *url;
268 printf("ttrss_get_feeds\n");
270 rq = ws_request_new("getFeeds");
276 n = json_object_array_length(rp);
278 for (i = 0; i < n; i++) {
279 jfeed = json_object_array_get_idx(rp, i);
281 j = json_object_object_get(jfeed, "id");
282 id = json_object_get_int(j);
284 feed = feeds_get_feed(feeds, id);
287 j = json_object_object_get(jfeed, "title");
288 title = json_object_get_string(j);
290 j = json_object_object_get(jfeed, "feed_url");
291 url = json_object_get_string(j);
293 feed = feed_new(id, url, title);
295 tmp = feeds_add(feeds, feed);
302 j = json_object_object_get(jfeed, "unread");
303 feed->unread = json_object_get_int(j);
312 printf("ttrss_get_feeds ended\n");