hythmbox-2.98/podcast/rb-podcast-search-itunes.c

No issues found

  1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  2  *
  3  *  Copyright (C) 2010 Jonathan Matthew <jonathan@d14n.org>
  4  *
  5  *  This program is free software; you can redistribute it and/or modify
  6  *  it under the terms of the GNU General Public License as published by
  7  *  the Free Software Foundation; either version 2 of the License, or
  8  *  (at your option) any later version.
  9  *
 10  *  The Rhythmbox authors hereby grant permission for non-GPL compatible
 11  *  GStreamer plugins to be used and distributed together with GStreamer
 12  *  and Rhythmbox. This permission is above and beyond the permissions granted
 13  *  by the GPL license by which Rhythmbox is covered. If you modify this code
 14  *  you may extend this exception to your version of the code, but you are not
 15  *  obligated to do so. If you do not wish to do so, delete this exception
 16  *  statement from your version.
 17  *
 18  *  This program is distributed in the hope that it will be useful,
 19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 21  *  GNU General Public License for more details.
 22  *
 23  *  You should have received a copy of the GNU General Public License
 24  *  along with this program; if not, write to the Free Software
 25  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
 26  *
 27  */
 28 
 29 #include "config.h"
 30 
 31 #include "rb-podcast-search.h"
 32 #include "rb-debug.h"
 33 
 34 #include <libsoup/soup.h>
 35 #include <libsoup/soup-gnome.h>
 36 #include <json-glib/json-glib.h>
 37 
 38 #define RB_TYPE_PODCAST_SEARCH_ITUNES         (rb_podcast_search_itunes_get_type ())
 39 #define RB_PODCAST_SEARCH_ITUNES(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), RB_TYPE_PODCAST_SEARCH_ITUNES, RBPodcastSearchITunes))
 40 #define RB_PODCAST_SEARCH_ITUNES_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST((k), RB_TYPE_PODCAST_SEARCH_ITUNES, RBPodcastSearchITunesClass))
 41 #define RB_IS_PODCAST_SEARCH_ITUNES(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), RB_TYPE_PODCAST_SEARCH_ITUNES))
 42 #define RB_IS_PODCAST_SEARCH_ITUNES_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), RB_TYPE_PODCAST_SEARCH_ITUNES))
 43 #define RB_PODCAST_SEARCH_ITUNES_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), RB_TYPE_PODCAST_SEARCH_ITUNES, RBPodcastSearchITunesClass))
 44 
 45 typedef struct _RBPodcastSearchITunes RBPodcastSearchITunes;
 46 typedef struct _RBPodcastSearchITunesClass RBPodcastSearchITunesClass;
 47 
 48 struct _RBPodcastSearchITunes
 49 {
 50 	RBPodcastSearch parent;
 51 
 52 	SoupSession *session;
 53 };
 54 
 55 struct _RBPodcastSearchITunesClass
 56 {
 57 	RBPodcastSearchClass parent;
 58 };
 59 
 60 static void rb_podcast_search_itunes_class_init (RBPodcastSearchITunesClass *klass);
 61 static void rb_podcast_search_itunes_init (RBPodcastSearchITunes *search);
 62 
 63 G_DEFINE_TYPE (RBPodcastSearchITunes, rb_podcast_search_itunes, RB_TYPE_PODCAST_SEARCH);
 64 
 65 #define ITUNES_SEARCH_URI	"http://itunes.apple.com/WebObjects/MZStoreServices.woa/ws/wsSearch"
 66 
 67 static void
 68 process_results (RBPodcastSearchITunes *search, JsonParser *parser)
 69 {
 70 	JsonObject *container;
 71 	JsonArray *results;
 72 	guint i;
 73 
 74 	container = json_node_get_object (json_parser_get_root (parser));
 75 	results = json_node_get_array (json_object_get_member (container, "results"));
 76 
 77 	for (i = 0; i < json_array_get_length (results); i++) {
 78 		JsonObject *feed;
 79 		RBPodcastChannel *channel;
 80 
 81 		feed = json_array_get_object_element (results, i);
 82 
 83 		/* check wrapperType==track, kind==podcast ? */
 84 
 85 		channel = g_new0 (RBPodcastChannel, 1);
 86 
 87 		channel->url = g_strdup (json_object_get_string_member (feed, "collectionViewUrl"));
 88 		channel->title = g_strdup (json_object_get_string_member (feed, "collectionName"));
 89 		channel->author = g_strdup (json_object_get_string_member (feed, "artistName"));
 90 		channel->img = g_strdup (json_object_get_string_member (feed, "artworkUrl100"));	/* 100? */
 91 		channel->is_opml = FALSE;
 92 
 93 		channel->num_posts = json_object_get_int_member (feed, "trackCount");
 94 
 95 		rb_debug ("got result %s (%s)", channel->title, channel->url);
 96 		rb_podcast_search_result (RB_PODCAST_SEARCH (search), channel);
 97 		rb_podcast_parse_channel_free (channel);
 98 	}
 99 }
100 
101 static void
102 search_response_cb (SoupSession *session, SoupMessage *msg, RBPodcastSearchITunes *search)
103 {
104 	JsonParser *parser;
105 	GError *error = NULL;
106 	int code;
107 
108 	g_object_get (msg, SOUP_MESSAGE_STATUS_CODE, &code, NULL);
109 	if (code != 200) {
110 		char *reason;
111 
112 		g_object_get (msg, SOUP_MESSAGE_REASON_PHRASE, &reason, NULL);
113 		rb_debug ("search request failed: %s", reason);
114 		g_free (reason);
115 		rb_podcast_search_finished (RB_PODCAST_SEARCH (search), FALSE);
116 		return;
117 	}
118 
119 	if (msg->response_body->data == NULL) {
120 		rb_debug ("no response data");
121 		rb_podcast_search_finished (RB_PODCAST_SEARCH (search), TRUE);
122 		return;
123 	}
124 
125 	parser = json_parser_new ();
126 	if (json_parser_load_from_data (parser, msg->response_body->data, msg->response_body->length, &error)) {
127 		process_results (search, parser);
128 	} else {
129 		rb_debug ("unable to parse response data: %s", error->message);
130 		g_clear_error (&error);
131 	}
132 
133 	g_object_unref (parser);
134 	rb_podcast_search_finished (RB_PODCAST_SEARCH (search), TRUE);
135 }
136 
137 static void
138 impl_start (RBPodcastSearch *bsearch, const char *text, int max_results)
139 {
140 	SoupURI *uri;
141 	SoupMessage *message;
142 	char *limit;
143 	RBPodcastSearchITunes *search = RB_PODCAST_SEARCH_ITUNES (bsearch);
144 
145 	search->session = soup_session_async_new_with_options (SOUP_SESSION_ADD_FEATURE_BY_TYPE,
146 							       SOUP_TYPE_GNOME_FEATURES_2_26,
147 							       NULL);
148 
149 	uri = soup_uri_new (ITUNES_SEARCH_URI);
150 	limit = g_strdup_printf ("%d", max_results);
151 	soup_uri_set_query_from_fields (uri,
152 					"term", text,
153 					"media", "podcast",
154 					"entity", "podcast",
155 					"limit", limit,
156 					"version", "2",
157 					"output", "json",
158 					NULL);
159 	g_free (limit);
160 
161 	message = soup_message_new_from_uri (SOUP_METHOD_GET, uri);
162 	soup_uri_free (uri);
163 
164 	soup_session_queue_message (search->session, message, (SoupSessionCallback) search_response_cb, search);
165 }
166 
167 static void
168 impl_cancel (RBPodcastSearch *bsearch)
169 {
170 	RBPodcastSearchITunes *search = RB_PODCAST_SEARCH_ITUNES (bsearch);
171 
172 	if (search->session != NULL) {
173 		soup_session_abort (search->session);
174 	}
175 }
176 
177 static void
178 impl_dispose (GObject *object)
179 {
180 	RBPodcastSearchITunes *search = RB_PODCAST_SEARCH_ITUNES (object);
181 
182 	if (search->session != NULL) {
183 		soup_session_abort (search->session);
184 		g_object_unref (search->session);
185 		search->session = NULL;
186 	}
187 
188 	G_OBJECT_CLASS (rb_podcast_search_itunes_parent_class)->dispose (object);
189 }
190 
191 static void
192 rb_podcast_search_itunes_init (RBPodcastSearchITunes *search)
193 {
194 	/* do nothing? */
195 }
196 
197 static void
198 rb_podcast_search_itunes_class_init (RBPodcastSearchITunesClass *klass)
199 {
200 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
201 	RBPodcastSearchClass *search_class = RB_PODCAST_SEARCH_CLASS (klass);
202 
203 	object_class->dispose = impl_dispose;
204 
205 	search_class->cancel = impl_cancel;
206 	search_class->start = impl_start;
207 }