]> git.sesse.net Git - vlc/blob - modules/meta_engine/musicbrainz.c
Some more meta changes (mostly cleanup and check the i_mandatory flags)
[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 #include <vlc_meta.h>
33
34 #include "musicbrainz/mb_c.h"
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39 static int FindMeta( vlc_object_t * );
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44
45 vlc_module_begin();
46 /*    set_category( CAT_INTERFACE );
47     set_subcategory( SUBCAT_INTERFACE_CONTROL );*/
48     set_shortname( N_( "MusicBrainz" ) );
49     set_description( _("MusicBrainz meta data") );
50
51     set_capability( "meta fetcher", 80 );
52     set_callbacks( FindMeta, NULL );
53 vlc_module_end();
54
55 /*****************************************************************************
56  *****************************************************************************/
57 static int FindMeta( vlc_object_t *p_this )
58 {
59     meta_engine_t *p_me = (meta_engine_t *)p_this;
60     input_item_t *p_item = p_me->p_item;
61
62     char *psz_title = NULL;
63     char *psz_artist = NULL;
64     char *psz_album = NULL;
65
66     char psz_buf[256];
67     char psz_data[256];
68     char i_album_count, i;
69     char *ppsz_args[4];
70     uint32_t i_meta;
71
72     if( !p_item->p_meta ) return VLC_EGENERIC;
73     psz_artist = p_item->p_meta->psz_artist;
74     psz_album = p_item->p_meta->psz_album;
75     psz_title = p_item->psz_name;
76
77     if( !psz_artist || !psz_album )
78         return VLC_EGENERIC;
79     musicbrainz_t p_mb;
80
81     p_mb = mb_New();
82 #ifdef WIN32
83     mb_WSAInit( p_mb );
84 #endif
85     mb_SetDepth( p_mb, 2 );
86     ppsz_args[0] = psz_album;
87     ppsz_args[1] = psz_artist;
88     ppsz_args[2] = NULL;
89     if( !mb_QueryWithArgs( p_mb,
90         "<mq:FindAlbum>\n" \
91         "   <mq:depth>@DEPTH@</mq:depth>\n" \
92         "   <mq:maxItems>@MAX_ITEMS@</mq:maxItems>\n" \
93         "   <mq:albumName>@1@</mq:albumName>\n" \
94         "   <mq:artistName>@2@</mq:artistName>\n" \
95         "</mq:FindAlbum>\n", ppsz_args ) )
96     {
97         mb_GetQueryError( p_mb, psz_buf, 256 );
98         msg_Err( p_me, "Query failed: %s\n", psz_buf );
99         mb_Delete( p_mb );
100         return VLC_EGENERIC;
101     }
102
103     i_album_count = mb_GetResultInt( p_mb, MBE_GetNumAlbums );
104     if( i_album_count < 1 )
105     {
106         msg_Err( p_me, "No albums found.\n" );
107         mb_Delete( p_mb );
108         return VLC_EGENERIC;
109     }
110
111     msg_Dbg( p_me, "Found %d albums.\n", i_album_count );
112
113     for( i = 1; i <= i_album_count; i++ )
114     {
115         mb_Select( p_mb, MBS_Rewind );
116         mb_Select1( p_mb, MBS_SelectAlbum, i );
117
118         mb_GetResultData( p_mb, MBE_AlbumGetAlbumId, psz_data, 256 );
119         mb_GetIDFromURL( p_mb, psz_data, psz_buf, 256 );
120         msg_Dbg( p_me, "Album Id: %s", psz_buf );
121
122         if( mb_GetResultData( p_mb, MBE_AlbumGetAmazonAsin, psz_buf, 256 ) )
123         {
124             msg_Dbg( p_me, "Amazon ASIN: %s", psz_buf );
125             sprintf( psz_data, "http://images.amazon.com/images/P/%s.01._SCLZZZZZZZ_.jpg", psz_buf );
126             vlc_meta_SetArtURL( p_item->p_meta, psz_data );
127             break;
128         }
129     }
130 #ifdef WIN32
131     mb_WSAInit( p_mb );
132 #endif
133
134     mb_Delete( p_mb );
135
136     i_meta = input_GetMetaEngineFlags( p_item->p_meta );
137     p_me->i_mandatory &= ~i_meta;
138     p_me->i_optional &= ~i_meta;
139     if( p_me->i_mandatory )
140         return VLC_EGENERIC;
141     else
142         return VLC_SUCCESS;
143 }