Added an example to test the WATC module. It lets you query whatsafterthecredits...
authorspenap <bulfaiter@gmail.com>
Wed, 18 Nov 2009 19:50:34 +0000 (19:50 +0000)
committerspenap <bulfaiter@gmail.com>
Wed, 18 Nov 2009 19:50:34 +0000 (19:50 +0000)
GSList of pairs (movie, boolean).
The provider files are modified, too, to allow for disposing the result list and the struct, and for printing the results.
The extra_scenes_provider (acting like a facade), is now ready to understand watc results.

git-svn-id: file:///svnroot/maevies/trunk@15 a96798e0-47ce-444a-94a4-1d14e63744fc

examples/Makefile
examples/test_watc.c [new file with mode: 0644]
src/extra_scenes_provider.c
src/watc_provider.c
src/watc_provider.h

index 4aabbff..b8c3354 100644 (file)
@@ -1,19 +1,34 @@
 CC = gcc
 
-LIBS_TMDB = `pkg-config rest-0.6 --libs`
+REST_LIBS = `pkg-config rest-0.6 --libs`
 
-FLAGS_TMDB = `pkg-config rest-0.6 --cflags` -I../src/
+REST_FLAGS = `pkg-config rest-0.6 --cflags`
 
-LIBS_GTRANSLATE = `pkg-config rest-0.6 --libs`
+SRC_FLAGS = -I../src/
 
-FLAGS_GTRANSLATE = `pkg-config rest-0.6 --cflags`
+LIBS_TMDB = $(REST_LIBS)
+
+FLAGS_TMDB = $(REST_FLAGS) -I../src/
+
+LIBS_GTRANSLATE = $(REST_LIBS)
+
+FLAGS_GTRANSLATE = $(REST_FLAGS)
 
 LIBS_GMOVIES = `pkg-config libxml-2.0 rest-0.6 --libs`
 
 FLAGS_GMOVIES = `pkg-config libxml-2.0 rest-0.6 --cflags`
 
-all: gtranslate gmovies tmdb
+all: gtranslate gmovies tmdb watc
        
+watc: watc_provider.o test_watc.o
+       $(CC) -ggdb -o watc watc_provider.o test_watc.o $(REST_LIBS)    
+       
+test_watc.o: test_watc.c
+       $(CC) -ggdb -c test_watc.c $(REST_FLAGS) $(SRC_FLAGS)
+       
+watc_provider.o: ../src/watc_provider.c
+       $(CC) -ggdb -c ../src/watc_provider.c $(REST_FLAGS) $(SRC_FLAGS)
+
 gtranslate: gtranslate.o
        $(CC) -o gtranslate gtranslate.o $(LIBS_GTRANSLATE)
 
@@ -36,4 +51,4 @@ test_tmdb.o: test_tmdb.c
        $(CC) -ggdb -c test_tmdb.c $(FLAGS_TMDB)
 
 clean:
-       rm -rf gtranslate gtranslate.o gmovies gmovies.o tmdb tmdb_movie.o test_tmdb.o *~
+       rm -rf gtranslate gtranslate.o gmovies gmovies.o tmdb tmdb_movie.o test_tmdb.o watc test_watc.o watc_provider.o *~
diff --git a/examples/test_watc.c b/examples/test_watc.c
new file mode 100644 (file)
index 0000000..5da7629
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * test_watc.c
+ *
+ * This file is part of maevies
+ * Copyright (C) 2009 Simon Pena <spenap@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 3 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.
+ *
+ */
+
+#include <glib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include "watc_provider.h"
+
+static void show_watc_info(gpointer info, gpointer data);
+
+gint main(gint argc, gchar **argv) {
+
+       GSList *result_list = NULL;
+       const gchar *movie_name = NULL;
+
+       if (argc != 2) {
+               movie_name = "Zombieland";
+       } else {
+               movie_name = argv[1];
+       }
+
+       g_thread_init(NULL);
+       g_type_init();
+
+       result_list = watc_has_stingers(movie_name);
+
+       g_slist_foreach(result_list, (GFunc) show_watc_info, NULL);
+
+       watcinfo_list_unref(result_list);
+
+       return 1;
+}
+
+static void show_watc_info(gpointer info, gpointer data) {
+
+       WATCInfo *movie_info = (info);
+
+       watcinfo_print(movie_info);
+
+}
index c4f2b43..61c73d8 100644 (file)
@@ -26,6 +26,18 @@ gboolean has_stingers(const gchar *movie) {
         * dlopen - http://stackoverflow.com/questions/384121/creating-a-module-system-dynamic-loading-in-c
         * Until we get it, we'll use just one of the provided providers ;)
         */
-       return watc_has_stingers(movie);
+       GSList *result_list = watc_has_stingers(movie);
+       WATCInfo *result = NULL;
+       gboolean value = FALSE;
 
+       if (g_slist_length(result_list) == 1) {
+
+               result = (WATCInfo *) g_slist_nth(result_list, 0);
+               if (result != NULL)
+                       value = result->has_stingers;
+       }
+
+       watcinfo_list_unref(result_list);
+
+       return value;
 }
index 8999d5c..a84a16c 100644 (file)
 #include "watc_provider.h"
 #include "string.h"
 
-static gboolean parse_response(const gchar *response);
+static GSList *parse_response(const gchar *response);
 
-gboolean watc_has_stingers(const gchar *name) {
+GSList *watc_has_stingers(const gchar *name) {
 
        RestProxy *proxy;
        RestProxyCall *call;
+       GSList *result_list;
        const gchar *response;
-       gssize len;
-
-       /* Initialization: most probably done in the invoker */
-       /* g_thread_init(NULL);
-       g_type_init(); */
 
        /* Provider initialization, should be refactored. Maybe it can be reused between calls */
        proxy = rest_proxy_new(WATC_SERVICE_URL, FALSE);
@@ -48,24 +44,70 @@ gboolean watc_has_stingers(const gchar *name) {
 
        /* Retrieving the results: should be done in/should receive a callback function */
        response = rest_proxy_call_get_payload(call);
-       len = rest_proxy_call_get_payload_length(call);
 
-       /* Process the output: must be checked whether the response has a * in the expected result
-        * or not */
-       write(1, response, len);
-       printf("\n");
+       result_list = parse_response(response);
 
-       /* Object disposal, should be refactored. */
        g_object_unref(call);
        g_object_unref(proxy);
 
-       return parse_response(response);
+       return result_list;
 }
 
 /* Ad-hoc implementation. Will give a wrong result if the query had more than one result,
  * if the title has a * in its text, or if the conventions used in what's after the credits vary.
+ *
+ * ["2012",["2012 (2009)","2012 (2009)?"]]
  */
-gboolean parse_response(const gchar *response) {
+static GSList *parse_response(const gchar *response) {
+
+       GSList *result_list = NULL;
+       gint i;
+       gchar **tokens = NULL;
+       WATCInfo *result = NULL;
+
+       /* We split the response into tokens.
+        *  - First: before [
+        *  - Second: before "
+        *  - Third: search term
+        *  - Fourth: before,
+        *  - Fifth: before [
+        *  - Sixth: before "
+        *  - Seventh: first actual result
+        *  */
+       tokens = g_strsplit_set(response, "[],\"", -1);
+
+       for (i = 4; i < g_strv_length(tokens); i++) {
+               if (strlen(tokens[i])) {
+                       result = g_new0(WATCInfo,1);
+                       result->movie_name = g_strdup(tokens[i]);
+                       result->has_stingers = g_str_has_suffix(tokens[i], "*")
+                                       || g_str_has_suffix(tokens[i], "?");
+                       result_list = g_slist_append(result_list, result);
+               }
+       }
+
+       g_strfreev(tokens);
+
+       return result_list;
+}
+
+void watcinfo_print(WATCInfo *info) {
+
+       g_print("Movie: %s", info->movie_name);
+       g_print(" %s\n", (info->has_stingers) ? "has extra scenes"
+                       : "doesn't have extra scenes");
+
+}
+
+void watcinfo_unref(WATCInfo *info) {
+
+       g_free(info->movie_name);
+       g_free(info);
+}
+
+void watcinfo_list_unref(GSList *list) {
+
+       g_slist_foreach(list, (GFunc) watcinfo_unref, NULL);
+       g_slist_free(list);
 
-       return (strpbrk(response, "*") != NULL);
 }
index 769da66..93fbcc9 100644 (file)
 #include <unistd.h>
 
 #define WATC_SERVICE_URL "http://whatsafterthecredits.com/api.php"
+typedef struct _WATCInfo WATCInfo;
 
-gboolean watc_has_stingers(const gchar *name);
+struct _WATCInfo {
+       gchar *movie_name;
+       gboolean has_stingers;
+};
+
+GSList *watc_has_stingers(const gchar *name);
+
+void watcinfo_unref(WATCInfo *info);
+
+void watcinfo_list_unref(GSList *list);
+
+void watcinfo_print(WATCInfo *info);
 
 #endif /* WATC_PROVIDER_H_ */