/* Queries Google movies for the theaters of a city and parses
- * the response using libxml2.
- */
+ * the response using libxml2.
+ */
#include <libxml/HTMLparser.h>
#include <libxml/tree.h>
-#include <rest/rest-proxy.h>
+#include <libsoup/soup.h>
#include <glib.h>
#include <stdio.h>
#include <unistd.h>
+#define BASE_URL "http://www.google.com/movies?near=%s"
typedef struct struct_theater
{
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)
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) {
}
tempNode = tempNode->next;
}
-
+
return result;
}
xmlNodePtr jumpXSiblings(xmlNodePtr node, int siblings)
{
xmlNodePtr r = node;
-
+
int i = 0;
for(; i<siblings; i++) {
r = r->next;
}
-
+
return r;
}
i++;
nav = nav->next;
}
-
+
return i;
}
{
//<body>
xmlNodePtr body = getSiblingByName(root->children, "body", 4);
-
+
//<div id="results">
xmlNodePtr tempNode = getFirstChildByAttributeValue(body, "id", "results", 8);
-
+
if (tempNode == NULL) {
//no results
return NULL;
}
-
+
//<div id="movie_results">
tempNode = getFirstChildByAttributeValue(tempNode, "id", "movie_results", 14);
-
+
if (tempNode == NULL) {
//no results
return NULL;
}
-
+
//<div class="movie_results">
tempNode = getFirstChildByAttributeValue(tempNode, "class", "movie_results", 14);
-
+
if (tempNode == NULL) {
//no results
return NULL;
}
-
+
return tempNode;
-
+
}
//<div class="movie_results"><div class="theater"/><div class="theater"/>...
xmlNodePtr nav = movieResults->children;
xmlNodePtr tmp1, tmp2, tmp3 = NULL;
-
+
GList* resultList = NULL;
-
+
while(nav != NULL) {
tmp1 = getFirstSiblingByAttributeValue(nav, "class", "theater", 7);
-
+
/*
* <div class="theater">
* <div class="desc">
* <div class="name"/><div class="info"/>
* </div>
* <div class="showtimes"/>
- * </div>
+ * </div>
*/
-
+
if (tmp1 != NULL) { //its theater data
Theater* t = malloc(sizeof(Theater));
tmp2 = getFirstChildByAttributeValue(tmp1, "class", "desc", 4);
tmp3 = getFirstChildByAttributeValue(tmp2, "class", "info", 4);
if (tmp3 != NULL) { //<div class="info">
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
}
resultList = g_list_append(resultList, m);
}
-
+
nav = nav->next;
}
-
+
return resultList;
}
GList* getTheaterMovies(Theater* t)
{
-
+
/*
* <div class="theater">
* <div class="desc"/>
* </div>
* </div>
*/
-
+
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);
-
+
}
-
-
+
+
}
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)
{
exit(-1);
}
- RestProxy *proxy;
- RestProxyCall *call;
+ SoupSession *session;
+ SoupMessage *message;
+ char *query_uri;
const gchar *payload;
const char *city = argv[1];
gssize len;
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);
}
#include <stdio.h>
-#include <rest/rest-proxy.h>
+#include <libsoup/soup.h>
#include <glib.h>
-#include <unistd.h>
-
-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);
}