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;
32 static struct json_object *create_op(const char *op)
34 struct json_object *j;
36 j = json_object_new_object();
37 json_object_object_add(j, "op", json_object_new_string(op));
39 if (session_id && strcmp(op, "login"))
40 json_object_object_add(j,
42 json_object_new_string(session_id));
47 void ttrss_login(const char *url, const char *user, const char *password)
49 struct json_object *content, *rp, *error, *sid, *rq;
55 tmp = url_normalize(url);
56 session_url = malloc(strlen(tmp) + strlen("/api/") + 1);
57 strcpy(session_url, tmp);
58 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 = post_json_object(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 static struct headline **get_headlines(int feed_id)
99 struct json_object *rp, *rq, *content, *jheadline, *j;
101 struct headline **headlines, *h;
103 printf("get_headlines %d\n", feed_id);
105 rq = create_op("getHeadlines");
106 json_object_object_add(rq, "feed_id", json_object_new_int(feed_id));
107 json_object_object_add(rq, "show_excerpt", json_object_new_boolean(1));
108 json_object_object_add(rq, "show_content", json_object_new_boolean(1));
110 rp = post_json_object(session_url, rq);
114 content = json_object_object_get(rp, "content");
117 n = json_object_array_length(content);
118 headlines = malloc((n+1)*sizeof(struct headline *));
119 for (i = 0; i < n; i++) {
120 jheadline = json_object_array_get_idx(content, i);
122 h = malloc(sizeof(struct headline));
124 j = json_object_object_get(jheadline, "title");
125 h->title = strdup(json_object_get_string(j));
127 j = json_object_object_get(jheadline, "excerpt");
128 h->excerpt = strdup(json_object_get_string(j));
130 j = json_object_object_get(jheadline, "content");
131 h->content = strdup(json_object_get_string(j));
133 j = json_object_object_get(jheadline, "unread");
134 h->unread = json_object_get_boolean(j);
145 printf("get_headlines %d end\n", feed_id);
150 static int feeds_length(struct feed **list)
166 static struct feed **feeds_add(struct feed **feeds, struct feed *feed)
169 struct feed **result;
171 n = feeds_length(feeds);
173 result = malloc((n + 1 + 1) * sizeof(struct feed *));
176 memcpy(result, feeds, n * sizeof(struct feed *));
179 result[n + 1] = NULL;
184 struct feed **ttrss_get_feeds()
186 struct json_object *rp, *rq, *content, *jfeed, *j;
188 struct feed **feeds, *feed, **tmp;
190 printf("ttrss_get_feeds\n");
192 rq = create_op("getFeeds");
194 rp = post_json_object(session_url, rq);
197 content = json_object_object_get(rp, "content");
200 n = json_object_array_length(content);
203 for (i = 0; i < n; i++) {
204 jfeed = json_object_array_get_idx(content, i);
206 feed = malloc(sizeof(struct feed));
208 j = json_object_object_get(jfeed, "title");
209 feed->title = strdup(json_object_get_string(j));
211 j = json_object_object_get(jfeed, "feed_url");
212 feed->url = strdup(json_object_get_string(j));
214 j = json_object_object_get(jfeed, "id");
215 feed->id = json_object_get_int(j);
217 j = json_object_object_get(jfeed, "unread");
218 feed->unread = json_object_get_int(j);
220 feed->headlines = NULL;
222 tmp = feeds_add(feeds, feed);
232 printf("ttrss_get_feeds ended\n");
237 struct headline **ttrss_get_headlines(struct feed *f)
240 f->headlines = get_headlines(f->id);