]> git.sesse.net Git - vlc/blob - modules/misc/lua/meta.c
Spelling
[vlc] / modules / misc / lua / meta.c
1 /*****************************************************************************
2  * meta.c: Get meta/artwork using lua scripts
3  *****************************************************************************
4  * Copyright (C) 2007-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod org>
8  *          Pierre d'Herbemont <pdherbemont # videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifndef  _GNU_SOURCE
29 #   define  _GNU_SOURCE
30 #endif
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <vlc_common.h>
37 #include <vlc_input.h>
38 #include <vlc_playlist.h>
39 #include <vlc_meta.h>
40 #include <vlc_demux.h>
41 #include <vlc_art_finder.h>
42 #include <vlc_url.h>
43 #include <vlc_strings.h>
44 #include <vlc_stream.h>
45
46 #include "vlc.h"
47 #include "libs.h"
48
49 /*****************************************************************************
50  * Init lua
51  *****************************************************************************/
52 static const luaL_Reg p_reg[] = { { NULL, NULL } };
53
54 static lua_State * init( vlc_object_t *p_this, input_item_t * p_item, const char *psz_filename )
55 {
56     lua_State * L = luaL_newstate();
57     if( !L )
58     {
59         msg_Err( p_this, "Could not create new Lua State" );
60         return NULL;
61     }
62
63     vlclua_set_this( L, p_this );
64
65     /* Load Lua libraries */
66     luaL_openlibs( L ); /* XXX: Don't open all the libs? */
67
68     luaL_register( L, "vlc", p_reg );
69
70     luaopen_msg( L );
71     luaopen_stream( L );
72     luaopen_strings( L );
73     luaopen_variables( L );
74     luaopen_object( L );
75     luaopen_misc( L );
76     luaopen_xml( L );
77     luaopen_md5( L );
78     luaopen_input_item( L, p_item );
79
80     if( vlclua_add_modules_path( p_this, L, psz_filename ) )
81     {
82         msg_Warn( p_this, "Error while setting the module search path for %s",
83                   psz_filename );
84         lua_close( L );
85         return NULL;
86     }
87
88     return L;
89 }
90
91 /*****************************************************************************
92  * Run a lua entry point function
93  *****************************************************************************/
94 static int run( vlc_object_t *p_this, const char * psz_filename,
95                 lua_State * L, const char *luafunction )
96 {
97     /* Ugly hack to delete previous versions of the fetchart()
98      * functions. */
99     lua_pushnil( L );
100     lua_setglobal( L, luafunction );
101
102     /* Load and run the script(s) */
103     if( luaL_dofile( L, psz_filename ) )
104     {
105         msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
106                  lua_tostring( L, lua_gettop( L ) ) );
107         goto error;
108     }
109
110     lua_getglobal( L, luafunction );
111
112     if( !lua_isfunction( L, lua_gettop( L ) ) )
113     {
114         msg_Warn( p_this, "Error while running script %s, "
115                  "function %s() not found", psz_filename, luafunction );
116         goto error;
117     }
118
119     if( lua_pcall( L, 0, 1, 0 ) )
120     {
121         msg_Warn( p_this, "Error while running script %s, "
122                  "function %s(): %s", psz_filename, luafunction,
123                  lua_tostring( L, lua_gettop( L ) ) );
124         goto error;
125     }
126     return VLC_SUCCESS;
127
128 error:
129     lua_pop( L, 1 );
130     return VLC_EGENERIC;
131 }
132
133 /*****************************************************************************
134  * Called through lua_scripts_batch_execute to call 'fetch_art' on the script
135  * pointed by psz_filename.
136  *****************************************************************************/
137 static int fetch_art( vlc_object_t *p_this, const char * psz_filename,
138                       void * user_data )
139 {
140     input_item_t * p_item = user_data;
141
142     lua_State *L = init( p_this, p_item, psz_filename );
143     if( !L )
144         return VLC_EGENERIC;
145
146     int i_ret = run(p_this, psz_filename, L, "fetch_art");
147     if(i_ret != VLC_SUCCESS)
148     {
149         lua_close( L );
150         return i_ret;
151     }
152
153     if(lua_gettop( L ))
154     {
155         const char * psz_value;
156
157         if( lua_isstring( L, -1 ) )
158         {
159             psz_value = lua_tostring( L, -1 );
160             if( psz_value && *psz_value != 0 )
161             {
162                 lua_Dbg( p_this, "setting arturl: %s", psz_value );
163                 input_item_SetArtURL ( p_item, psz_value );
164                 lua_close( L );
165                 return VLC_SUCCESS;
166             }
167         }
168         else if( !lua_isnoneornil( L, -1 ) )
169         {
170             msg_Err( p_this, "Lua art fetcher script %s: "
171                  "didn't return a string", psz_filename );
172         }
173     }
174     else
175     {
176         msg_Err( p_this, "Script went completely foobar" );
177     }
178
179     lua_close( L );
180     return VLC_EGENERIC;
181 }
182
183 /*****************************************************************************
184  * Called through lua_scripts_batch_execute to call 'fetch_art' on the script
185  * pointed by psz_filename.
186  *****************************************************************************/
187 static int read_meta( vlc_object_t *p_this, const char * psz_filename,
188                       void * user_data )
189 {
190     input_item_t * p_item = user_data;
191     lua_State *L = init( p_this, p_item, psz_filename );
192     if( !L )
193         return VLC_EGENERIC;
194
195     int i_ret = run(p_this, psz_filename, L, "read_meta");
196     if(i_ret != VLC_SUCCESS)
197     {
198         lua_close( L );
199         return i_ret;
200     }
201
202     // Continue, all "meta reader" are always run.
203     lua_close( L );
204     return 1;
205 }
206
207
208 /*****************************************************************************
209  * Called through lua_scripts_batch_execute to call 'fetch_meta' on the script
210  * pointed by psz_filename.
211  *****************************************************************************/
212 static int fetch_meta( vlc_object_t *p_this, const char * psz_filename,
213                        void * user_data )
214 {
215     input_item_t * p_item = user_data;
216     lua_State *L = init( p_this, p_item, psz_filename );
217     if( !L )
218         return VLC_EGENERIC;
219
220     int ret = run(p_this, psz_filename, L, "fetch_meta");
221     lua_close( L );
222
223     return ret;
224 }
225
226 /*****************************************************************************
227  * Read meta.
228  *****************************************************************************/
229
230 int ReadMeta( vlc_object_t *p_this )
231 {
232     demux_meta_t *p_demux_meta = (demux_meta_t *)p_this;
233     input_item_t *p_item = p_demux_meta->p_item;
234
235     return vlclua_scripts_batch_execute( p_this, "meta"DIR_SEP"reader",
236                                          &read_meta, p_item );
237 }
238
239
240 /*****************************************************************************
241  * Read meta.
242  *****************************************************************************/
243
244 int FetchMeta( vlc_object_t *p_this )
245 {
246     demux_meta_t *p_demux_meta = (demux_meta_t *)p_this;
247     input_item_t *p_item = p_demux_meta->p_item;
248
249     return vlclua_scripts_batch_execute( p_this, "meta"DIR_SEP"fetcher",
250                                          &fetch_meta, p_item );
251 }
252
253
254 /*****************************************************************************
255  * Module entry point for art.
256  *****************************************************************************/
257 int FindArt( vlc_object_t *p_this )
258 {
259     art_finder_t *p_finder = (art_finder_t *)p_this;
260     input_item_t *p_item = p_finder->p_item;
261
262     return vlclua_scripts_batch_execute( p_this, "meta"DIR_SEP"art",
263                                          &fetch_art, p_item );
264 }
265