Added a new provider type to support the retrieval of movie schedules
[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 gboolean watc_has_stingers(const gchar *name) {
23
24         RestProxy *proxy;
25         RestProxyCall *call;
26         const gchar *response;
27         gssize len;
28
29         /* Initialization: most probably done in the invoker */
30         /* g_thread_init(NULL);
31         g_type_init(); */
32
33         /* Provider initialization, should be refactored. Maybe it can be reused between calls */
34         proxy = rest_proxy_new(WATC_SERVICE_URL, FALSE);
35         call = rest_proxy_new_call(proxy);
36
37         /* Adding params to the call: check http://en.wikipedia.org/w/api.php
38          *
39          * There's only one variable param: the movie name
40          * */
41         rest_proxy_call_add_params(call, "action", "opensearch", "search", name,
42                         NULL);
43
44         /* The actual call */
45         rest_proxy_call_run(call, NULL, NULL);
46
47         /* Retrieving the results: should be done in/should receive a callback function */
48         response = rest_proxy_call_get_payload(call);
49         len = rest_proxy_call_get_payload_length(call);
50
51         /* Process the output: must be checked whether the response has a * in the expected result
52          * or not */
53         write(1, response, len);
54         printf("\n");
55
56         /* Object disposal, should be refactored. */
57         g_object_unref(call);
58         g_object_unref(proxy);
59
60         return parse_response(response);
61 }
62
63 /* Ad-hoc implementation. Will give a wrong result if the query had more than one result,
64  * if the title has a * in its text, or if the conventions used in what's after the credits vary.
65  */
66 gboolean parse_response(const gchar *response) {
67
68         return (strpbrk(response, "*") != NULL);
69 }