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