]> git.sesse.net Git - vlc/blob - modules/misc/musicbrainz.c
2f14175bd63d43b91059315742769f1cf6def0d4
[vlc] / modules / misc / musicbrainz.c
1 /*****************************************************************************
2  * musicbrainz.c
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/intf.h>
31 #include <vlc_meta.h>
32
33 #include "musicbrainz/mb_c.h"
34
35
36 /*****************************************************************************
37  * intf_sys_t: description and status of log interface
38  *****************************************************************************/
39 struct intf_sys_t
40 {
41 };
42
43 /*****************************************************************************
44  * Local prototypes
45  *****************************************************************************/
46 static int  Open    ( vlc_object_t * );
47 static void Close   ( vlc_object_t * );
48
49 static int ItemChange( vlc_object_t *, const char *,
50                        vlc_value_t, vlc_value_t, void * );
51
52 /*****************************************************************************
53  * Module descriptor
54  *****************************************************************************/
55
56 vlc_module_begin();
57     set_category( CAT_INTERFACE );
58     set_subcategory( SUBCAT_INTERFACE_CONTROL );
59     set_shortname( N_( "MusicBrainz" ) );
60     set_description( _("MusicBrainz meta data") );
61
62     set_capability( "interface", 0 );
63     set_callbacks( Open, Close );
64 vlc_module_end();
65
66 /*****************************************************************************
67  * Open: initialize and create stuff
68  *****************************************************************************/
69 static int Open( vlc_object_t *p_this )
70 {
71     intf_thread_t *p_intf = (intf_thread_t *)p_this;
72     playlist_t *p_playlist;
73
74     MALLOC_ERR( p_intf->p_sys, intf_sys_t );
75
76     p_playlist = pl_Yield( p_intf );
77     var_AddCallback( p_playlist, "item-change", ItemChange, p_intf );
78     var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
79     pl_Release( p_intf );
80
81     return VLC_SUCCESS;
82 }
83
84 /*****************************************************************************
85  * Close: destroy interface stuff
86  *****************************************************************************/
87 static void Close( vlc_object_t *p_this )
88 {
89     intf_thread_t *p_intf = (intf_thread_t *)p_this;
90     playlist_t *p_playlist = pl_Yield( p_this );
91
92     var_DelCallback( p_playlist, "item-change", ItemChange, p_intf );
93     var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
94     pl_Release( p_this );
95
96     /* Destroy structure */
97     free( p_intf->p_sys );
98 }
99
100 /*****************************************************************************
101  * ItemChange: Playlist item change callback
102  *****************************************************************************/
103 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
104                        vlc_value_t oldval, vlc_value_t newval, void *param )
105 {
106     intf_thread_t *p_intf = (intf_thread_t *)param;
107     char *psz_title = NULL;
108     char *psz_artist = NULL;
109     char *psz_album = NULL;
110     input_thread_t *p_input;
111     playlist_t *p_playlist = pl_Yield( p_this );
112
113     p_input = p_playlist->p_input;
114     pl_Release( p_this );
115
116     if( !p_input ) return VLC_SUCCESS;
117     vlc_object_yield( p_input );
118
119     if( p_input->b_dead || !p_input->input.p_item->psz_name )
120     {
121         /* Not playing anything ... */
122         vlc_object_release( p_input );
123         return VLC_SUCCESS;
124     }
125
126     /* Playing something ... */
127     psz_artist = p_input->input.p_item->p_meta->psz_artist;
128     psz_album = p_input->input.p_item->p_meta->psz_album;
129     psz_title = p_input->input.p_item->psz_name;
130
131     if( psz_artist && psz_album /* && psz_title */ )
132     {
133         musicbrainz_t p_mb;
134         char psz_buf[256];
135         char psz_data[256];
136         char i_album_count, i;
137         char *ppsz_args[4];
138
139         fprintf( stdout,"\e[33;1m--> %s %s %s\e[0m\n", psz_artist, psz_album, psz_title );
140         p_mb = mb_New();
141 #ifdef WIN32
142         mb_WSAInit( p_mb );
143 #endif
144         mb_SetDepth( p_mb, 2 );
145         ppsz_args[0] = psz_album;
146         ppsz_args[1] = psz_artist;
147         ppsz_args[2] = NULL;
148         if( !mb_QueryWithArgs( p_mb,
149             "<mq:FindAlbum>\n" \
150             "   <mq:depth>@DEPTH@</mq:depth>\n" \
151             "   <mq:maxItems>@MAX_ITEMS@</mq:maxItems>\n" \
152             "   <mq:albumName>@1@</mq:albumName>\n" \
153             "   <mq:artistName>@2@</mq:artistName>\n" \
154             "</mq:FindAlbum>\n", ppsz_args ) )
155         {
156             mb_GetQueryError( p_mb, psz_buf, 256 );
157             msg_Err( p_intf, "Query failed: %s\n", psz_buf );
158             mb_Delete( p_mb );
159             vlc_object_release( p_input );
160             return VLC_EGENERIC;
161         }
162
163         i_album_count = mb_GetResultInt( p_mb, MBE_GetNumAlbums );
164         if( i_album_count < 1 )
165         {
166             msg_Err( p_intf, "No albums found.\n" );
167             mb_Delete( p_mb );
168             vlc_object_release( p_input );
169             return VLC_EGENERIC;
170         }
171
172         msg_Dbg( p_intf, "Found %d albums.\n", i_album_count );
173
174         for( i = 1; i <= i_album_count; i++ )
175         {
176             mb_Select( p_mb, MBS_Rewind );
177             mb_Select1( p_mb, MBS_SelectAlbum, i );
178
179             mb_GetResultData( p_mb, MBE_AlbumGetAlbumId, psz_data, 256 );
180             mb_GetIDFromURL( p_mb, psz_data, psz_buf, 256 );
181             msg_Dbg( p_intf, "Album Id: %s", psz_buf );
182
183             if( mb_GetResultData( p_mb, MBE_AlbumGetAmazonAsin, psz_buf, 256 ) )
184             {
185                 msg_Dbg( p_intf, "Amazon ASIN: %s", psz_buf );
186                 msg_Dbg( p_intf, "Album art url: " "http://images.amazon.com/images/P/%s.01._AA240_SCLZZZZZZZ_.jpg", psz_buf );
187                 break;
188             }
189         }
190 #ifdef WIN32
191         mb_WSAInit( p_mb );
192 #endif
193
194         mb_Delete( p_mb );
195
196     }
197
198     vlc_object_release( p_input );
199
200     return VLC_SUCCESS;
201 }