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)
67 session_user = strdup(user);
71 session_pwd = strdup(pwd);
76 tmp = url_normalize(url);
77 session_url = malloc(strlen(tmp) + strlen("/api/") + 1);
78 strcpy(session_url, tmp);
79 strcat(session_url, "/api/");
83 struct json_object *ws_reply_get_content(struct json_object *rp)
85 return json_object_object_get(rp, "content");
88 struct json_object *ws_execute(struct json_object *rq)
90 struct json_object *rp, *content;
92 rp = http_json_get(session_url, rq);
95 content = ws_reply_get_content(rp);
97 if (content && !json_object_object_get(rp, "error"))
106 int ws_get_api_version()
108 struct json_object *rp, *rq, *j;
111 rq = ws_request_new("getApiLevel");
118 j = json_object_object_get(rp, "level");
121 v = json_object_get_int(j);
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");
149 str = strdup(json_object_get_string(j));
161 int ws_open_session()
168 session_id = ws_login();
171 version = ws_get_api_version();
172 printf("API version: %d\n", version);
188 const char *ttrss_get_headline_content(struct headline *h)
190 struct json_object *rp, *rq, *content, *array, *item;
192 printf("get_headlines %d\n", h->id);
195 rq = ws_request_new("getArticle");
196 json_object_object_add(rq, "article_id",
197 json_object_new_int(h->id));
199 rp = http_json_get(session_url, rq);
203 array = json_object_object_get(rp, "content");
208 item = json_object_array_get_idx(array, 0);
213 content = json_object_object_get(item, "content");
215 h->content = strdup(json_object_get_string(content));
223 static struct headline **get_headlines(int feed_id)
225 struct json_object *rp, *rq, *content, *jheadline, *j;
227 struct headline **headlines, *h;
229 printf("get_headlines %d\n", feed_id);
231 rq = ws_request_new("getHeadlines");
232 json_object_object_add(rq, "feed_id", json_object_new_int(feed_id));
234 rp = http_json_get(session_url, rq);
238 content = json_object_object_get(rp, "content");
241 n = json_object_array_length(content);
242 headlines = malloc((n+1)*sizeof(struct headline *));
243 for (i = 0; i < n; i++) {
244 jheadline = json_object_array_get_idx(content, i);
246 h = malloc(sizeof(struct headline));
248 j = json_object_object_get(jheadline, "id");
249 h->id = json_object_get_int(j);
251 j = json_object_object_get(jheadline, "title");
252 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));
260 j = json_object_object_get(jheadline, "unread");
261 h->unread = json_object_get_boolean(j);
272 printf("get_headlines %d end\n", feed_id);
277 static int feeds_length(struct feed **list)
293 static struct feed **feeds_add(struct feed **feeds, struct feed *feed)
296 struct feed **result;
298 n = feeds_length(feeds);
300 result = malloc((n + 1 + 1) * sizeof(struct feed *));
303 memcpy(result, feeds, n * sizeof(struct feed *));
306 result[n + 1] = NULL;
311 struct feed **ttrss_get_feeds()
313 struct json_object *rp, *rq, *content, *jfeed, *j;
315 struct feed **feeds, *feed, **tmp;
317 printf("ttrss_get_feeds\n");
319 rq = ws_request_new("getFeeds");
321 rp = http_json_get(session_url, rq);
324 content = json_object_object_get(rp, "content");
327 n = json_object_array_length(content);
330 for (i = 0; i < n; i++) {
331 jfeed = json_object_array_get_idx(content, i);
333 feed = malloc(sizeof(struct feed));
335 j = json_object_object_get(jfeed, "title");
336 feed->title = strdup(json_object_get_string(j));
338 j = json_object_object_get(jfeed, "feed_url");
339 feed->url = strdup(json_object_get_string(j));
341 j = json_object_object_get(jfeed, "id");
342 feed->id = json_object_get_int(j);
344 j = json_object_object_get(jfeed, "unread");
345 feed->unread = json_object_get_int(j);
347 feed->headlines = NULL;
349 tmp = feeds_add(feeds, feed);
359 printf("ttrss_get_feeds ended\n");
364 struct headline **ttrss_get_headlines(struct feed *f)
367 f->headlines = get_headlines(f->id);
372 void ttrss_set_article_unread(int id, int unread)
374 struct json_object *rp, *rq;
376 printf("ttrss_set_article_unread %d %d\n", id, unread);
378 rq = ws_request_new("updateArticle");
379 json_object_object_add(rq, "article_ids", json_object_new_int(id));
380 json_object_object_add(rq, "field", json_object_new_int(2));
381 json_object_object_add(rq, "mode", json_object_new_int(unread));
383 rp = http_json_get(session_url, rq);