1 /*****************************************************************************
2 * meta.c: Get meta/artwork using lua scripts
3 *****************************************************************************
4 * Copyright (C) 2007-2008 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea at videolan tod org>
8 * Pierre d'Herbemont <pdherbemont # videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
36 #include <vlc_common.h>
37 #include <vlc_input.h>
38 #include <vlc_playlist.h>
41 #include <vlc_strings.h>
42 #include <vlc_stream.h>
43 #include <vlc_charset.h>
49 /*****************************************************************************
51 *****************************************************************************/
52 static int fetch_art( vlc_object_t *p_this, const char * psz_filename,
53 lua_State * L, void * user_data );
54 static lua_State *vlclua_meta_init( vlc_object_t *p_this,
55 input_item_t * p_item );
58 /*****************************************************************************
60 *****************************************************************************/
61 static lua_State * vlclua_meta_init( vlc_object_t *p_this, input_item_t * p_item )
63 lua_State * L = luaL_newstate();
66 msg_Err( p_this, "Could not create new Lua State" );
71 /* Load Lua libraries */
72 luaL_openlibs( L ); /* XXX: Don't open all the libs? */
74 luaL_register( L, "vlc", NULL /* FIXME ? */ );
80 lua_pushlightuserdata( L, p_this );
81 lua_setfield( L, -2, "private" );
83 psz_meta = input_item_GetName( p_item );
84 lua_pushstring( L, psz_meta );
85 lua_setfield( L, -2, "name" );
88 psz_meta = input_item_GetArtist( p_item );
89 lua_pushstring( L, psz_meta );
90 lua_setfield( L, -2, "artist" );
93 psz_meta = input_item_GetTitle( p_item ) ;
94 lua_pushstring( L, psz_meta );
95 lua_setfield( L, -2, "title" );
98 psz_meta = input_item_GetAlbum( p_item );
99 lua_pushstring( L, psz_meta );
100 lua_setfield( L, -2, "album" );
103 psz_meta = input_item_GetArtURL( p_item );
104 lua_pushstring( L, psz_meta );
105 lua_setfield( L, -2, "arturl" );
107 /* XXX: all should be passed ( could use macro ) */
112 /*****************************************************************************
113 * Called through lua_scripts_batch_execute to call 'fetch_art' on the script
114 * pointed by psz_filename.
115 *****************************************************************************/
116 static int fetch_art( vlc_object_t *p_this, const char * psz_filename,
117 lua_State * L, void * user_data )
119 int i_ret = VLC_EGENERIC;
120 input_item_t * p_input = user_data;
123 /* Ugly hack to delete previous versions of the fetchart()
126 lua_setglobal( L, "fetch_art" );
128 /* Load and run the script(s) */
129 if( luaL_dofile( L, psz_filename ) )
131 msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
132 lua_tostring( L, lua_gettop( L ) ) );
137 lua_getglobal( L, "fetch_art" );
139 if( !lua_isfunction( L, lua_gettop( L ) ) )
141 msg_Warn( p_this, "Error while runing script %s, "
142 "function fetch_art() not found", psz_filename );
147 if( lua_pcall( L, 0, 1, 0 ) )
149 msg_Warn( p_this, "Error while runing script %s, "
150 "function fetch_art(): %s", psz_filename,
151 lua_tostring( L, lua_gettop( L ) ) );
156 if((s = lua_gettop( L )))
158 const char * psz_value;
160 if( lua_isstring( L, s ) )
162 psz_value = lua_tostring( L, s );
163 if( psz_value && *psz_value != 0 )
165 lua_Dbg( p_this, "setting arturl: %s", psz_value );
166 input_item_SetArtURL ( p_input, psz_value );
170 else if( !lua_isnil( L, s ) )
172 msg_Err( p_this, "Lua art fetcher script %s: "
173 "didn't return a string", psz_filename );
178 msg_Err( p_this, "Script went completely foobar" );
184 /*****************************************************************************
185 * Module entry point for art.
186 *****************************************************************************/
187 int FindArt( vlc_object_t *p_this )
189 playlist_t *p_playlist = (playlist_t *)p_this;
190 input_item_t *p_item = (input_item_t *)(p_playlist->p_private);
191 lua_State *L = vlclua_meta_init( p_this, p_item );
193 int i_ret = vlclua_scripts_batch_execute( p_this, "meta", &fetch_art, L, p_item );