From a7e89c999775c195f8b7759f4c283fcedb041a9e Mon Sep 17 00:00:00 2001 From: Jean-Philippe Orsini Date: Mon, 22 Apr 2013 07:11:30 +0000 Subject: [PATCH] --- src/ttrss.c | 186 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ttrss.h | 41 ++++++++++++++ 2 files changed, 227 insertions(+) create mode 100644 src/ttrss.c create mode 100644 src/ttrss.h diff --git a/src/ttrss.c b/src/ttrss.c new file mode 100644 index 0000000..78884c1 --- /dev/null +++ b/src/ttrss.c @@ -0,0 +1,186 @@ +/* + * Copyright (C) 2010-2013 jeanfi@gmail.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#include +#include + +#include + +#include "phttp.h" +#include "ttrss.h" +#include "url.h" + +static char *session_id; +static char *session_url; + +static struct json_object *create_op(const char *op) +{ + struct json_object *j; + + j = json_object_new_object(); + json_object_object_add(j, "op", json_object_new_string(op)); + + if (session_id && strcmp(op, "login")) + json_object_object_add(j, + "sid", + json_object_new_string(session_id)); + + return j; +} + +void ttrss_login(const char *url, const char *user, const char *password) +{ + struct json_object *content, *rp, *error, *sid, *rq; + char *tmp; + + if (session_url) + free(session_url); + + tmp = url_normalize(url); + session_url = malloc(strlen(tmp) + strlen("/api/") + 1); + strcpy(session_url, tmp); + strcat(session_url, "/api/"); + free(tmp); + + + rq = create_op("login"); + json_object_object_add(rq, "user", json_object_new_string(user)); + json_object_object_add(rq, + "password", + json_object_new_string(password)); + + rp = post_json_object(session_url, rq); + json_object_put(rq); + + content = json_object_object_get(rp, "content"); + if (!content) { + fprintf(stderr, "Login failed: no content"); + return ; + } + + error = json_object_object_get(content, "error"); + if (error) { + fprintf(stderr, "Login failed"); + return ; + } + + sid = json_object_object_get(content, "session_id"); + + if (session_id) { + free(session_id); + session_id = NULL; + } + + session_id = strdup(json_object_get_string(sid)); + + printf("Session id: %s\n", session_id); + + json_object_put(rp); +} + +struct feed **ttrss_get_feeds() +{ + 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); + + feeds = malloc((n+1) * sizeof(struct feed *)); + 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); + + feed->headlines = ttrss_get_headlines(feed->id); + + feeds[i] = feed; + } + feeds[n] = NULL; + } else { + feeds = NULL; + } + + json_object_put(rp); + + return feeds; +} + +struct headline **ttrss_get_headlines(int feed_id) +{ + struct json_object *rp, *rq, *content, *jheadline, *j; + int i, n; + struct headline **headlines, *h; + + 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); + + json_object_put(rq); + + content = json_object_object_get(rp, "content"); + + if (content) { + n = json_object_array_length(content); + headlines = malloc((n+1)*sizeof(struct headline *)); + for (i = 0; i < n; i++) { + jheadline = json_object_array_get_idx(content, i); + + h = malloc(sizeof(struct headline)); + + 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, "content"); + h->content = strdup(json_object_get_string(j)); + + headlines[i] = h; + } + headlines[n] = NULL; + } else { + headlines = NULL; + } + + json_object_put(rp); + + return headlines; +} diff --git a/src/ttrss.h b/src/ttrss.h new file mode 100644 index 0000000..20243d2 --- /dev/null +++ b/src/ttrss.h @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2010-2013 jeanfi@gmail.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ + +#ifndef _TTRSS_H_ +#define _TTRSS_H_ + +struct headline { + char *title; + char *excerpt; + char *content; +}; + +struct feed { + char *title; + char *url; + int id; + + struct headline **headlines; +}; + +void ttrss_login(const char *url, const char *user, const char *password); +struct feed **ttrss_get_feeds(); +struct headline **ttrss_get_headlines(int); + +#endif -- 2.7.4