(no commit message)
[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");
74                 return ;
75         }
76
77         error = json_object_object_get(content, "error");
78         if (error) {
79                 fprintf(stderr, "Login failed");
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 static struct headline **get_headlines(int feed_id)
98 {
99         struct json_object *rp, *rq, *content, *jheadline, *j;
100         int i, n;
101         struct headline **headlines, *h;
102
103         printf("get_headlines %d\n", feed_id);
104
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));
109
110         rp = post_json_object(session_url, rq);
111
112         json_object_put(rq);
113
114         content = json_object_object_get(rp, "content");
115
116         if (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);
121
122                         h = malloc(sizeof(struct headline));
123
124                         j = json_object_object_get(jheadline, "title");
125                         h->title = strdup(json_object_get_string(j));
126
127                         j = json_object_object_get(jheadline, "excerpt");
128                         h->excerpt = strdup(json_object_get_string(j));
129
130                         j = json_object_object_get(jheadline, "content");
131                         h->content = strdup(json_object_get_string(j));
132
133                         j = json_object_object_get(jheadline, "unread");
134                         h->unread = json_object_get_boolean(j);
135
136                         headlines[i] = h;
137                 }
138                 headlines[n] = NULL;
139         } else {
140                 headlines = NULL;
141         }
142
143         json_object_put(rp);
144
145         printf("get_headlines %d end\n", feed_id);
146
147         return headlines;
148 }
149
150 struct feed **ttrss_get_feeds()
151 {
152         struct json_object *rp, *rq, *content, *jfeed, *j;
153         int i, n;
154         struct feed **feeds, *feed;
155
156         printf("ttrss_get_feeds\n");
157
158         rq = create_op("getFeeds");
159
160         rp = post_json_object(session_url, rq);
161         json_object_put(rq);
162
163         content = json_object_object_get(rp, "content");
164
165         if (content) {
166                 n = json_object_array_length(content);
167
168                 feeds = malloc((n+1) * sizeof(struct feed *));
169                 for (i = 0; i < n; i++) {
170                         jfeed = json_object_array_get_idx(content, i);
171
172                         feed = malloc(sizeof(struct feed));
173
174                         j = json_object_object_get(jfeed, "title");
175                         feed->title = strdup(json_object_get_string(j));
176
177                         j = json_object_object_get(jfeed, "feed_url");
178                         feed->url = strdup(json_object_get_string(j));
179
180                         j = json_object_object_get(jfeed, "id");
181                         feed->id = json_object_get_int(j);
182
183                         j = json_object_object_get(jfeed, "unread");
184                         feed->unread = json_object_get_int(j);
185
186                         feed->headlines = NULL;
187
188                         feeds[i] = feed;
189                 }
190                 feeds[n] = NULL;
191         } else {
192                 feeds = NULL;
193         }
194
195         json_object_put(rp);
196
197         printf("ttrss_get_feeds ended\n");
198
199         return feeds;
200 }
201
202 struct headline **ttrss_get_headlines(struct feed *f)
203 {
204         if (!f->headlines)
205                 f->headlines = get_headlines(f->id);
206
207         return f->headlines;
208 }