From fa7b78690a1b515d768e03244451c76da757d656 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sim=C3=B3n=20Pena?= Date: Mon, 17 May 2010 18:23:37 +0200 Subject: [PATCH] Project: Removed librest dependencies The project dependencies on librest have been removed: * Modified gtranslate example to use libsoup * Modified gmovies example to use libsoup * Updated configure.ac --- configure.ac | 2 +- examples/gmovies.c | 149 ++++++++++++++++++++++++++----------------------- examples/gtranslate.c | 110 +++++++++++++++++++++++------------- 3 files changed, 151 insertions(+), 110 deletions(-) diff --git a/configure.ac b/configure.ac index 03317c8..aab0c4c 100644 --- a/configure.ac +++ b/configure.ac @@ -16,7 +16,7 @@ AC_PROG_LIBTOOL AC_HEADER_STDC PKG_CHECK_MODULES([DEPS], [glib-2.0 gobject-2.0 libsoup-2.4 \ - libxml-2.0 json-glib-1.0 rest-0.6 >= 0.6]) + libxml-2.0 json-glib-1.0]) AC_CONFIG_FILES([ Makefile diff --git a/examples/gmovies.c b/examples/gmovies.c index 1f93daa..14f7998 100644 --- a/examples/gmovies.c +++ b/examples/gmovies.c @@ -1,14 +1,15 @@ /* Queries Google movies for the theaters of a city and parses - * the response using libxml2. - */ + * the response using libxml2. + */ #include #include -#include +#include #include #include #include +#define BASE_URL "http://www.google.com/movies?near=%s" typedef struct struct_theater { @@ -31,20 +32,20 @@ typedef struct struct_movie gchar* info; GList* showTimes; } Movie; - + /** Search for a sibling node by the name of the sibling node */ xmlNodePtr getSiblingByName(xmlNodePtr node, xmlChar* name, int nameLen) { - + xmlNodePtr sibling = node->next; while((sibling != NULL) && (strncmp(sibling->name, name, nameLen) != 0)) { sibling = sibling->next; } - + return sibling; - -} + +} /** Search a child node by its node name */ xmlNodePtr getChildByName(xmlNodePtr node, xmlChar* name, int nameLen) @@ -52,14 +53,14 @@ xmlNodePtr getChildByName(xmlNodePtr node, xmlChar* name, int nameLen) return getSiblingByName(node->children, name, nameLen); } -/** Search the first sibling node that has an attribute 'attr' +/** Search the first sibling node that has an attribute 'attr' * with the value 'attrValue' */ xmlNodePtr getFirstSiblingByAttributeValue( xmlNodePtr sibling, xmlChar* attr, xmlChar * attrValue, int attrValueLen) { xmlNodePtr tempNode = sibling; xmlNodePtr result = NULL; - + while ((tempNode != NULL) && (result == NULL)) { xmlChar* value = xmlGetProp(tempNode, attr); if (value != NULL) { @@ -70,7 +71,7 @@ xmlNodePtr getFirstSiblingByAttributeValue( } tempNode = tempNode->next; } - + return result; } @@ -86,12 +87,12 @@ xmlNodePtr getFirstChildByAttributeValue( xmlNodePtr jumpXSiblings(xmlNodePtr node, int siblings) { xmlNodePtr r = node; - + int i = 0; for(; inext; } - + return r; } @@ -104,7 +105,7 @@ int childrenCount(xmlNodePtr node) i++; nav = nav->next; } - + return i; } @@ -115,33 +116,33 @@ xmlNodePtr getMovieResultsDiv(xmlNodePtr root) { // xmlNodePtr body = getSiblingByName(root->children, "body", 4); - + //
xmlNodePtr tempNode = getFirstChildByAttributeValue(body, "id", "results", 8); - + if (tempNode == NULL) { //no results return NULL; } - + //
tempNode = getFirstChildByAttributeValue(tempNode, "id", "movie_results", 14); - + if (tempNode == NULL) { //no results return NULL; } - + //
tempNode = getFirstChildByAttributeValue(tempNode, "class", "movie_results", 14); - + if (tempNode == NULL) { //no results return NULL; } - + return tempNode; - + } @@ -153,21 +154,21 @@ GList* getTheaterList(xmlNodePtr movieResults) //
... xmlNodePtr nav = movieResults->children; xmlNodePtr tmp1, tmp2, tmp3 = NULL; - + GList* resultList = NULL; - + while(nav != NULL) { tmp1 = getFirstSiblingByAttributeValue(nav, "class", "theater", 7); - + /* *
*
*
*
*
- *
+ *
*/ - + if (tmp1 != NULL) { //its theater data Theater* t = malloc(sizeof(Theater)); tmp2 = getFirstChildByAttributeValue(tmp1, "class", "desc", 4); @@ -179,45 +180,45 @@ GList* getTheaterList(xmlNodePtr movieResults) tmp3 = getFirstChildByAttributeValue(tmp2, "class", "info", 4); if (tmp3 != NULL) { //
t->address = xmlNodeGetContent(tmp3); - } + } } t->data = tmp1; resultList = g_list_append(resultList, t); } nav = nav->next; } - + return resultList; - + } GList* getShowtimes(gchar* times) { GList* resultList = NULL; - + gchar** timesArray = g_strsplit(times, " ", -1); - + int i = 0; for(i=0; timesArray[i] != NULL; i++) { ShowTime* st = malloc(sizeof(ShowTime)); st->time = g_strndup(timesArray[i], 5); resultList = g_list_append(resultList, st); } - + g_strfreev(timesArray); - + return resultList; } GList* getMovieList(xmlNodePtr movieSideDiv) { - + xmlNodePtr nav = movieSideDiv->children; xmlNodePtr tmp1, tmp2, tmp3 = NULL; - + GList* resultList = NULL; - + while(nav != NULL) { tmp1 = getFirstSiblingByAttributeValue(nav, "class", "movie", 5); if (tmp1 != NULL) { //is a movie @@ -239,16 +240,16 @@ GList* getMovieList(xmlNodePtr movieSideDiv) } resultList = g_list_append(resultList, m); } - + nav = nav->next; } - + return resultList; } GList* getTheaterMovies(Theater* t) { - + /* *
*
@@ -264,25 +265,25 @@ GList* getTheaterMovies(Theater* t) *
*
*/ - + GList* left = NULL; GList* right = NULL; xmlNodePtr showtimesDiv = getFirstChildByAttributeValue(t->data, "class", "showtimes", 9); if (showtimesDiv != NULL) { - - xmlNodePtr showLeft = getFirstChildByAttributeValue(showtimesDiv, "class", "show_left", 9); + + xmlNodePtr showLeft = getFirstChildByAttributeValue(showtimesDiv, "class", "show_left", 9); if (showLeft != NULL) left = getMovieList(showLeft); - - xmlNodePtr showRight = getFirstChildByAttributeValue(showtimesDiv, "class", "show_right", 10); + + xmlNodePtr showRight = getFirstChildByAttributeValue(showtimesDiv, "class", "show_right", 10); if (showRight != NULL) - right = getMovieList(showRight); - + right = getMovieList(showRight); + return g_list_concat(left, right); - + } - - + + } @@ -355,6 +356,15 @@ void showTheater(Theater * t, gpointer nothing) printf("\n"); } +static char* +get_query_uri (const char *city) +{ + char *query_uri = g_strdup_printf (BASE_URL, + city); + g_message ("%s\n", query_uri); + + return query_uri; +} int main (int argc, char ** argv) { @@ -364,8 +374,9 @@ int main (int argc, char ** argv) exit(-1); } - RestProxy *proxy; - RestProxyCall *call; + SoupSession *session; + SoupMessage *message; + char *query_uri; const gchar *payload; const char *city = argv[1]; gssize len; @@ -373,42 +384,38 @@ int main (int argc, char ** argv) g_thread_init(NULL); g_type_init(); - proxy = rest_proxy_new( - "http://www.google.com/movies", - FALSE); - call = rest_proxy_new_call(proxy); + session = soup_session_async_new (); + query_uri = get_query_uri (city); + message = soup_message_new ("GET", query_uri); - rest_proxy_call_add_params(call, - "near", city, - NULL); - rest_proxy_call_run(call, NULL, NULL); + soup_session_send_message (session, message); - payload = rest_proxy_call_get_payload(call); - len = rest_proxy_call_get_payload_length(call); + payload = message->response_body->data; + len = message->response_body->length; htmlDocPtr doc = htmlReadMemory(payload, len, "http://movies.google.com", "UTF-8", HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING); xmlNodePtr root = xmlDocGetRootElement(doc);//html - + //get the body node - xmlNodePtr movieResults = getMovieResultsDiv(root); + xmlNodePtr movieResults = getMovieResultsDiv(root); if (movieResults == NULL) { printf("NO RESULTS.\n"); exit(-1); } - + GList* theaterList = getTheaterList(movieResults); - + g_list_foreach(theaterList, (GFunc) showTheater, NULL); - + deleteTheaterList(theaterList); - + xmlFreeDoc(doc); - g_object_unref(call); - g_object_unref(proxy); - + g_free(query_uri); + g_object_unref(session); + exit(0); } diff --git a/examples/gtranslate.c b/examples/gtranslate.c index b6c3140..0f4c07a 100644 --- a/examples/gtranslate.c +++ b/examples/gtranslate.c @@ -1,41 +1,75 @@ #include -#include +#include #include -#include - -int main(int argc, char **argv) { - - RestProxy *proxy; - RestProxyCall *call; - const gchar *payload; - const char *text; - gssize len; - - g_thread_init(NULL); - g_type_init(); - - if (argc > 1) { - text = argv[1]; - } else { - text = "Hola Mundo"; - } - - proxy = rest_proxy_new( - "http://ajax.googleapis.com/ajax/services/language/translate", - FALSE); - call = rest_proxy_new_call(proxy); -perror(""); - rest_proxy_call_add_params(call, - "v", "1.0", - "q", text, - "langpair","es|en", - NULL); - rest_proxy_call_run(call, NULL, NULL); - - payload = rest_proxy_call_get_payload(call); - len = rest_proxy_call_get_payload_length(call); - write(1, payload, len); - printf("\n"); - g_object_unref(call); - g_object_unref(proxy); + +#define URL_BASE "http://ajax.googleapis.com/ajax/services/\ +language/translate?v=%s&q=%s&langpair=%s" +#define VERSION "1.0" +#define LANGPAIR "es|en" + +static gchar* +get_query_uri (const gchar *text) +{ + gchar *query = g_strdup_printf (URL_BASE, VERSION, text, LANGPAIR); + + g_message ("%s\n", query); + + return query; +} + +static void process_response (SoupSession *session, SoupMessage *message, + gpointer user_data) +{ + if (!SOUP_STATUS_IS_SUCCESSFUL (message->status_code) + || message->response_body->length <= 0) { + + g_print ("%s\n", message->reason_phrase); + } else { + + const gchar *mime = soup_message_headers_get_content_type ( + message->response_headers, NULL); + + g_message ("Mime type: %s\n", mime); + g_print ("%s\n", message->response_body->data); + } + + GMainLoop *loop = (GMainLoop *) user_data; + + g_main_loop_quit (loop); +} + +int main (int argc, char **argv) +{ + SoupSession *session = NULL; + SoupMessage *message = NULL; + GMainLoop *loop = NULL; + const gchar *text = NULL; + gchar *query = NULL; + + g_thread_init (NULL); + g_type_init (); + + loop = g_main_loop_new (NULL, FALSE); + + if (argc > 1) { + text = argv[1]; + } else { + text = "Hola Mundo"; + } + + query = get_query_uri (text); + + session = soup_session_async_new (); + message = soup_message_new ("GET", query); + + if (message) + soup_session_queue_message (session, message, + process_response, + loop); + + g_main_loop_run (loop); + + g_free (query); + g_main_loop_unref (loop); + g_object_unref (session); } -- 1.7.9.5