Added XML parsing for search results
This commit is contained in:
parent
0951764836
commit
41574f12b6
|
@ -1,9 +1,11 @@
|
||||||
# Build library
|
# Build library
|
||||||
find_package(PkgConfig REQUIRED)
|
find_package(PkgConfig REQUIRED)
|
||||||
pkg_check_modules(PC_LIBSOUP REQUIRED libsoup-2.4)
|
pkg_check_modules(PC_LIBSOUP REQUIRED libsoup-2.4)
|
||||||
|
pkg_check_modules(LIBXML2 REQUIRED libxml-2.0)
|
||||||
|
|
||||||
include_directories(${PC_LIBSOUP_INCLUDE_DIRS})
|
include_directories(${PC_LIBSOUP_INCLUDE_DIRS} ${LIBXML2_INCLUDE_DIRS})
|
||||||
link_directories(${PC_LIBSOUP_LIBRARY_DIRS})
|
link_directories(${PC_LIBSOUP_LIBRARY_DIRS} ${LIBXML2_LIBRARY_DIRS})
|
||||||
|
add_definitions(${LIBXML2_CFLAGS_OTHER})
|
||||||
|
|
||||||
aux_source_directory(. LIBSRC_LIST)
|
aux_source_directory(. LIBSRC_LIST)
|
||||||
add_library(konaclient SHARED ${LIBSRC_LIST})
|
add_library(konaclient SHARED ${LIBSRC_LIST})
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#include "konasearch.h"
|
#include "konasearch.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <libsoup/soup.h>
|
#include <libsoup/soup.h>
|
||||||
|
#include <libxml2/libxml/parser.h>
|
||||||
|
#include "konaimage.h"
|
||||||
|
|
||||||
// Function prototypes for local functions
|
// Function prototypes for local functions
|
||||||
static void kona_search_posts_soup_cb (SoupSession *session, SoupMessage *msg, gpointer user_data);
|
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 ***/
|
/*** GLIB object start ***/
|
||||||
|
|
||||||
|
struct searchResult_t {
|
||||||
|
int count;
|
||||||
|
GList *images;
|
||||||
|
};
|
||||||
|
|
||||||
struct _KonaSearch {
|
struct _KonaSearch {
|
||||||
GObject parent_instance; // struct of parent (inherit)
|
GObject parent_instance; // struct of parent (inherit)
|
||||||
gchar *searchString;
|
gchar *searchString;
|
||||||
|
|
||||||
SoupSession *postsSession;
|
SoupSession *postsSession;
|
||||||
|
|
||||||
|
struct searchResult_t searchResult;
|
||||||
};
|
};
|
||||||
|
|
||||||
G_DEFINE_TYPE(KonaSearch, kona_search, G_TYPE_OBJECT)
|
G_DEFINE_TYPE(KonaSearch, kona_search, G_TYPE_OBJECT)
|
||||||
|
@ -50,6 +59,8 @@ KonaSearch *kona_search_new(gchar *search)
|
||||||
KonaSearch *ks;
|
KonaSearch *ks;
|
||||||
ks = g_object_new(KONA_TYPE_SEARCH, 0);
|
ks = g_object_new(KONA_TYPE_SEARCH, 0);
|
||||||
ks->searchString = g_strdup(search);
|
ks->searchString = g_strdup(search);
|
||||||
|
ks->searchResult.count = -1;
|
||||||
|
ks->searchResult.images = NULL;
|
||||||
return ks;
|
return ks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,7 +74,7 @@ void kona_search_start(KonaSearch *self)
|
||||||
|
|
||||||
self->postsSession = soup_session_new();
|
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);
|
msg = soup_message_new("GET", url);
|
||||||
g_free(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)
|
static void kona_search_posts_soup_cb (SoupSession *session, SoupMessage *msg, gpointer user_data)
|
||||||
{
|
{
|
||||||
printf("RECEIVED!!\n");
|
KonaSearch *ks;
|
||||||
printf("%s\n", *msg->response_body);
|
|
||||||
|
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);
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue