]> git.sesse.net Git - vlc/blob - modules/meta_engine/musicbrainz.c
Rewrite a useful tooltip for Windows DShow.
[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_common.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 #include <assert.h>
42
43 /*****************************************************************************
44  * Local prototypes
45  *****************************************************************************/
46 static int FindArt( vlc_object_t * );
47
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51
52 vlc_module_begin();
53     set_shortname( N_( "MusicBrainz" ) );
54     set_description( N_("MusicBrainz meta data") );
55
56     /* This art finder module fetches the album ID from musicbrainz and
57      * uses it to fetch the amazon ASIN from musicbrainz.
58      * TODO:
59      *  - Add ability to reuse MB album ID if we already have it
60      */
61     set_capability( "art finder", 80 );
62     set_callbacks( FindArt, NULL );
63 vlc_module_end();
64
65 /*****************************************************************************
66  *****************************************************************************/
67
68 static int GetData( vlc_object_t *p_obj, input_item_t *p_item,
69                     bool b_art )
70 {
71     char psz_buf[256];
72     char psz_data[256];
73     char i_album_count, i;
74     char *ppsz_args[4];
75     bool b_art_found = false;
76
77     char *psz_artist;
78     char *psz_album;
79
80     psz_artist = input_item_GetArtist( p_item );
81     psz_album = input_item_GetAlbum( p_item );
82
83     if( !psz_artist || !psz_album )
84     {
85         free( psz_artist );
86         free( psz_album );
87         return VLC_EGENERIC;
88     }
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", psz_buf );
111         mb_Delete( p_mb );
112         free( psz_artist );
113         free( psz_album );
114         return VLC_EGENERIC;
115     }
116     free( psz_artist );
117     free( psz_album );
118
119     i_album_count = mb_GetResultInt( p_mb, MBE_GetNumAlbums );
120     if( i_album_count < 1 )
121     {
122         mb_Delete( p_mb );
123         return VLC_EGENERIC;
124     }
125
126     /** \todo Get the MB Track ID and store it */
127     msg_Dbg( p_obj, "found %d albums.\n", i_album_count );
128
129     for( i = 1; i <= i_album_count; i++ )
130     {
131         mb_Select( p_mb, MBS_Rewind );
132         mb_Select1( p_mb, MBS_SelectAlbum, i );
133
134         mb_GetResultData( p_mb, MBE_AlbumGetAlbumId, psz_data, 256 );
135         mb_GetIDFromURL( p_mb, psz_data, psz_buf, 256 );
136         msg_Dbg( p_obj, "album Id: %s", psz_buf );
137
138
139         if( !b_art )
140             break;
141
142         if( mb_GetResultData( p_mb, MBE_AlbumGetAmazonAsin, psz_buf, 256 ) )
143         {
144             msg_Dbg( p_obj, "Amazon ASIN: %s", psz_buf );
145             snprintf( psz_data, 255,
146                     "http://images.amazon.com/images/P/%s.01._SCLZZZZZZZ_.jpg",
147                     psz_buf );
148             msg_Dbg( p_obj, "Album art URL: %s", psz_data );
149             input_item_SetArtURL( p_item, psz_data );
150             b_art_found = true;
151             break;
152         }
153     }
154 #ifdef WIN32
155     mb_WSAInit( p_mb );
156 #endif
157     mb_Delete( p_mb );
158
159     if( !b_art )
160         return VLC_SUCCESS;
161     else
162         return b_art_found ? VLC_SUCCESS : VLC_EGENERIC;
163 }
164
165 static int FindArt( vlc_object_t *p_this )
166 {
167     playlist_t *p_playlist = (playlist_t *)p_this;
168     input_item_t *p_item = (input_item_t *)(p_playlist->p_private);
169     assert( p_item );
170
171     return GetData( VLC_OBJECT(p_playlist), p_item, true );
172 }