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>
39 #include <vlc_demux.h>
40 #include <vlc_art_finder.h>
42 #include <vlc_strings.h>
43 #include <vlc_stream.h>
48 /*****************************************************************************
50 *****************************************************************************/
51 static const luaL_Reg p_reg[] = { { NULL, NULL } };
53 static lua_State * init( vlc_object_t *p_this, input_item_t * p_item, const char *psz_filename )
55 lua_State * L = luaL_newstate();
58 msg_Err( p_this, "Could not create new Lua State" );
62 vlclua_set_this( L, p_this );
64 /* Load Lua libraries */
65 luaL_openlibs( L ); /* XXX: Don't open all the libs? */
67 luaL_register( L, "vlc", p_reg );
72 luaopen_variables( L );
77 luaopen_input_item( L, p_item );
79 if( vlclua_add_modules_path( p_this, L, psz_filename ) )
81 msg_Warn( p_this, "Error while setting the module search path for %s",
90 /*****************************************************************************
91 * Run a lua entry point function
92 *****************************************************************************/
93 static int run( vlc_object_t *p_this, const char * psz_filename,
94 lua_State * L, const char *luafunction )
96 /* Ugly hack to delete previous versions of the fetchart()
99 lua_setglobal( L, luafunction );
101 /* Load and run the script(s) */
102 if( luaL_dofile( L, psz_filename ) )
104 msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
105 lua_tostring( L, lua_gettop( L ) ) );
109 lua_getglobal( L, luafunction );
111 if( !lua_isfunction( L, lua_gettop( L ) ) )
113 msg_Warn( p_this, "Error while running script %s, "
114 "function %s() not found", psz_filename, luafunction );
118 if( lua_pcall( L, 0, 1, 0 ) )
120 msg_Warn( p_this, "Error while running script %s, "
121 "function %s(): %s", psz_filename, luafunction,
122 lua_tostring( L, lua_gettop( L ) ) );
132 /*****************************************************************************
133 * Called through lua_scripts_batch_execute to call 'fetch_art' on the script
134 * pointed by psz_filename.
135 *****************************************************************************/
136 static int fetch_art( vlc_object_t *p_this, const char * psz_filename,
139 input_item_t * p_item = user_data;
141 lua_State *L = init( p_this, p_item, psz_filename );
145 int i_ret = run(p_this, psz_filename, L, "fetch_art");
146 if(i_ret != VLC_SUCCESS)
154 const char * psz_value;
156 if( lua_isstring( L, -1 ) )
158 psz_value = lua_tostring( L, -1 );
159 if( psz_value && *psz_value != 0 )
161 lua_Dbg( p_this, "setting arturl: %s", psz_value );
162 input_item_SetArtURL ( p_item, psz_value );
167 else if( !lua_isnoneornil( L, -1 ) )
169 msg_Err( p_this, "Lua art fetcher script %s: "
170 "didn't return a string", psz_filename );
175 msg_Err( p_this, "Script went completely foobar" );
182 /*****************************************************************************
183 * Called through lua_scripts_batch_execute to call 'fetch_art' on the script
184 * pointed by psz_filename.
185 *****************************************************************************/
186 static int read_meta( vlc_object_t *p_this, const char * psz_filename,
189 input_item_t * p_item = user_data;
190 lua_State *L = init( p_this, p_item, psz_filename );
194 int i_ret = run(p_this, psz_filename, L, "read_meta");
195 if(i_ret != VLC_SUCCESS)
201 // Continue, all "meta reader" are always run.
207 /*****************************************************************************
208 * Called through lua_scripts_batch_execute to call 'fetch_meta' on the script
209 * pointed by psz_filename.
210 *****************************************************************************/
211 static int fetch_meta( vlc_object_t *p_this, const char * psz_filename,
214 input_item_t * p_item = user_data;
215 lua_State *L = init( p_this, p_item, psz_filename );
219 int ret = run(p_this, psz_filename, L, "fetch_meta");
225 /*****************************************************************************
227 *****************************************************************************/
229 int ReadMeta( vlc_object_t *p_this )
231 demux_meta_t *p_demux_meta = (demux_meta_t *)p_this;
232 input_item_t *p_item = p_demux_meta->p_item;
234 return vlclua_scripts_batch_execute( p_this, "meta"DIR_SEP"reader",
235 &read_meta, p_item );
239 /*****************************************************************************
241 *****************************************************************************/
243 int FetchMeta( vlc_object_t *p_this )
245 demux_meta_t *p_demux_meta = (demux_meta_t *)p_this;
246 input_item_t *p_item = p_demux_meta->p_item;
248 return vlclua_scripts_batch_execute( p_this, "meta"DIR_SEP"fetcher",
249 &fetch_meta, p_item );
253 /*****************************************************************************
254 * Module entry point for art.
255 *****************************************************************************/
256 int FindArt( vlc_object_t *p_this )
258 art_finder_t *p_finder = (art_finder_t *)p_this;
259 input_item_t *p_item = p_finder->p_item;
261 return vlclua_scripts_batch_execute( p_this, "meta"DIR_SEP"art",
262 &fetch_art, p_item );