#include "watc_provider.h"
#include "string.h"
-gboolean watc_has_stingers(const gchar *name) {
+static GSList *parse_response(const gchar *response);
+
+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);
/* 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);
}