]> git.sesse.net Git - vlc/blob - modules/meta_engine/musicbrainz.c
Start of meta engine stuff. src/input/input.c needs to be fixed a bit. I'll finish...
[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_engine.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 engine", 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
71     if( !p_item->p_meta ) return VLC_EGENERIC;
72     psz_artist = p_item->p_meta->psz_artist;
73     psz_album = p_item->p_meta->psz_album;
74     psz_title = p_item->psz_name;
75
76     if( !psz_artist || !psz_album )
77         return VLC_EGENERIC;
78     musicbrainz_t p_mb;
79
80     p_mb = mb_New();
81 #ifdef WIN32
82     mb_WSAInit( p_mb );
83 #endif
84     mb_SetDepth( p_mb, 2 );
85     ppsz_args[0] = psz_album;
86     ppsz_args[1] = psz_artist;
87     ppsz_args[2] = NULL;
88     if( !mb_QueryWithArgs( p_mb,
89         "<mq:FindAlbum>\n" \
90         "   <mq:depth>@DEPTH@</mq:depth>\n" \
91         "   <mq:maxItems>@MAX_ITEMS@</mq:maxItems>\n" \
92         "   <mq:albumName>@1@</mq:albumName>\n" \
93         "   <mq:artistName>@2@</mq:artistName>\n" \
94         "</mq:FindAlbum>\n", ppsz_args ) )
95     {
96         mb_GetQueryError( p_mb, psz_buf, 256 );
97         msg_Err( p_me, "Query failed: %s\n", psz_buf );
98         mb_Delete( p_mb );
99         return VLC_EGENERIC;
100     }
101
102     i_album_count = mb_GetResultInt( p_mb, MBE_GetNumAlbums );
103     if( i_album_count < 1 )
104     {
105         msg_Err( p_me, "No albums found.\n" );
106         mb_Delete( p_mb );
107         return VLC_EGENERIC;
108     }
109
110     msg_Dbg( p_me, "Found %d albums.\n", i_album_count );
111
112     for( i = 1; i <= i_album_count; i++ )
113     {
114         mb_Select( p_mb, MBS_Rewind );
115         mb_Select1( p_mb, MBS_SelectAlbum, i );
116
117         mb_GetResultData( p_mb, MBE_AlbumGetAlbumId, psz_data, 256 );
118         mb_GetIDFromURL( p_mb, psz_data, psz_buf, 256 );
119         msg_Dbg( p_me, "Album Id: %s", psz_buf );
120
121         if( mb_GetResultData( p_mb, MBE_AlbumGetAmazonAsin, psz_buf, 256 ) )
122         {
123             msg_Dbg( p_me, "Amazon ASIN: %s", psz_buf );
124             sprintf( psz_data, "http://images.amazon.com/images/P/%s.01._SCLZZZZZZZ_.jpg", psz_buf );
125             vlc_meta_SetArtURL( p_item->p_meta, psz_data );
126             break;
127         }
128     }
129 #ifdef WIN32
130     mb_WSAInit( p_mb );
131 #endif
132
133     mb_Delete( p_mb );
134
135     return VLC_SUCCESS;
136 }