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>
30 static char *session_id;
31 static char *session_url;
32 static char *session_user;
33 static char *session_pwd;
35 void ws_request_add_att_str(json_object *rq, const char *k, const char *str)
37 json_object_object_add(rq, k, json_object_new_string(str));
40 void ws_request_add_att_int(json_object *rq, const char *k, int v)
42 json_object_object_add(rq, k, json_object_new_int(v));
45 struct json_object *ws_request_new(const char *op)
47 struct json_object *rq;
49 rq = json_object_new_object();
51 ws_request_add_att_str(rq, "op", op);
54 ws_request_add_att_str(rq, "sid", session_id);
59 void ws_init(const char *url, const char *user, const char *pwd)
63 if (session_url && !strcmp(session_url, url)
64 && session_user && !strcmp(session_user, user)
65 && session_pwd && !strcmp(session_pwd, pwd))
72 session_user = strdup(user);
75 session_pwd = strdup(pwd);
77 tmp = url_normalize(url);
79 session_url = malloc(strlen(tmp) + strlen("/api/") + 1);
80 strcpy(session_url, tmp);
81 strcat(session_url, "/api/");
85 struct json_object *ws_reply_get_content(struct json_object *rp)
87 return json_object_object_get(rp, "content");
90 struct json_object *ws_execute(struct json_object *rq)
92 struct json_object *rp, *content;
94 rp = http_json_get(session_url, rq);
97 content = ws_reply_get_content(rp);
99 if (content && !json_object_object_get(rp, "error")) {
100 json_object_get(content);
111 int ws_get_api_version()
113 struct json_object *rp, *rq;
116 rq = ws_request_new("getApiLevel");
123 v = json_object_get_int(json_object_object_get(rp, "level"));
135 struct json_object *rq, *rp, *j;
138 rq = ws_request_new("login");
139 ws_request_add_att_str(rq, "user", session_user);
140 ws_request_add_att_str(rq, "password", session_pwd);
146 j = json_object_object_get(rp, "session_id");
147 str = strdup(json_object_get_string(j));
157 int ws_open_session()
164 session_id = ws_login();
167 version = ws_get_api_version();
168 printf("API version: %d\n", version);
184 char *ws_get_article_content(int id)
186 struct json_object *rp, *rq, *content, *item;
189 rq = ws_request_new("getArticle");
190 ws_request_add_att_int(rq, "article_id", id);
199 item = json_object_array_get_idx(rp, 0);
202 content = json_object_object_get(item, "content");
203 str = strdup(json_object_get_string(content));
212 int ws_update_headlines(struct feed *feed)
214 struct json_object *rp, *rq, *jheadline, *j;
216 struct headline *h, **tmp;
217 const char *title, *url;
219 rq = ws_request_new("getHeadlines");
220 ws_request_add_att_int(rq, "feed_id", feed->id);
227 n = json_object_array_length(rp);
228 for (i = 0; i < n; i++) {
229 jheadline = json_object_array_get_idx(rp, i);
231 j = json_object_object_get(jheadline, "id");
232 hid = json_object_get_int(j);
233 h = feed_get_headline(feed, hid);
236 j = json_object_object_get(jheadline, "title");
237 title = json_object_get_string(j);
239 j = json_object_object_get(jheadline, "link");
240 url = json_object_get_string(j);
242 h = headline_new(hid, url, title);
244 tmp = headlines_add(feed->headlines, h);
246 free(feed->headlines);
247 feed->headlines = tmp;
250 j = json_object_object_get(jheadline, "unread");
251 h->unread = json_object_get_boolean(j);
262 struct feed **ws_update_feeds(struct feed **feeds)
264 struct json_object *rp, *rq, *jfeed, *j;
266 struct feed *feed, **tmp;
267 const char *title, *url;
269 printf("ttrss_get_feeds\n");
271 rq = ws_request_new("getFeeds");
277 n = json_object_array_length(rp);
279 for (i = 0; i < n; i++) {
280 jfeed = json_object_array_get_idx(rp, i);
282 j = json_object_object_get(jfeed, "id");
283 id = json_object_get_int(j);
285 feed = feeds_get_feed(feeds, id);
288 j = json_object_object_get(jfeed, "title");
289 title = json_object_get_string(j);
291 j = json_object_object_get(jfeed, "feed_url");
292 url = json_object_get_string(j);
294 feed = feed_new(id, url, title);
296 tmp = feeds_add(feeds, feed);
303 j = json_object_object_get(jfeed, "unread");
304 feed->unread = json_object_get_int(j);
313 printf("ttrss_get_feeds ended\n");