c22cf342d133802c889770081f7b0c798dc24c71
[prss.git] / src / ttrss.c
1 /*
2  * Copyright (C) 2010-2013 jeanfi@gmail.com
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301 USA
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22
23 #include <json/json.h>
24
25 #include "phttp.h"
26 #include "ttrss.h"
27 #include "url.h"
28
29 static char *session_id;
30 static char *session_url;
31
32 static struct json_object *create_op(const char *op)
33 {
34         struct json_object *j;
35
36         j = json_object_new_object();
37         json_object_object_add(j, "op", json_object_new_string(op));
38
39         if (session_id && strcmp(op, "login"))
40                 json_object_object_add(j,
41                                        "sid",
42                                        json_object_new_string(session_id));
43
44         return j;
45 }
46
47 void ttrss_login(const char *url, const char *user, const char *password)
48 {
49         struct json_object *content, *rp, *error, *sid, *rq;
50         char *tmp;
51
52         if (session_url)
53                 free(session_url);
54
55         tmp = url_normalize(url);
56         session_url = malloc(strlen(tmp) + strlen("/api/") + 1);
57         strcpy(session_url, tmp);
58         strcat(session_url, "/api/");
59         free(tmp);
60
61
62         rq = create_op("login");
63         json_object_object_add(rq, "user", json_object_new_string(user));
64         json_object_object_add(rq,
65                                "password",
66                                json_object_new_string(password));
67
68         rp = post_json_object(session_url, rq);
69         json_object_put(rq);
70
71         content = json_object_object_get(rp, "content");
72         if (!content) {
73                 fprintf(stderr, "Login failed: no content\n");
74                 return ;
75         }
76
77         error = json_object_object_get(content, "error");
78         if (error) {
79                 fprintf(stderr, "Login failed\n");
80                 return ;
81         }
82
83         sid = json_object_object_get(content, "session_id");
84
85         if (session_id) {
86                 free(session_id);
87                 session_id = NULL;
88         }
89
90         session_id = strdup(json_object_get_string(sid));
91
92         printf("Session id: %s\n", session_id);
93
94         json_object_put(rp);
95 }
96
97 const char *ttrss_get_headline_content(struct headline *h)
98 {
99         struct json_object *rp, *rq, *content, *array, *item;
100
101         printf("get_headlines %d\n", h->id);
102
103         if (!h->content) {
104                 rq = create_op("getArticle");
105                 json_object_object_add(rq, "article_id",
106                                        json_object_new_int(h->id));
107                 
108                 rp = post_json_object(session_url, rq);
109                 
110                 json_object_put(rq);
111                 
112                 array = json_object_object_get(rp, "content");
113
114                 if (!array)
115                         goto release;
116
117                 item = json_object_array_get_idx(array, 0);
118
119                 if (!item)
120                         goto release;
121
122                 content = json_object_object_get(item, "content");
123
124                 h->content = strdup(json_object_get_string(content));
125                         
126         release:
127                 json_object_put(rp);
128         }
129         return h->content;
130 }
131
132 static struct headline **get_headlines(int feed_id)
133 {
134         struct json_object *rp, *rq, *content, *jheadline, *j;
135         int i, n;
136         struct headline **headlines, *h;
137
138         printf("get_headlines %d\n", feed_id);
139
140         rq = create_op("getHeadlines");
141         json_object_object_add(rq, "feed_id", json_object_new_int(feed_id));
142
143         rp = post_json_object(session_url, rq);
144
145         json_object_put(rq);
146
147         content = json_object_object_get(rp, "content");
148
149         if (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);
154
155                         h = malloc(sizeof(struct headline));
156
157                         j = json_object_object_get(jheadline, "id");
158                         h->id = json_object_get_int(j);
159
160                         j = json_object_object_get(jheadline, "title");
161                         h->title = strdup(json_object_get_string(j));
162
163                         h->excerpt = NULL;
164                         h->content = NULL;
165
166                         j = json_object_object_get(jheadline, "unread");
167                         h->unread = json_object_get_boolean(j);
168
169                         headlines[i] = h;
170                 }
171                 headlines[n] = NULL;
172         } else {
173                 headlines = NULL;
174         }
175
176         json_object_put(rp);
177
178         printf("get_headlines %d end\n", feed_id);
179
180         return headlines;
181 }
182
183 static int feeds_length(struct feed **list)
184 {
185         int n;
186
187         if (!list)
188                 return 0;
189         
190         n = 0;
191         while(*list) {
192                 n++;
193                 list++;
194         }
195
196         return n;
197 }
198
199 static struct feed **feeds_add(struct feed **feeds, struct feed *feed)
200 {
201         int n;
202         struct feed **result;
203
204         n = feeds_length(feeds);
205
206         result = malloc((n + 1 + 1) * sizeof(struct feed *));
207
208         if (feeds)
209                 memcpy(result, feeds, n * sizeof(struct feed *));
210
211         result[n] = feed;
212         result[n + 1] = NULL;
213
214         return result;
215 }
216
217 struct feed **ttrss_get_feeds()
218 {
219         struct json_object *rp, *rq, *content, *jfeed, *j;
220         int i, n;
221         struct feed **feeds, *feed, **tmp;
222
223         printf("ttrss_get_feeds\n");
224
225         rq = create_op("getFeeds");
226
227         rp = post_json_object(session_url, rq);
228         json_object_put(rq);
229
230         content = json_object_object_get(rp, "content");
231
232         if (content) {
233                 n = json_object_array_length(content);
234
235                 feeds = NULL;
236                 for (i = 0; i < n; i++) {
237                         jfeed = json_object_array_get_idx(content, i);
238
239                         feed = malloc(sizeof(struct feed));
240
241                         j = json_object_object_get(jfeed, "title");
242                         feed->title = strdup(json_object_get_string(j));
243
244                         j = json_object_object_get(jfeed, "feed_url");
245                         feed->url = strdup(json_object_get_string(j));
246
247                         j = json_object_object_get(jfeed, "id");
248                         feed->id = json_object_get_int(j);
249
250                         j = json_object_object_get(jfeed, "unread");
251                         feed->unread = json_object_get_int(j);
252
253                         feed->headlines = NULL;
254
255                         tmp = feeds_add(feeds, feed);
256                         free(feeds);
257                         feeds = tmp;
258                 }
259         } else {
260                 feeds = NULL;
261         }
262
263         json_object_put(rp);
264
265         printf("ttrss_get_feeds ended\n");
266
267         return feeds;
268 }
269
270 struct headline **ttrss_get_headlines(struct feed *f)
271 {
272         if (!f->headlines)
273                 f->headlines = get_headlines(f->id);
274
275         return f->headlines;
276 }