]> git.sesse.net Git - vlc/blob - modules/meta_engine/musicbrainz.c
Don't loop preparse threads when idle
[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 FindMeta( vlc_object_t * );
39
40 /*****************************************************************************
41  * Module descriptor
42  *****************************************************************************/
43
44 vlc_module_begin();
45 /*    set_category( CAT_INTERFACE );
46     set_subcategory( SUBCAT_INTERFACE_CONTROL );*/
47     set_shortname( N_( "MusicBrainz" ) );
48     set_description( _("MusicBrainz meta data") );
49
50     set_capability( "meta fetcher", 80 );
51     set_callbacks( FindMeta, NULL );
52 vlc_module_end();
53
54 /*****************************************************************************
55  *****************************************************************************/
56 static int FindMeta( vlc_object_t *p_this )
57 {
58     meta_engine_t *p_me = (meta_engine_t *)p_this;
59     input_item_t *p_item = p_me->p_item;
60
61     char *psz_title = NULL;
62     char *psz_artist = NULL;
63     char *psz_album = NULL;
64
65     char psz_buf[256];
66     char psz_data[256];
67     char i_album_count, i;
68     char *ppsz_args[4];
69     uint32_t i_meta;
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     i_meta = input_CurrentMetaFlags( p_item->p_meta );
136     p_me->i_mandatory &= ~i_meta;
137     p_me->i_optional &= ~i_meta;
138     if( p_me->i_mandatory )
139         return VLC_EGENERIC;
140     else
141         return VLC_SUCCESS;
142 }