Added a new provider type to support the retrieval of movie schedules
[maevies] / src / tmdb_movie.c
1 /*
2  * tmdb_movie.c
3  *
4  * This file is part of maevies
5  * Copyright (C) 2009 Simón Pena <spenap@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 "tmdb_movie.h"
20
21 void query_tmdb(const gchar *name);
22
23 static void parse_child_element(gpointer key, gpointer element, gpointer data);
24
25 RestXmlNode *get_xml(RestProxyCall *call);
26
27 TMDBMovie *tmdb_get_info(const gchar *name) {
28
29         TMDBMovie *movie = g_new0(TMDBMovie, 1);
30
31         movie->rating = 10;
32         movie->released = g_date_new_dmy(1, G_DATE_FEBRUARY, 2009);
33
34         query_tmdb(name);
35
36         return movie;
37 }
38
39 void query_tmdb(const gchar *name) {
40
41         RestProxy *proxy;
42         RestProxyCall *call;
43         gchar *tmdb_function;
44         const gchar *response;
45         RestXmlNode *root;
46         gssize len;
47
48         /* Provider initialization, should be refactored. Maybe it can be reused between calls */
49         proxy = rest_proxy_new(TMDB_SERVICE_URL, FALSE);
50         call = rest_proxy_new_call(proxy);
51
52         /* Generate the function string */
53         tmdb_function = g_strdup_printf("%s/%s/%s/%s/%s", TMDB_SEARCH_METHOD,
54                         TMDB_LANGUAGE, TMDB_FORMAT, TMDB_API_KEY, name);
55
56         rest_proxy_call_set_function(call, tmdb_function);
57
58         /* The actual call */
59         rest_proxy_call_run(call, NULL, NULL);
60
61         get_xml(call);
62
63         /* Object disposal, should be refactored. */
64         g_object_unref(call);
65         g_object_unref(proxy);
66 }
67
68 RestXmlNode *get_xml(RestProxyCall *call) {
69         RestXmlParser *parser;
70         RestXmlNode *root;
71         GError *error = NULL;
72
73         parser = rest_xml_parser_new();
74
75         root = rest_xml_parser_parse_from_data(parser, rest_proxy_call_get_payload(
76                         call), rest_proxy_call_get_payload_length(call));
77
78         g_hash_table_foreach(root->children, (GHFunc) parse_child_element, NULL);
79
80         g_object_unref(parser);
81
82         return root;
83 }
84
85 static void parse_child_element(gpointer key, gpointer element, gpointer data) {
86
87         RestXmlNode *node = (RestXmlNode*) element;
88
89         g_print("%s: ", key);
90         if (g_hash_table_size(node->children) == 0) {
91                 g_print("%s", node->content);
92         }
93         g_print("\n");
94         g_hash_table_foreach(node->children, (GHFunc) parse_child_element, NULL);
95 }