Regarding to TheMovieDB.org, I finished the parsing of 1 result (ignoring data if...
[butaca:butaca-server.git] / 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 static void do_get_info(TMDBMovie *movie);
22
23 static void parse_xml_response(RestProxyCall *call, TMDBMovie *movie);
24
25 TMDBMovie *tmdb_get_info(const gchar *name) {
26
27         TMDBMovie *movie = g_new0(TMDBMovie, 1);
28
29         movie->name = g_strdup(name);
30
31         do_get_info(movie);
32
33         /*
34          * movie->rating = 10;
35          * movie->released = g_date_new_dmy(1, G_DATE_FEBRUARY, 2009);
36          */
37
38         return movie;
39 }
40
41 static void do_get_info(TMDBMovie *movie) {
42
43         RestProxy *proxy;
44         RestProxyCall *call;
45         gchar *tmdb_function;
46
47         proxy = rest_proxy_new(TMDB_SERVICE_URL, FALSE);
48         call = rest_proxy_new_call(proxy);
49
50         /* Generate the function string.
51          * It's METHOD/LANGUAGE/FORMAT/APIKEY/MOVIENAME,
52          * so the actual query puts that after the URL */
53         tmdb_function = g_strdup_printf("%s/%s/%s/%s/%s", TMDB_SEARCH_METHOD,
54                         TMDB_LANGUAGE, TMDB_FORMAT, TMDB_API_KEY, movie->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         parse_xml_response(call, movie);
62
63         /* Object disposal, should be refactored. */
64         g_object_unref(call);
65         g_free(tmdb_function);
66         g_object_unref(proxy);
67 }
68
69 static void parse_xml_response(RestProxyCall *call, TMDBMovie *movie) {
70         RestXmlParser *parser;
71         RestXmlNode *root;
72         RestXmlNode *tmp;
73         RestXmlNode *field;
74         gint total_results;
75
76         parser = rest_xml_parser_new();
77
78         root = rest_xml_parser_parse_from_data(parser, rest_proxy_call_get_payload(
79                         call), rest_proxy_call_get_payload_length(call));
80
81         tmp = g_hash_table_lookup(root->children, g_intern_string(
82                         "opensearch:totalResults"));
83
84         total_results = atof(tmp->content);
85
86         tmp = g_hash_table_lookup(root->children, g_intern_string("movies"));
87
88         tmp = g_hash_table_lookup(tmp->children, g_intern_string("movie"));
89
90         while (tmp != NULL) {
91
92                 field = g_hash_table_lookup(tmp->children,
93                                 g_intern_string("popularity"));
94                 movie->popularity = atoi(field->content);
95
96                 field = g_hash_table_lookup(tmp->children, g_intern_string("name"));
97                 movie->name = g_strdup(field->content);
98
99                 field = g_hash_table_lookup(tmp->children, g_intern_string(
100                                 "alternative_name"));
101                 movie->alternative_name = g_strdup(field->content);
102
103                 field = g_hash_table_lookup(tmp->children, g_intern_string("id"));
104                 movie->id = atoi(field->content);
105
106                 field = g_hash_table_lookup(tmp->children, g_intern_string("imdb_id"));
107                 movie->imdb_id = g_strdup(field->content);
108
109                 field = g_hash_table_lookup(tmp->children, g_intern_string("url"));
110                 movie->url = g_strdup(field->content);
111
112                 field = g_hash_table_lookup(tmp->children, g_intern_string("rating"));
113                 movie->rating = atof(field->content);
114
115                 field = g_hash_table_lookup(tmp->children, g_intern_string("released"));
116                 movie->released = g_date_new();
117                 g_date_set_parse(movie->released, field->content);
118
119                 field = g_hash_table_lookup(tmp->children, g_intern_string("overview"));
120                 movie->overview = g_strdup(field->content);
121
122                 /* tmp = tmp->next; */
123                 tmp = NULL;
124         }
125
126         g_object_unref(parser);
127         rest_xml_node_unref(root);
128 }
129
130 void tmdb_movie_unref(TMDBMovie *movie) {
131
132         g_free(movie->alternative_name);
133         g_free(movie->imdb_id);
134         g_free(movie->name);
135         g_free(movie->overview);
136         g_free(movie->url);
137
138         if (movie->released != NULL)
139                 g_date_free(movie->released);
140
141         g_free(movie);
142 }
143
144 void tmdb_movie_print(const TMDBMovie *movie) {
145
146         g_print("themoviedb.org - The Movie Database.\n");
147         g_print("Movie name: %s\n", GET_MESSAGE(movie->name));
148         g_print("Alternate name: %s\n", GET_MESSAGE(movie->alternative_name));
149         g_print("Rating: %f\n", movie->rating);
150         g_print("Overview:\n %s\n", GET_MESSAGE(movie->overview));
151         g_print("ID: %d\n", movie->id);
152         g_print("IMDB ID: %s\n", GET_MESSAGE(movie->imdb_id));
153         g_print("Popularity: %d\n", movie->popularity);
154         g_print("URL: %s\n", GET_MESSAGE(movie->url));
155         g_print("Runtime: %d\n", movie->runtime);
156         if (g_date_valid(movie->released)) {
157                 g_print("Released: %d-%d-%d\n", g_date_get_year(movie->released),
158                                 g_date_get_month(movie->released), g_date_get_day(
159                                                 movie->released));
160         }
161 }