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>
40 #include <vlc_demux.h>
41 #include <vlc_art_finder.h>
43 #include <vlc_strings.h>
44 #include <vlc_stream.h>
49 /*****************************************************************************
51 *****************************************************************************/
52 static const luaL_Reg p_reg[] = { { NULL, NULL } };
54 static lua_State * init( vlc_object_t *p_this, input_item_t * p_item )
56 lua_State * L = luaL_newstate();
59 msg_Err( p_this, "Could not create new Lua State" );
63 /* Load Lua libraries */
64 luaL_openlibs( L ); /* XXX: Don't open all the libs? */
66 luaL_register( L, "vlc", p_reg );
71 luaopen_variables( L );
74 luaopen_input_item( L, p_item );
76 lua_pushlightuserdata( L, p_this );
77 lua_setfield( L, -2, "private" );
82 /*****************************************************************************
83 * Run a lua entry point function
84 *****************************************************************************/
85 static int run( vlc_object_t *p_this, const char * psz_filename,
86 lua_State * L, const char *luafunction )
88 /* Ugly hack to delete previous versions of the fetchart()
91 lua_setglobal( L, luafunction );
93 /* Load and run the script(s) */
94 if( luaL_dofile( L, psz_filename ) )
96 msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
97 lua_tostring( L, lua_gettop( L ) ) );
101 lua_getglobal( L, luafunction );
103 if( !lua_isfunction( L, lua_gettop( L ) ) )
105 msg_Warn( p_this, "Error while runing script %s, "
106 "function %s() not found", psz_filename, luafunction );
110 if( lua_pcall( L, 0, 1, 0 ) )
112 msg_Warn( p_this, "Error while runing script %s, "
113 "function %s(): %s", psz_filename, luafunction,
114 lua_tostring( L, lua_gettop( L ) ) );
124 /*****************************************************************************
125 * Called through lua_scripts_batch_execute to call 'fetch_art' on the script
126 * pointed by psz_filename.
127 *****************************************************************************/
128 static int fetch_art( vlc_object_t *p_this, const char * psz_filename,
131 input_item_t * p_item = user_data;
134 lua_State *L = init( p_this, p_item );
136 int i_ret = run(p_this, psz_filename, L, "fetch_art");
137 if(i_ret != VLC_SUCCESS)
143 if((s = lua_gettop( L )))
145 const char * psz_value;
147 if( lua_isstring( L, s ) )
149 psz_value = lua_tostring( L, s );
150 if( psz_value && *psz_value != 0 )
152 lua_Dbg( p_this, "setting arturl: %s", psz_value );
153 input_item_SetArtURL ( p_item, psz_value );
158 else if( !lua_isnil( L, s ) )
160 msg_Err( p_this, "Lua art fetcher script %s: "
161 "didn't return a string", psz_filename );
166 msg_Err( p_this, "Script went completely foobar" );
173 /*****************************************************************************
174 * Called through lua_scripts_batch_execute to call 'fetch_art' on the script
175 * pointed by psz_filename.
176 *****************************************************************************/
177 static int read_meta( vlc_object_t *p_this, const char * psz_filename,
180 input_item_t * p_item = user_data;
181 lua_State *L = init( p_this, p_item );
183 int i_ret = run(p_this, psz_filename, L, "read_meta");
184 if(i_ret != VLC_SUCCESS)
190 // Continue, all "meta reader" are always run.
196 /*****************************************************************************
197 * Called through lua_scripts_batch_execute to call 'fetch_meta' on the script
198 * pointed by psz_filename.
199 *****************************************************************************/
200 static int fetch_meta( vlc_object_t *p_this, const char * psz_filename,
203 input_item_t * p_item = user_data;
204 lua_State *L = init( p_this, p_item );
206 int ret = run(p_this, psz_filename, L, "fetch_meta");
212 /*****************************************************************************
214 *****************************************************************************/
216 int ReadMeta( vlc_object_t *p_this )
218 demux_meta_t *p_demux_meta = (demux_meta_t *)p_this;
219 input_item_t *p_item = p_demux_meta->p_item;
221 int i_ret = vlclua_scripts_batch_execute( p_this, "meta/reader", &read_meta, p_item );
227 /*****************************************************************************
229 *****************************************************************************/
231 int FetchMeta( vlc_object_t *p_this )
233 demux_meta_t *p_demux_meta = (demux_meta_t *)p_this;
234 input_item_t *p_item = p_demux_meta->p_item;
236 int i_ret = vlclua_scripts_batch_execute( p_this, "meta/fetcher", &fetch_meta, p_item );
242 /*****************************************************************************
243 * Module entry point for art.
244 *****************************************************************************/
245 int FindArt( vlc_object_t *p_this )
247 art_finder_t *p_finder = (art_finder_t *)p_this;
248 input_item_t *p_item = p_finder->p_item;
250 int i_ret = vlclua_scripts_batch_execute( p_this, "meta/art", &fetch_art, p_item );