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