]> git.sesse.net Git - vlc/blob - modules/meta_engine/musicbrainz.c
Merge branch 'master' of git@git.videolan.org:vlc
[vlc] / modules / meta_engine / 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
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc/vlc.h>
33 #include <vlc_interface.h>
34 #include <vlc_input.h>
35 #include <vlc_playlist.h>
36 #include <vlc_meta.h>
37
38 #include "musicbrainz/mb_c.h"
39
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 static int FindArt( vlc_object_t * );
44 static int FindMetaMBId( vlc_object_t *p_this );
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49
50 vlc_module_begin();
51     set_shortname( N_( "MusicBrainz" ) );
52     set_description( _("MusicBrainz meta data") );
53
54     set_capability( "meta fetcher", 10 );
55         /* This meta fetcher module only retrieves the musicbrainz track id
56          * and stores it
57          * TODO:
58          *  - Actually do it
59          *  - Also store the album id
60          * */
61         set_callbacks( FindMetaMBId, NULL );
62     add_submodule();
63         /* This art finder module fetches the album ID from musicbrainz and
64          * uses it to fetch the amazon ASIN from musicbrainz.
65          * TODO:
66          *  - Add ability to reuse MB album ID if we already have it
67          */
68         set_capability( "art finder", 80 );
69         set_callbacks( FindArt, NULL );
70 vlc_module_end();
71
72 /*****************************************************************************
73  *****************************************************************************/
74
75 static int GetData( vlc_object_t *p_obj, input_item_t *p_item,
76                     bool b_art )
77 {
78     char psz_buf[256];
79     char psz_data[256];
80     char i_album_count, i;
81     char *ppsz_args[4];
82     bool b_art_found = false;
83
84     char *psz_artist;
85     char *psz_album;
86
87     psz_artist = input_item_GetArtist( p_item );
88     psz_album = input_item_GetAlbum( p_item );
89
90     if( !psz_artist || !psz_album )
91     {
92         free( psz_artist );
93         free( psz_album );
94         return VLC_EGENERIC;
95     }
96
97     musicbrainz_t p_mb;
98
99     p_mb = mb_New();
100 #ifdef WIN32
101     mb_WSAInit( p_mb );
102 #endif
103
104     mb_SetDepth( p_mb, 2 );
105     ppsz_args[0] = psz_album;
106     ppsz_args[1] = psz_artist;
107     ppsz_args[2] = NULL;
108     if( !mb_QueryWithArgs( p_mb,
109         "<mq:FindAlbum>\n" \
110         "   <mq:depth>@DEPTH@</mq:depth>\n" \
111         "   <mq:maxItems>@MAX_ITEMS@</mq:maxItems>\n" \
112         "   <mq:albumName>@1@</mq:albumName>\n" \
113         "   <mq:artistName>@2@</mq:artistName>\n" \
114         "</mq:FindAlbum>\n", ppsz_args ) )
115     {
116         mb_GetQueryError( p_mb, psz_buf, 256 );
117         msg_Err( p_obj, "Query failed: %s", psz_buf );
118         mb_Delete( p_mb );
119         free( psz_artist );
120         free( psz_album );
121         return VLC_EGENERIC;
122     }
123     free( psz_artist );
124     free( psz_album );
125
126     i_album_count = mb_GetResultInt( p_mb, MBE_GetNumAlbums );
127     if( i_album_count < 1 )
128     {
129         mb_Delete( p_mb );
130         return VLC_EGENERIC;
131     }
132
133     /** \todo Get the MB Track ID and store it */
134     msg_Dbg( p_obj, "found %d albums.\n", i_album_count );
135
136     for( i = 1; i <= i_album_count; i++ )
137     {
138         mb_Select( p_mb, MBS_Rewind );
139         mb_Select1( p_mb, MBS_SelectAlbum, i );
140
141         mb_GetResultData( p_mb, MBE_AlbumGetAlbumId, psz_data, 256 );
142         mb_GetIDFromURL( p_mb, psz_data, psz_buf, 256 );
143         msg_Dbg( p_obj, "album Id: %s", psz_buf );
144
145
146         if( !b_art )
147             break;
148
149         if( mb_GetResultData( p_mb, MBE_AlbumGetAmazonAsin, psz_buf, 256 ) )
150         {
151             msg_Dbg( p_obj, "Amazon ASIN: %s", psz_buf );
152             snprintf( psz_data, 255,
153                     "http://images.amazon.com/images/P/%s.01._SCLZZZZZZZ_.jpg",
154                     psz_buf );
155             msg_Dbg( p_obj, "Album art URL: %s", psz_data );
156             input_item_SetArtURL( p_item, psz_data );
157             b_art_found = true;
158             break;
159         }
160     }
161 #ifdef WIN32
162     mb_WSAInit( p_mb );
163 #endif
164     mb_Delete( p_mb );
165
166     if( !b_art )
167         return VLC_SUCCESS;
168     else
169         return b_art_found ? VLC_SUCCESS : VLC_EGENERIC;
170 }
171
172 static int FindMetaMBId( vlc_object_t *p_this )
173 {
174     meta_engine_t *p_me = (meta_engine_t *)p_this;
175     input_item_t *p_item = p_me->p_item;
176     int i_ret = GetData( VLC_OBJECT(p_me), p_item,
177                          p_me->i_mandatory & VLC_META_ENGINE_ART_URL );
178
179     if( !i_ret )
180     {
181         uint32_t i_meta = input_CurrentMetaFlags( input_item_GetMetaObject( p_item ) );
182         p_me->i_mandatory &= ~i_meta;
183         p_me->i_optional &= ~i_meta;
184         return p_me->i_mandatory ? VLC_EGENERIC : VLC_SUCCESS;
185     }
186     return VLC_EGENERIC;
187 }
188
189 static int FindArt( vlc_object_t *p_this )
190 {
191     playlist_t *p_playlist = (playlist_t *)p_this;
192     input_item_t *p_item = (input_item_t *)(p_playlist->p_private);
193     assert( p_item );
194
195     return GetData( VLC_OBJECT(p_playlist), p_item, true );
196 }