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>
30 static char *session_id;
31 static char *session_url;
33 static struct json_object *create_op(const char *op)
35 struct json_object *j;
37 j = json_object_new_object();
38 json_object_object_add(j, "op", json_object_new_string(op));
40 if (session_id && strcmp(op, "login"))
41 json_object_object_add(j,
43 json_object_new_string(session_id));
48 void ttrss_login(const char *url, const char *user, const char *password)
50 struct json_object *content, *rp, *error, *sid, *rq;
56 tmp = url_normalize(url);
57 session_url = malloc(strlen(tmp) + strlen("/api/") + 1);
58 strcpy(session_url, tmp);
59 strcat(session_url, "/api/");
62 rq = create_op("login");
63 json_object_object_add(rq, "user", json_object_new_string(user));
64 json_object_object_add(rq,
66 json_object_new_string(password));
68 rp = http_json_get(session_url, rq);
71 content = json_object_object_get(rp, "content");
73 fprintf(stderr, "Login failed: no content\n");
77 error = json_object_object_get(content, "error");
79 fprintf(stderr, "Login failed\n");
83 sid = json_object_object_get(content, "session_id");
90 session_id = strdup(json_object_get_string(sid));
92 printf("Session id: %s\n", session_id);
97 const char *ttrss_get_headline_content(struct headline *h)
99 struct json_object *rp, *rq, *content, *array, *item;
101 printf("get_headlines %d\n", h->id);
104 rq = create_op("getArticle");
105 json_object_object_add(rq, "article_id",
106 json_object_new_int(h->id));
108 rp = http_json_get(session_url, rq);
112 array = json_object_object_get(rp, "content");
117 item = json_object_array_get_idx(array, 0);
122 content = json_object_object_get(item, "content");
124 h->content = strdup(json_object_get_string(content));
132 static struct headline **get_headlines(int feed_id)
134 struct json_object *rp, *rq, *content, *jheadline, *j;
136 struct headline **headlines, *h;
138 printf("get_headlines %d\n", feed_id);
140 rq = create_op("getHeadlines");
141 json_object_object_add(rq, "feed_id", json_object_new_int(feed_id));
143 rp = http_json_get(session_url, rq);
147 content = json_object_object_get(rp, "content");
150 n = json_object_array_length(content);
151 headlines = malloc((n+1)*sizeof(struct headline *));
152 for (i = 0; i < n; i++) {
153 jheadline = json_object_array_get_idx(content, i);
155 h = malloc(sizeof(struct headline));
157 j = json_object_object_get(jheadline, "id");
158 h->id = json_object_get_int(j);
160 j = json_object_object_get(jheadline, "title");
161 h->title = strdup(json_object_get_string(j));
163 j = json_object_object_get(jheadline, "link");
164 h->url = strdup(json_object_get_string(j));
169 j = json_object_object_get(jheadline, "unread");
170 h->unread = json_object_get_boolean(j);
181 printf("get_headlines %d end\n", feed_id);
186 static int feeds_length(struct feed **list)
202 static struct feed **feeds_add(struct feed **feeds, struct feed *feed)
205 struct feed **result;
207 n = feeds_length(feeds);
209 result = malloc((n + 1 + 1) * sizeof(struct feed *));
212 memcpy(result, feeds, n * sizeof(struct feed *));
215 result[n + 1] = NULL;
220 struct feed **ttrss_get_feeds()
222 struct json_object *rp, *rq, *content, *jfeed, *j;
224 struct feed **feeds, *feed, **tmp;
226 printf("ttrss_get_feeds\n");
228 rq = create_op("getFeeds");
230 rp = http_json_get(session_url, rq);
233 content = json_object_object_get(rp, "content");
236 n = json_object_array_length(content);
239 for (i = 0; i < n; i++) {
240 jfeed = json_object_array_get_idx(content, i);
242 feed = malloc(sizeof(struct feed));
244 j = json_object_object_get(jfeed, "title");
245 feed->title = strdup(json_object_get_string(j));
247 j = json_object_object_get(jfeed, "feed_url");
248 feed->url = strdup(json_object_get_string(j));
250 j = json_object_object_get(jfeed, "id");
251 feed->id = json_object_get_int(j);
253 j = json_object_object_get(jfeed, "unread");
254 feed->unread = json_object_get_int(j);
256 feed->headlines = NULL;
258 tmp = feeds_add(feeds, feed);
268 printf("ttrss_get_feeds ended\n");
273 struct headline **ttrss_get_headlines(struct feed *f)
276 f->headlines = get_headlines(f->id);
281 void ttrss_set_article_unread(int id, int unread)
283 struct json_object *rp, *rq;
285 printf("ttrss_set_article_unread %d %d\n", id, unread);
287 rq = create_op("updateArticle");
288 json_object_object_add(rq, "article_ids", json_object_new_int(id));
289 json_object_object_add(rq, "field", json_object_new_int(2));
290 json_object_object_add(rq, "mode", json_object_new_int(unread));
292 rp = http_json_get(session_url, rq);