4 * This file is part of maevies
5 * Copyright (C) 2009 Simón Pena <bulfaiter@gmail.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
19 #include "watc_provider.h"
22 static GSList *parse_response(const gchar *response);
24 GSList *watc_has_stingers(const gchar *name) {
29 const gchar *response;
31 /* Provider initialization, should be refactored. Maybe it can be reused between calls */
32 proxy = rest_proxy_new(WATC_SERVICE_URL, FALSE);
33 call = rest_proxy_new_call(proxy);
35 /* Adding params to the call: check http://en.wikipedia.org/w/api.php
37 * There's only one variable param: the movie name
39 rest_proxy_call_add_params(call, "action", "opensearch", "search", name,
43 rest_proxy_call_run(call, NULL, NULL);
45 /* Retrieving the results: should be done in/should receive a callback function */
46 response = rest_proxy_call_get_payload(call);
48 result_list = parse_response(response);
51 g_object_unref(proxy);
56 /* Ad-hoc implementation. Will give a wrong result if the query had more than one result,
57 * if the title has a * in its text, or if the conventions used in what's after the credits vary.
59 * ["2012",["2012 (2009)","2012 (2009)?"]]
61 static GSList *parse_response(const gchar *response) {
63 GSList *result_list = NULL;
65 gchar **tokens = NULL;
66 WATCInfo *result = NULL;
68 /* We split the response into tokens.
71 * - Third: search term
75 * - Seventh: first actual result
77 tokens = g_strsplit_set(response, "[],\"", -1);
79 for (i = 4; i < g_strv_length(tokens); i++) {
80 if (strlen(tokens[i])) {
81 result = g_new0(WATCInfo,1);
82 result->movie_name = g_strdup(tokens[i]);
83 result->has_stingers = g_str_has_suffix(tokens[i], "*")
84 || g_str_has_suffix(tokens[i], "?");
85 result_list = g_slist_append(result_list, result);
94 void watcinfo_print(WATCInfo *info) {
96 g_print("Movie: %s", info->movie_name);
97 g_print(" %s\n", (info->has_stingers) ? "has extra scenes"
98 : "doesn't have extra scenes");
102 void watcinfo_unref(WATCInfo *info) {
104 g_free(info->movie_name);
108 void watcinfo_list_unref(GSList *list) {
110 g_slist_foreach(list, (GFunc) watcinfo_unref, NULL);