a84a16c4e6e0cfe387b09130e0216cbd8dfdb8e8
[maevies] / src / watc_provider.c
1 /*
2  * watc_provider.c
3  *
4  * This file is part of maevies
5  * Copyright (C) 2009 Simón Pena <bulfaiter@gmail.com>
6  *
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.
11  *
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.
16  *
17  */
18
19 #include "watc_provider.h"
20 #include "string.h"
21
22 static GSList *parse_response(const gchar *response);
23
24 GSList *watc_has_stingers(const gchar *name) {
25
26         RestProxy *proxy;
27         RestProxyCall *call;
28         GSList *result_list;
29         const gchar *response;
30
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);
34
35         /* Adding params to the call: check http://en.wikipedia.org/w/api.php
36          *
37          * There's only one variable param: the movie name
38          * */
39         rest_proxy_call_add_params(call, "action", "opensearch", "search", name,
40                         NULL);
41
42         /* The actual call */
43         rest_proxy_call_run(call, NULL, NULL);
44
45         /* Retrieving the results: should be done in/should receive a callback function */
46         response = rest_proxy_call_get_payload(call);
47
48         result_list = parse_response(response);
49
50         g_object_unref(call);
51         g_object_unref(proxy);
52
53         return result_list;
54 }
55
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.
58  *
59  * ["2012",["2012 (2009)","2012 (2009)?"]]
60  */
61 static GSList *parse_response(const gchar *response) {
62
63         GSList *result_list = NULL;
64         gint i;
65         gchar **tokens = NULL;
66         WATCInfo *result = NULL;
67
68         /* We split the response into tokens.
69          *  - First: before [
70          *  - Second: before "
71          *  - Third: search term
72          *  - Fourth: before,
73          *  - Fifth: before [
74          *  - Sixth: before "
75          *  - Seventh: first actual result
76          *  */
77         tokens = g_strsplit_set(response, "[],\"", -1);
78
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);
86                 }
87         }
88
89         g_strfreev(tokens);
90
91         return result_list;
92 }
93
94 void watcinfo_print(WATCInfo *info) {
95
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");
99
100 }
101
102 void watcinfo_unref(WATCInfo *info) {
103
104         g_free(info->movie_name);
105         g_free(info);
106 }
107
108 void watcinfo_list_unref(GSList *list) {
109
110         g_slist_foreach(list, (GFunc) watcinfo_unref, NULL);
111         g_slist_free(list);
112
113 }