(no commit message)
[prss.git] / src / ttrss.c
index 4dbde65..2c57b4e 100644 (file)
@@ -70,13 +70,13 @@ void ttrss_login(const char *url, const char *user, const char *password)
 
        content = json_object_object_get(rp, "content");
        if (!content) {
-               fprintf(stderr, "Login failed: no content");
+               fprintf(stderr, "Login failed: no content\n");
                return ;
        }
 
        error = json_object_object_get(content, "error");
        if (error) {
-               fprintf(stderr, "Login failed");
+               fprintf(stderr, "Login failed\n");
                return ;
        }
 
@@ -94,61 +94,51 @@ void ttrss_login(const char *url, const char *user, const char *password)
        json_object_put(rp);
 }
 
-struct feed **ttrss_get_feeds()
+const char *ttrss_get_headline_content(struct headline *h)
 {
-       struct json_object *rp, *rq, *content, *jfeed, *j;
-       int i, n;
-       struct feed **feeds, *feed;
-
-       rq = create_op("getFeeds");
-
-       rp = post_json_object(session_url, rq);
-       json_object_put(rq);
-
-       content = json_object_object_get(rp, "content");
-
-       if (content) {
-               n = json_object_array_length(content);
+       struct json_object *rp, *rq, *content, *array, *item;
 
-               feeds = malloc((n+1) * sizeof(struct feed *));
-               for (i = 0; i < n; i++) {
-                       jfeed = json_object_array_get_idx(content, i);
+       printf("get_headlines %d\n", h->id);
 
-                       feed = malloc(sizeof(struct feed));
+       if (!h->content) {
+               rq = create_op("getArticle");
+               json_object_object_add(rq, "article_id",
+                                      json_object_new_int(h->id));
+               
+               rp = post_json_object(session_url, rq);
+               
+               json_object_put(rq);
+               
+               array = json_object_object_get(rp, "content");
 
-                       j = json_object_object_get(jfeed, "title");
-                       feed->title = strdup(json_object_get_string(j));
+               if (!array)
+                       goto release;
 
-                       j = json_object_object_get(jfeed, "feed_url");
-                       feed->url = strdup(json_object_get_string(j));
+               item = json_object_array_get_idx(array, 0);
 
-                       j = json_object_object_get(jfeed, "id");
-                       feed->id = json_object_get_int(j);
+               if (!item)
+                       goto release;
 
-                       feed->headlines = ttrss_get_headlines(feed->id);
+               content = json_object_object_get(item, "content");
 
-                       feeds[i] = feed;
-               }
-               feeds[n] = NULL;
-       } else {
-               feeds = NULL;
+               h->content = strdup(json_object_get_string(content));
+                       
+       release:
+               json_object_put(rp);
        }
-
-       json_object_put(rp);
-
-       return feeds;
+       return h->content;
 }
 
-struct headline **ttrss_get_headlines(int feed_id)
+static struct headline **get_headlines(int feed_id)
 {
        struct json_object *rp, *rq, *content, *jheadline, *j;
        int i, n;
        struct headline **headlines, *h;
 
+       printf("get_headlines %d\n", feed_id);
+
        rq = create_op("getHeadlines");
        json_object_object_add(rq, "feed_id", json_object_new_int(feed_id));
-       json_object_object_add(rq, "show_excerpt", json_object_new_boolean(1));
-       json_object_object_add(rq, "show_content", json_object_new_boolean(1));
 
        rp = post_json_object(session_url, rq);
 
@@ -164,14 +154,17 @@ struct headline **ttrss_get_headlines(int feed_id)
 
                        h = malloc(sizeof(struct headline));
 
+                       j = json_object_object_get(jheadline, "id");
+                       h->id = json_object_get_int(j);
+
                        j = json_object_object_get(jheadline, "title");
                        h->title = strdup(json_object_get_string(j));
 
-                       j = json_object_object_get(jheadline, "excerpt");
-                       h->excerpt = strdup(json_object_get_string(j));
+                       j = json_object_object_get(jheadline, "link");
+                       h->url = strdup(json_object_get_string(j));
 
-                       j = json_object_object_get(jheadline, "content");
-                       h->content = strdup(json_object_get_string(j));
+                       h->excerpt = NULL;
+                       h->content = NULL;
 
                        j = json_object_object_get(jheadline, "unread");
                        h->unread = json_object_get_boolean(j);
@@ -185,5 +178,102 @@ struct headline **ttrss_get_headlines(int feed_id)
 
        json_object_put(rp);
 
+       printf("get_headlines %d end\n", feed_id);
+
        return headlines;
 }
+
+static int feeds_length(struct feed **list)
+{
+       int n;
+
+       if (!list)
+               return 0;
+       
+       n = 0;
+       while(*list) {
+               n++;
+               list++;
+       }
+
+       return n;
+}
+
+static struct feed **feeds_add(struct feed **feeds, struct feed *feed)
+{
+       int n;
+       struct feed **result;
+
+       n = feeds_length(feeds);
+
+       result = malloc((n + 1 + 1) * sizeof(struct feed *));
+
+       if (feeds)
+               memcpy(result, feeds, n * sizeof(struct feed *));
+
+       result[n] = feed;
+       result[n + 1] = NULL;
+
+       return result;
+}
+
+struct feed **ttrss_get_feeds()
+{
+       struct json_object *rp, *rq, *content, *jfeed, *j;
+       int i, n;
+       struct feed **feeds, *feed, **tmp;
+
+       printf("ttrss_get_feeds\n");
+
+       rq = create_op("getFeeds");
+
+       rp = post_json_object(session_url, rq);
+       json_object_put(rq);
+
+       content = json_object_object_get(rp, "content");
+
+       if (content) {
+               n = json_object_array_length(content);
+
+               feeds = NULL;
+               for (i = 0; i < n; i++) {
+                       jfeed = json_object_array_get_idx(content, i);
+
+                       feed = malloc(sizeof(struct feed));
+
+                       j = json_object_object_get(jfeed, "title");
+                       feed->title = strdup(json_object_get_string(j));
+
+                       j = json_object_object_get(jfeed, "feed_url");
+                       feed->url = strdup(json_object_get_string(j));
+
+                       j = json_object_object_get(jfeed, "id");
+                       feed->id = json_object_get_int(j);
+
+                       j = json_object_object_get(jfeed, "unread");
+                       feed->unread = json_object_get_int(j);
+
+                       feed->headlines = NULL;
+
+                       tmp = feeds_add(feeds, feed);
+                       free(feeds);
+                       feeds = tmp;
+               }
+       } else {
+               feeds = NULL;
+       }
+
+       json_object_put(rp);
+
+       printf("ttrss_get_feeds ended\n");
+
+       return feeds;
+}
+
+struct headline **ttrss_get_headlines(struct feed *f)
+{
+       if (!f->headlines)
+               f->headlines = get_headlines(f->id);
+
+       return f->headlines;
+}