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")) {
98 json_object_get(content);
109 int ws_get_api_version()
111 struct json_object *rp, *rq;
114 rq = ws_request_new("getApiLevel");
121 v = json_object_get_int(json_object_object_get(rp, "level"));
133 struct json_object *rq, *rp, *j;
136 rq = ws_request_new("login");
137 ws_request_add_att_str(rq, "user", session_user);
138 ws_request_add_att_str(rq, "password", session_pwd);
144 j = json_object_object_get(rp, "session_id");
145 str = strdup(json_object_get_string(j));
155 int ws_open_session()
162 session_id = ws_login();
165 version = ws_get_api_version();
166 printf("API version: %d\n", version);
182 const char *ttrss_get_headline_content(struct headline *h)
184 struct json_object *rp, *rq, *content, *array, *item;
186 printf("get_headlines %d\n", h->id);
189 rq = ws_request_new("getArticle");
190 json_object_object_add(rq, "article_id",
191 json_object_new_int(h->id));
193 rp = http_json_get(session_url, rq);
197 array = json_object_object_get(rp, "content");
202 item = json_object_array_get_idx(array, 0);
207 content = json_object_object_get(item, "content");
209 h->content = strdup(json_object_get_string(content));
217 static struct headline **get_headlines(int feed_id)
219 struct json_object *rp, *rq, *content, *jheadline, *j;
221 struct headline **headlines, *h;
223 printf("get_headlines %d\n", feed_id);
225 rq = ws_request_new("getHeadlines");
226 json_object_object_add(rq, "feed_id", json_object_new_int(feed_id));
228 rp = http_json_get(session_url, rq);
232 content = json_object_object_get(rp, "content");
235 n = json_object_array_length(content);
236 headlines = malloc((n+1)*sizeof(struct headline *));
237 for (i = 0; i < n; i++) {
238 jheadline = json_object_array_get_idx(content, i);
240 h = malloc(sizeof(struct headline));
242 j = json_object_object_get(jheadline, "id");
243 h->id = json_object_get_int(j);
245 j = json_object_object_get(jheadline, "title");
246 h->title = strdup(json_object_get_string(j));
248 j = json_object_object_get(jheadline, "link");
249 h->url = strdup(json_object_get_string(j));
254 j = json_object_object_get(jheadline, "unread");
255 h->unread = json_object_get_boolean(j);
266 printf("get_headlines %d end\n", feed_id);
271 static int feeds_length(struct feed **list)
287 static struct feed **feeds_add(struct feed **feeds, struct feed *feed)
290 struct feed **result;
292 n = feeds_length(feeds);
294 result = malloc((n + 1 + 1) * sizeof(struct feed *));
297 memcpy(result, feeds, n * sizeof(struct feed *));
300 result[n + 1] = NULL;
305 struct feed **ttrss_get_feeds()
307 struct json_object *rp, *rq, *content, *jfeed, *j;
309 struct feed **feeds, *feed, **tmp;
311 printf("ttrss_get_feeds\n");
313 rq = ws_request_new("getFeeds");
315 rp = http_json_get(session_url, rq);
318 content = json_object_object_get(rp, "content");
321 n = json_object_array_length(content);
324 for (i = 0; i < n; i++) {
325 jfeed = json_object_array_get_idx(content, i);
327 feed = malloc(sizeof(struct feed));
329 j = json_object_object_get(jfeed, "title");
330 feed->title = strdup(json_object_get_string(j));
332 j = json_object_object_get(jfeed, "feed_url");
333 feed->url = strdup(json_object_get_string(j));
335 j = json_object_object_get(jfeed, "id");
336 feed->id = json_object_get_int(j);
338 j = json_object_object_get(jfeed, "unread");
339 feed->unread = json_object_get_int(j);
341 feed->headlines = NULL;
343 tmp = feeds_add(feeds, feed);
353 printf("ttrss_get_feeds ended\n");
358 struct headline **ttrss_get_headlines(struct feed *f)
361 f->headlines = get_headlines(f->id);
366 void ttrss_set_article_unread(int id, int unread)
368 struct json_object *rp, *rq;
370 printf("ttrss_set_article_unread %d %d\n", id, unread);
372 rq = ws_request_new("updateArticle");
373 json_object_object_add(rq, "article_ids", json_object_new_int(id));
374 json_object_object_add(rq, "field", json_object_new_int(2));
375 json_object_object_add(rq, "mode", json_object_new_int(unread));
377 rp = http_json_get(session_url, rq);