1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3
* Copyright (C) 2010 Jonathan Matthew <jonathan@d14n.org>
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.
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.
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.
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.
31
#include "rb-podcast-search.h"
34
#include <libsoup/soup.h>
35
#include <libsoup/soup-gnome.h>
36
#include <json-glib/json-glib.h>
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))
45
typedef struct _RBPodcastSearchITunes RBPodcastSearchITunes;
46
typedef struct _RBPodcastSearchITunesClass RBPodcastSearchITunesClass;
48
struct _RBPodcastSearchITunes
50
RBPodcastSearch parent;
55
struct _RBPodcastSearchITunesClass
57
RBPodcastSearchClass parent;
60
static void rb_podcast_search_itunes_class_init (RBPodcastSearchITunesClass *klass);
61
static void rb_podcast_search_itunes_init (RBPodcastSearchITunes *search);
63
G_DEFINE_TYPE (RBPodcastSearchITunes, rb_podcast_search_itunes, RB_TYPE_PODCAST_SEARCH);
65
#define ITUNES_SEARCH_URI "http://itunes.apple.com/WebObjects/MZStoreServices.woa/ws/wsSearch"
68
process_results (RBPodcastSearchITunes *search, JsonParser *parser)
70
JsonObject *container;
74
container = json_node_get_object (json_parser_get_root (parser));
75
results = json_node_get_array (json_object_get_member (container, "results"));
77
for (i = 0; i < json_array_get_length (results); i++) {
79
RBPodcastChannel *channel;
81
feed = json_array_get_object_element (results, i);
83
/* check wrapperType==track, kind==podcast ? */
85
channel = g_new0 (RBPodcastChannel, 1);
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;
93
channel->num_posts = json_object_get_int_member (feed, "trackCount");
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);
102
search_response_cb (SoupSession *session, SoupMessage *msg, RBPodcastSearchITunes *search)
105
GError *error = NULL;
108
g_object_get (msg, SOUP_MESSAGE_STATUS_CODE, &code, NULL);
112
g_object_get (msg, SOUP_MESSAGE_REASON_PHRASE, &reason, NULL);
113
rb_debug ("search request failed: %s", reason);
115
rb_podcast_search_finished (RB_PODCAST_SEARCH (search), FALSE);
119
if (msg->response_body->data == NULL) {
120
rb_debug ("no response data");
121
rb_podcast_search_finished (RB_PODCAST_SEARCH (search), TRUE);
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);
129
rb_debug ("unable to parse response data: %s", error->message);
130
g_clear_error (&error);
133
g_object_unref (parser);
134
rb_podcast_search_finished (RB_PODCAST_SEARCH (search), TRUE);
138
impl_start (RBPodcastSearch *bsearch, const char *text, int max_results)
141
SoupMessage *message;
143
RBPodcastSearchITunes *search = RB_PODCAST_SEARCH_ITUNES (bsearch);
145
search->session = soup_session_async_new_with_options (SOUP_SESSION_ADD_FEATURE_BY_TYPE,
146
SOUP_TYPE_GNOME_FEATURES_2_26,
149
uri = soup_uri_new (ITUNES_SEARCH_URI);
150
limit = g_strdup_printf ("%d", max_results);
151
soup_uri_set_query_from_fields (uri,
161
message = soup_message_new_from_uri (SOUP_METHOD_GET, uri);
164
soup_session_queue_message (search->session, message, (SoupSessionCallback) search_response_cb, search);
168
impl_cancel (RBPodcastSearch *bsearch)
170
RBPodcastSearchITunes *search = RB_PODCAST_SEARCH_ITUNES (bsearch);
172
if (search->session != NULL) {
173
soup_session_abort (search->session);
178
impl_dispose (GObject *object)
180
RBPodcastSearchITunes *search = RB_PODCAST_SEARCH_ITUNES (object);
182
if (search->session != NULL) {
183
soup_session_abort (search->session);
184
g_object_unref (search->session);
185
search->session = NULL;
188
G_OBJECT_CLASS (rb_podcast_search_itunes_parent_class)->dispose (object);
192
rb_podcast_search_itunes_init (RBPodcastSearchITunes *search)
198
rb_podcast_search_itunes_class_init (RBPodcastSearchITunesClass *klass)
200
GObjectClass *object_class = G_OBJECT_CLASS (klass);
201
RBPodcastSearchClass *search_class = RB_PODCAST_SEARCH_CLASS (klass);
203
object_class->dispose = impl_dispose;
205
search_class->cancel = impl_cancel;
206
search_class->start = impl_start;