4 * This file is part of maevies
5 * Copyright (C) 2009 Simón Pena <spenap@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 "tmdb_movie.h"
21 static void do_get_info(TMDBMovie *movie);
23 static void parse_xml_response(RestProxyCall *call, TMDBMovie *movie);
25 TMDBMovie *tmdb_get_info(const gchar *name) {
27 TMDBMovie *movie = g_new0(TMDBMovie, 1);
29 movie->name = g_strdup(name);
35 * movie->released = g_date_new_dmy(1, G_DATE_FEBRUARY, 2009);
41 static void do_get_info(TMDBMovie *movie) {
47 proxy = rest_proxy_new(TMDB_SERVICE_URL, FALSE);
48 call = rest_proxy_new_call(proxy);
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);
56 rest_proxy_call_set_function(call, tmdb_function);
59 rest_proxy_call_run(call, NULL, NULL);
61 parse_xml_response(call, movie);
63 /* Object disposal, should be refactored. */
65 g_free(tmdb_function);
66 g_object_unref(proxy);
69 static void parse_xml_response(RestProxyCall *call, TMDBMovie *movie) {
70 RestXmlParser *parser;
76 parser = rest_xml_parser_new();
78 root = rest_xml_parser_parse_from_data(parser, rest_proxy_call_get_payload(
79 call), rest_proxy_call_get_payload_length(call));
81 tmp = g_hash_table_lookup(root->children, g_intern_string(
82 "opensearch:totalResults"));
84 total_results = atof(tmp->content);
86 tmp = g_hash_table_lookup(root->children, g_intern_string("movies"));
88 tmp = g_hash_table_lookup(tmp->children, g_intern_string("movie"));
92 field = g_hash_table_lookup(tmp->children,
93 g_intern_string("popularity"));
94 movie->popularity = atoi(field->content);
96 field = g_hash_table_lookup(tmp->children, g_intern_string("name"));
97 movie->name = g_strdup(field->content);
99 field = g_hash_table_lookup(tmp->children, g_intern_string(
100 "alternative_name"));
101 movie->alternative_name = g_strdup(field->content);
103 field = g_hash_table_lookup(tmp->children, g_intern_string("id"));
104 movie->id = atoi(field->content);
106 field = g_hash_table_lookup(tmp->children, g_intern_string("imdb_id"));
107 movie->imdb_id = g_strdup(field->content);
109 field = g_hash_table_lookup(tmp->children, g_intern_string("url"));
110 movie->url = g_strdup(field->content);
112 field = g_hash_table_lookup(tmp->children, g_intern_string("rating"));
113 movie->rating = atof(field->content);
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);
119 field = g_hash_table_lookup(tmp->children, g_intern_string("overview"));
120 movie->overview = g_strdup(field->content);
122 /* tmp = tmp->next; */
126 g_object_unref(parser);
127 rest_xml_node_unref(root);
130 void tmdb_movie_unref(TMDBMovie *movie) {
132 g_free(movie->alternative_name);
133 g_free(movie->imdb_id);
135 g_free(movie->overview);
138 if (movie->released != NULL)
139 g_date_free(movie->released);
144 void tmdb_movie_print(const TMDBMovie *movie) {
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(