Added XML parsing for search results

This commit is contained in:
Markus Koch 2016-09-08 14:00:04 +02:00
parent 0951764836
commit 41574f12b6
2 changed files with 71 additions and 5 deletions

View File

@ -1,9 +1,11 @@
# Build library
find_package(PkgConfig REQUIRED)
pkg_check_modules(PC_LIBSOUP REQUIRED libsoup-2.4)
pkg_check_modules(LIBXML2 REQUIRED libxml-2.0)
include_directories(${PC_LIBSOUP_INCLUDE_DIRS})
link_directories(${PC_LIBSOUP_LIBRARY_DIRS})
include_directories(${PC_LIBSOUP_INCLUDE_DIRS} ${LIBXML2_INCLUDE_DIRS})
link_directories(${PC_LIBSOUP_LIBRARY_DIRS} ${LIBXML2_LIBRARY_DIRS})
add_definitions(${LIBXML2_CFLAGS_OTHER})
aux_source_directory(. LIBSRC_LIST)
add_library(konaclient SHARED ${LIBSRC_LIST})

View File

@ -1,6 +1,8 @@
#include "konasearch.h"
#include <stdio.h>
#include <libsoup/soup.h>
#include <libxml2/libxml/parser.h>
#include "konaimage.h"
// Function prototypes for local functions
static void kona_search_posts_soup_cb (SoupSession *session, SoupMessage *msg, gpointer user_data);
@ -8,11 +10,18 @@ static void kona_search_posts_soup_cb (SoupSession *session, SoupMessage *msg, g
/*** GLIB object start ***/
struct searchResult_t {
int count;
GList *images;
};
struct _KonaSearch {
GObject parent_instance; // struct of parent (inherit)
gchar *searchString;
SoupSession *postsSession;
struct searchResult_t searchResult;
};
G_DEFINE_TYPE(KonaSearch, kona_search, G_TYPE_OBJECT)
@ -50,6 +59,8 @@ KonaSearch *kona_search_new(gchar *search)
KonaSearch *ks;
ks = g_object_new(KONA_TYPE_SEARCH, 0);
ks->searchString = g_strdup(search);
ks->searchResult.count = -1;
ks->searchResult.images = NULL;
return ks;
}
@ -63,7 +74,7 @@ void kona_search_start(KonaSearch *self)
self->postsSession = soup_session_new();
url = g_strdup_printf("http://konachan.com/post.xml?tag=%s", self->searchString);
url = g_strdup_printf("http://konachan.com/post.xml?tags=%s", self->searchString);
msg = soup_message_new("GET", url);
g_free(url);
@ -72,7 +83,60 @@ void kona_search_start(KonaSearch *self)
static void kona_search_posts_soup_cb (SoupSession *session, SoupMessage *msg, gpointer user_data)
{
printf("RECEIVED!!\n");
printf("%s\n", *msg->response_body);
KonaSearch *ks;
KonaImage *ki;
kona_image_meta meta;
gchar **tagsplit, **tagsplitc;
GValue val = G_VALUE_INIT;
xmlDocPtr xDocPtr;
xmlNodePtr xNodePtr;
xmlChar *key;
ks = (KonaSearch*) user_data;
g_value_init(&val, G_TYPE_POINTER);
if (xDocPtr = xmlParseMemory(msg->response_body->data, msg->response_body->length))
{
xNodePtr = xmlDocGetRootElement(xDocPtr);
if (!xmlStrcmp(xNodePtr->name, "posts")) {
ks->searchResult.count = atoi(xmlGetProp(xNodePtr, "count"));
xNodePtr = xNodePtr->children;
do {
if (!xmlStrcmp(xNodePtr->name, "post")) {
// Assemble meta information structure
meta.id = atoi(xmlGetProp(xNodePtr, "id"));
meta.width = atoi(xmlGetProp(xNodePtr, "width"));
meta.height = atoi(xmlGetProp(xNodePtr, "height"));
meta.image_url = xmlGetProp(xNodePtr, "file_url");
meta.preview_url = xmlGetProp(xNodePtr, "preview_url");
meta.tags = NULL;
tagsplit = g_strsplit(xmlGetProp(xNodePtr, "tags"), " ", 100);
for (tagsplitc = tagsplit; *tagsplitc; tagsplitc++) {
meta.tags = g_list_append(meta.tags, g_strdup(*tagsplitc));
}
g_strfreev(tagsplit);
// Create new KonaImage
ki = kona_image_new();
g_value_set_pointer(&val, (gpointer) &meta);
g_object_set_property((GObject*) ki, "meta", &val);
// Add to list
ks->searchResult.images = g_list_append(ks->searchResult.images, ki);
}
} while (xNodePtr = xNodePtr->next);
}
else {
fprintf(stderr, "Error: Invalid XML root node!\n");
}
}
else
{
fprintf(stderr, "Error parsing XML result!\n");
}
fflush(stdout);
}