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