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 static struct feed **data;
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)
69 session_user = strdup(user);
73 session_pwd = strdup(pwd);
78 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;
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 h = malloc(sizeof(struct headline));
242 tmp = headlines_add(feed->headlines, h);
244 free(feed->headlines);
245 feed->headlines = tmp;
249 j = json_object_object_get(jheadline, "title");
250 h->title = strdup(json_object_get_string(j));
254 j = json_object_object_get(jheadline, "link");
255 h->url = strdup(json_object_get_string(j));
258 j = json_object_object_get(jheadline, "unread");
259 h->unread = json_object_get_boolean(j);
270 const char *ttrss_get_headline_content(struct headline *h)
273 h->content = ws_get_article_content(h->id);
278 struct feed **ttrss_get_feeds()
280 struct json_object *rp, *rq, *content, *jfeed, *j;
282 struct feed **feeds, *feed, **tmp;
284 printf("ttrss_get_feeds\n");
286 rq = ws_request_new("getFeeds");
288 rp = http_json_get(session_url, rq);
291 content = json_object_object_get(rp, "content");
294 n = json_object_array_length(content);
297 for (i = 0; i < n; i++) {
298 jfeed = json_object_array_get_idx(content, i);
300 feed = malloc(sizeof(struct feed));
302 j = json_object_object_get(jfeed, "title");
303 feed->title = strdup(json_object_get_string(j));
305 j = json_object_object_get(jfeed, "feed_url");
306 feed->url = strdup(json_object_get_string(j));
308 j = json_object_object_get(jfeed, "id");
309 feed->id = json_object_get_int(j);
311 j = json_object_object_get(jfeed, "unread");
312 feed->unread = json_object_get_int(j);
314 feed->headlines = NULL;
316 tmp = feeds_add(feeds, feed);
326 printf("ttrss_get_feeds ended\n");
331 struct headline **ttrss_get_headlines(struct feed *f)
334 ws_update_headlines(f);
339 void ttrss_set_article_unread(int id, int unread)
341 struct json_object *rp, *rq;
343 printf("ttrss_set_article_unread %d %d\n", id, unread);
345 rq = ws_request_new("updateArticle");
346 json_object_object_add(rq, "article_ids", json_object_new_int(id));
347 json_object_object_add(rq, "field", json_object_new_int(2));
348 json_object_object_add(rq, "mode", json_object_new_int(unread));
350 rp = http_json_get(session_url, rq);
356 void ttrss_set_config(const char *url, const char *user, const char *pwd)
360 ws_init(url, user, pwd);