1 /*****************************************************************************
2 * luameta.c: Get meta/artwork using lua scripts
3 *****************************************************************************
4 * Copyright (C) 2007 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 *****************************************************************************/
33 #include <vlc_input.h>
34 #include <vlc_playlist.h>
37 #include <vlc_strings.h>
38 #include <vlc_stream.h>
39 #include <vlc_charset.h>
44 /*****************************************************************************
46 *****************************************************************************/
47 static int fetch_meta( vlc_object_t *p_this, const char * psz_filename,
48 lua_State * p_state, void * user_data );
49 static int fetch_art( vlc_object_t *p_this, const char * psz_filename,
50 lua_State * p_state, void * user_data );
51 static lua_State * vlclua_meta_init( vlc_object_t *p_this, input_item_t * p_item );
54 /*****************************************************************************
56 *****************************************************************************/
57 /* Functions to register */
58 static luaL_Reg p_reg[] =
60 { "stream_new", vlclua_stream_new },
61 { "stream_read", vlclua_stream_read },
62 { "stream_readline", vlclua_stream_readline },
63 { "stream_delete", vlclua_stream_delete },
64 { "decode_uri", vlclua_decode_uri },
65 { "resolve_xml_special_chars", vlclua_resolve_xml_special_chars },
66 { "msg_dbg", vlclua_msg_dbg },
67 { "msg_warn", vlclua_msg_warn },
68 { "msg_err", vlclua_msg_err },
69 { "msg_info", vlclua_msg_info },
73 /*****************************************************************************
75 *****************************************************************************/
76 static lua_State * vlclua_meta_init( vlc_object_t *p_this, input_item_t * p_item )
78 lua_State * p_state = luaL_newstate();
81 msg_Err( p_this, "Could not create new Lua State" );
86 /* Load Lua libraries */
87 luaL_openlibs( p_state ); /* XXX: Don't open all the libs? */
89 luaL_register( p_state, "vlc", p_reg );
91 lua_pushlightuserdata( p_state, p_this );
92 lua_setfield( p_state, lua_gettop( p_state ) - 1, "private" );
94 psz_meta = input_item_GetName( p_item );
95 lua_pushstring( p_state, psz_meta );
96 lua_setfield( p_state, lua_gettop( p_state ) - 1, "name" );
99 psz_meta = input_item_GetArtist( p_item );
100 lua_pushstring( p_state, psz_meta );
101 lua_setfield( p_state, lua_gettop( p_state ) - 1, "artist" );
104 psz_meta = input_item_GetTitle( p_item ) ;
105 lua_pushstring( p_state, psz_meta );
106 lua_setfield( p_state, lua_gettop( p_state ) - 1, "title" );
109 psz_meta = input_item_GetAlbum( p_item );
110 lua_pushstring( p_state, psz_meta );
111 lua_setfield( p_state, lua_gettop( p_state ) - 1, "album" );
114 psz_meta = input_item_GetArtURL( p_item );
115 lua_pushstring( p_state, psz_meta );
116 lua_setfield( p_state, lua_gettop( p_state ) - 1, "arturl" );
118 /* XXX: all should be passed ( could use macro ) */
123 /*****************************************************************************
124 * Called through lua_scripts_batch_execute to call 'fetch_art' on the script
125 * pointed by psz_filename.
126 *****************************************************************************/
127 static int fetch_art( vlc_object_t *p_this, const char * psz_filename,
128 lua_State * p_state, void * user_data )
130 int i_ret = VLC_EGENERIC;
131 input_item_t * p_input = user_data;
134 /* Ugly hack to delete previous versions of the fetchart()
136 lua_pushnil( p_state );
137 lua_setglobal( p_state, "fetch_art" );
139 /* Load and run the script(s) */
140 if( luaL_dofile( p_state, psz_filename ) )
142 msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
143 lua_tostring( p_state, lua_gettop( p_state ) ) );
144 lua_pop( p_state, 1 );
148 lua_getglobal( p_state, "fetch_art" );
150 if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
152 msg_Warn( p_this, "Error while runing script %s, "
153 "function fetch_art() not found", psz_filename );
154 lua_pop( p_state, 1 );
158 if( lua_pcall( p_state, 0, 1, 0 ) )
160 msg_Warn( p_this, "Error while runing script %s, "
161 "function fetch_art(): %s", psz_filename,
162 lua_tostring( p_state, lua_gettop( p_state ) ) );
163 lua_pop( p_state, 1 );
167 if((s = lua_gettop( p_state )))
169 const char * psz_value;
171 if( lua_isstring( p_state, s ) )
173 psz_value = lua_tostring( p_state, s );
174 if( psz_value && *psz_value != 0 )
176 msg_Dbg( p_this, "setting arturl: %s", psz_value );
177 input_item_SetArtURL ( p_input, psz_value );
183 msg_Err( p_this, "Lua art fetcher script %s: "
184 "didn't return a string", psz_filename );
189 msg_Err( p_this, "Script went completely foobar" );
195 /*****************************************************************************
196 * Called through lua_scripts_batch_execute to call 'fetch_meta' on the script
197 * pointed by psz_filename.
198 *****************************************************************************/
199 static int fetch_meta( vlc_object_t *p_this, const char * psz_filename,
200 lua_State * p_state, void * user_data )
202 input_item_t * p_input = user_data;
205 /* Ugly hack to delete previous versions of the fetchmeta()
207 lua_pushnil( p_state );
208 lua_setglobal( p_state, "fetch_meta" );
210 /* Load and run the script(s) */
211 if( luaL_dofile( p_state, psz_filename ) )
213 msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
214 lua_tostring( p_state, lua_gettop( p_state ) ) );
215 lua_pop( p_state, 1 );
219 lua_getglobal( p_state, "fetch_meta" );
221 if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
223 msg_Warn( p_this, "Error while runing script %s, "
224 "function fetch_meta() not found", psz_filename );
225 lua_pop( p_state, 1 );
229 if( lua_pcall( p_state, 0, 1, 0 ) )
231 msg_Warn( p_this, "Error while runing script %s, "
232 "function fetch_meta(): %s", psz_filename,
233 lua_tostring( p_state, lua_gettop( p_state ) ) );
234 lua_pop( p_state, 1 );
239 if((t = lua_gettop( p_state )))
241 if( lua_istable( p_state, t ) )
243 vlclua_read_meta_data( p_this, p_state, t, t+1, p_input );
244 vlclua_read_custom_meta_data( p_this, p_state, t, t+1, p_input );
248 msg_Err( p_this, "Lua playlist script %s: "
249 "didn't return a table", psz_filename );
254 msg_Err( p_this, "Script went completely foobar" );
257 /* We tell the batch thing to continue, hence all script
258 * will get the change to add its meta. This behaviour could
263 /*****************************************************************************
264 * Module entry point for meta.
265 *****************************************************************************/
266 int E_(FindMeta)( vlc_object_t *p_this )
268 meta_engine_t *p_me = (meta_engine_t *)p_this;
269 input_item_t *p_item = p_me->p_item;
270 lua_State *p_state = vlclua_meta_init( p_this, p_item );
272 int i_ret = vlclua_scripts_batch_execute( p_this, "luameta", &fetch_meta, p_state, p_item );
273 lua_close( p_state );
277 /*****************************************************************************
278 * Module entry point for art.
279 *****************************************************************************/
280 int E_(FindArt)( vlc_object_t *p_this )
282 playlist_t *p_playlist = (playlist_t *)p_this;
283 input_item_t *p_item = (input_item_t *)(p_playlist->p_private);
284 lua_State *p_state = vlclua_meta_init( p_this, p_item );
286 int i_ret = vlclua_scripts_batch_execute( p_this, "luameta", &fetch_art, p_state, p_item );
287 lua_close( p_state );