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 int list_length(void **list)
50 static void **list_add(void **list, void *item)
55 n = list_length(list);
57 result = malloc((n + 1 + 1) * sizeof(void *));
60 memcpy(result, list, n * sizeof(void *));
69 headlines_add(struct headline **list, struct headline *h)
71 return (struct headline **)list_add((void **)list, (void *)h);
74 struct headline *feed_get_headline(struct feed *feed, int id)
76 struct headline **headlines;
78 headlines = feed->headlines;
81 if ((*headlines)->id == id)
89 void ws_request_add_att_str(json_object *rq, const char *k, const char *str)
91 json_object_object_add(rq, k, json_object_new_string(str));
94 void ws_request_add_att_int(json_object *rq, const char *k, int v)
96 json_object_object_add(rq, k, json_object_new_int(v));
99 struct json_object *ws_request_new(const char *op)
101 struct json_object *rq;
103 rq = json_object_new_object();
105 ws_request_add_att_str(rq, "op", op);
108 ws_request_add_att_str(rq, "sid", session_id);
113 void ws_init(const char *url, const char *user, const char *pwd)
122 session_user = strdup(user);
126 session_pwd = strdup(pwd);
131 tmp = url_normalize(url);
132 session_url = malloc(strlen(tmp) + strlen("/api/") + 1);
133 strcpy(session_url, tmp);
134 strcat(session_url, "/api/");
138 struct json_object *ws_reply_get_content(struct json_object *rp)
140 return json_object_object_get(rp, "content");
143 struct json_object *ws_execute(struct json_object *rq)
145 struct json_object *rp, *content;
147 rp = http_json_get(session_url, rq);
150 content = ws_reply_get_content(rp);
152 if (content && !json_object_object_get(rp, "error")) {
153 json_object_get(content);
164 int ws_get_api_version()
166 struct json_object *rp, *rq;
169 rq = ws_request_new("getApiLevel");
176 v = json_object_get_int(json_object_object_get(rp, "level"));
188 struct json_object *rq, *rp, *j;
191 rq = ws_request_new("login");
192 ws_request_add_att_str(rq, "user", session_user);
193 ws_request_add_att_str(rq, "password", session_pwd);
199 j = json_object_object_get(rp, "session_id");
200 str = strdup(json_object_get_string(j));
210 int ws_open_session()
217 session_id = ws_login();
220 version = ws_get_api_version();
221 printf("API version: %d\n", version);
237 char *ws_get_article_content(int id)
239 struct json_object *rp, *rq, *content, *item;
242 rq = ws_request_new("getArticle");
243 ws_request_add_att_int(rq, "article_id", id);
252 item = json_object_array_get_idx(rp, 0);
255 content = json_object_object_get(item, "content");
257 str = strdup(json_object_get_string(content));
266 int ws_update_headlines(struct feed *feed)
268 struct json_object *rp, *rq, *jheadline, *j;
270 struct headline *h, **tmp;
272 rq = ws_request_new("getHeadlines");
273 ws_request_add_att_int(rq, "feed_id", feed->id);
280 n = json_object_array_length(rp);
281 for (i = 0; i < n; i++) {
282 jheadline = json_object_array_get_idx(rp, i);
284 j = json_object_object_get(jheadline, "id");
285 hid = json_object_get_int(j);
286 h = feed_get_headline(feed, hid);
289 h = malloc(sizeof(struct headline));
296 tmp = headlines_add(feed->headlines, h);
298 free(feed->headlines);
299 feed->headlines = tmp;
303 j = json_object_object_get(jheadline, "title");
304 h->title = strdup(json_object_get_string(j));
308 j = json_object_object_get(jheadline, "link");
309 h->url = strdup(json_object_get_string(j));
312 j = json_object_object_get(jheadline, "unread");
313 h->unread = json_object_get_boolean(j);
324 const char *ttrss_get_headline_content(struct headline *h)
327 h->content = ws_get_article_content(h->id);
333 struct feed **ttrss_get_feeds()
335 struct json_object *rp, *rq, *content, *jfeed, *j;
337 struct feed **feeds, *feed, **tmp;
339 printf("ttrss_get_feeds\n");
341 rq = ws_request_new("getFeeds");
343 rp = http_json_get(session_url, rq);
346 content = json_object_object_get(rp, "content");
349 n = json_object_array_length(content);
352 for (i = 0; i < n; i++) {
353 jfeed = json_object_array_get_idx(content, i);
355 feed = malloc(sizeof(struct feed));
357 j = json_object_object_get(jfeed, "title");
358 feed->title = strdup(json_object_get_string(j));
360 j = json_object_object_get(jfeed, "feed_url");
361 feed->url = strdup(json_object_get_string(j));
363 j = json_object_object_get(jfeed, "id");
364 feed->id = json_object_get_int(j);
366 j = json_object_object_get(jfeed, "unread");
367 feed->unread = json_object_get_int(j);
369 feed->headlines = NULL;
371 tmp = (struct feed **)list_add((void **)feeds, feed);
381 printf("ttrss_get_feeds ended\n");
386 struct headline **ttrss_get_headlines(struct feed *f)
389 ws_update_headlines(f);
394 void ttrss_set_article_unread(int id, int unread)
396 struct json_object *rp, *rq;
398 printf("ttrss_set_article_unread %d %d\n", id, unread);
400 rq = ws_request_new("updateArticle");
401 json_object_object_add(rq, "article_ids", json_object_new_int(id));
402 json_object_object_add(rq, "field", json_object_new_int(2));
403 json_object_object_add(rq, "mode", json_object_new_int(unread));
405 rp = http_json_get(session_url, rq);