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/");
63 rq = create_op("login");
64 json_object_object_add(rq, "user", json_object_new_string(user));
65 json_object_object_add(rq,
67 json_object_new_string(password));
69 rp = post_json_object(session_url, rq);
72 content = json_object_object_get(rp, "content");
74 fprintf(stderr, "Login failed: no content\n");
78 error = json_object_object_get(content, "error");
80 fprintf(stderr, "Login failed\n");
84 sid = json_object_object_get(content, "session_id");
91 session_id = strdup(json_object_get_string(sid));
93 printf("Session id: %s\n", session_id);
98 const char *ttrss_get_headline_content(struct headline *h)
100 struct json_object *rp, *rq, *content, *array, *item;
102 printf("get_headlines %d\n", h->id);
105 rq = create_op("getArticle");
106 json_object_object_add(rq, "article_id",
107 json_object_new_int(h->id));
109 rp = post_json_object(session_url, rq);
113 array = json_object_object_get(rp, "content");
118 item = json_object_array_get_idx(array, 0);
123 content = json_object_object_get(item, "content");
125 h->content = strdup(json_object_get_string(content));
133 static struct headline **get_headlines(int feed_id)
135 struct json_object *rp, *rq, *content, *jheadline, *j;
137 struct headline **headlines, *h;
139 printf("get_headlines %d\n", feed_id);
141 rq = create_op("getHeadlines");
142 json_object_object_add(rq, "feed_id", json_object_new_int(feed_id));
144 rp = post_json_object(session_url, rq);
148 content = json_object_object_get(rp, "content");
151 n = json_object_array_length(content);
152 headlines = malloc((n+1)*sizeof(struct headline *));
153 for (i = 0; i < n; i++) {
154 jheadline = json_object_array_get_idx(content, i);
156 h = malloc(sizeof(struct headline));
158 j = json_object_object_get(jheadline, "id");
159 h->id = json_object_get_int(j);
161 j = json_object_object_get(jheadline, "title");
162 h->title = strdup(json_object_get_string(j));
164 j = json_object_object_get(jheadline, "link");
165 h->url = strdup(json_object_get_string(j));
170 j = json_object_object_get(jheadline, "unread");
171 h->unread = json_object_get_boolean(j);
182 printf("get_headlines %d end\n", feed_id);
187 static int feeds_length(struct feed **list)
203 static struct feed **feeds_add(struct feed **feeds, struct feed *feed)
206 struct feed **result;
208 n = feeds_length(feeds);
210 result = malloc((n + 1 + 1) * sizeof(struct feed *));
213 memcpy(result, feeds, n * sizeof(struct feed *));
216 result[n + 1] = NULL;
221 struct feed **ttrss_get_feeds()
223 struct json_object *rp, *rq, *content, *jfeed, *j;
225 struct feed **feeds, *feed, **tmp;
227 printf("ttrss_get_feeds\n");
229 rq = create_op("getFeeds");
231 rp = post_json_object(session_url, rq);
234 content = json_object_object_get(rp, "content");
237 n = json_object_array_length(content);
240 for (i = 0; i < n; i++) {
241 jfeed = json_object_array_get_idx(content, i);
243 feed = malloc(sizeof(struct feed));
245 j = json_object_object_get(jfeed, "title");
246 feed->title = strdup(json_object_get_string(j));
248 j = json_object_object_get(jfeed, "feed_url");
249 feed->url = strdup(json_object_get_string(j));
251 j = json_object_object_get(jfeed, "id");
252 feed->id = json_object_get_int(j);
254 j = json_object_object_get(jfeed, "unread");
255 feed->unread = json_object_get_int(j);
257 feed->headlines = NULL;
259 tmp = feeds_add(feeds, feed);
269 printf("ttrss_get_feeds ended\n");
274 struct headline **ttrss_get_headlines(struct feed *f)
277 f->headlines = get_headlines(f->id);
282 void ttrss_set_article_unread(int id, int unread)
284 struct json_object *rp, *rq;
286 printf("ttrss_set_article_unread %d %d\n", id, unread);
288 rq = create_op("updateArticle");
289 json_object_object_add(rq, "article_ids", json_object_new_int(id));
290 json_object_object_add(rq, "field", json_object_new_int(2));
291 json_object_object_add(rq, "mode", json_object_new_int(unread));
293 rp = post_json_object(session_url, rq);