]> git.sesse.net Git - vlc/blob - modules/misc/lua/meta.c
Export VLC's md5 API.
[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     /* Load Lua libraries */
64     luaL_openlibs( L ); /* XXX: Don't open all the libs? */
65
66     luaL_register( L, "vlc", p_reg );
67
68     luaopen_msg( L );
69     luaopen_stream( L );
70     luaopen_strings( L );
71     luaopen_variables( L );
72     luaopen_object( L );
73     luaopen_misc( L );
74     luaopen_xml( L );
75     luaopen_md5( L );
76     luaopen_input_item( L, p_item );
77
78     lua_pushlightuserdata( L, p_this );
79     lua_setfield( L, -2, "private" );
80
81     if( vlclua_add_modules_path( p_this, L, psz_filename ) )
82     {
83         msg_Warn( p_this, "Error while setting the module search path for %s",
84                   psz_filename );
85         lua_close( L );
86         return NULL;
87     }
88
89     return L;
90 }
91
92 /*****************************************************************************
93  * Run a lua entry point function
94  *****************************************************************************/
95 static int run( vlc_object_t *p_this, const char * psz_filename,
96                 lua_State * L, const char *luafunction )
97 {
98     /* Ugly hack to delete previous versions of the fetchart()
99      * functions. */
100     lua_pushnil( L );
101     lua_setglobal( L, luafunction );
102
103     /* Load and run the script(s) */
104     if( luaL_dofile( L, psz_filename ) )
105     {
106         msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
107                  lua_tostring( L, lua_gettop( L ) ) );
108         goto error;
109     }
110
111     lua_getglobal( L, luafunction );
112
113     if( !lua_isfunction( L, lua_gettop( L ) ) )
114     {
115         msg_Warn( p_this, "Error while runing script %s, "
116                  "function %s() not found", psz_filename, luafunction );
117         goto error;
118     }
119
120     if( lua_pcall( L, 0, 1, 0 ) )
121     {
122         msg_Warn( p_this, "Error while runing script %s, "
123                  "function %s(): %s", psz_filename, luafunction,
124                  lua_tostring( L, lua_gettop( L ) ) );
125         goto error;
126     }
127     return VLC_SUCCESS;
128
129 error:
130     lua_pop( L, 1 );
131     return VLC_EGENERIC;
132 }
133
134 /*****************************************************************************
135  * Called through lua_scripts_batch_execute to call 'fetch_art' on the script
136  * pointed by psz_filename.
137  *****************************************************************************/
138 static int fetch_art( vlc_object_t *p_this, const char * psz_filename,
139                       void * user_data )
140 {
141     input_item_t * p_item = user_data;
142     int s;
143
144     lua_State *L = init( p_this, p_item, psz_filename );
145     if( !L )
146         return VLC_EGENERIC;
147
148     int i_ret = run(p_this, psz_filename, L, "fetch_art");
149     if(i_ret != VLC_SUCCESS)
150     {
151         lua_close( L );
152         return i_ret;
153     }
154
155     if((s = lua_gettop( L )))
156     {
157         const char * psz_value;
158
159         if( lua_isstring( L, s ) )
160         {
161             psz_value = lua_tostring( L, s );
162             if( psz_value && *psz_value != 0 )
163             {
164                 lua_Dbg( p_this, "setting arturl: %s", psz_value );
165                 input_item_SetArtURL ( p_item, psz_value );
166                 lua_close( L );
167                 return VLC_SUCCESS;
168             }
169         }
170         else if( !lua_isnil( L, s ) )
171         {
172             msg_Err( p_this, "Lua art fetcher script %s: "
173                  "didn't return a string", psz_filename );
174         }
175     }
176     else
177     {
178         msg_Err( p_this, "Script went completely foobar" );
179     }
180
181     lua_close( L );
182     return VLC_EGENERIC;
183 }
184
185 /*****************************************************************************
186  * Called through lua_scripts_batch_execute to call 'fetch_art' on the script
187  * pointed by psz_filename.
188  *****************************************************************************/
189 static int read_meta( vlc_object_t *p_this, const char * psz_filename,
190                       void * user_data )
191 {
192     input_item_t * p_item = user_data;
193     lua_State *L = init( p_this, p_item, psz_filename );
194     if( !L )
195         return VLC_EGENERIC;
196
197     int i_ret = run(p_this, psz_filename, L, "read_meta");
198     if(i_ret != VLC_SUCCESS)
199     {
200         lua_close( L );
201         return i_ret;
202     }
203
204     // Continue, all "meta reader" are always run.
205     lua_close( L );
206     return 1;
207 }
208
209
210 /*****************************************************************************
211  * Called through lua_scripts_batch_execute to call 'fetch_meta' on the script
212  * pointed by psz_filename.
213  *****************************************************************************/
214 static int fetch_meta( vlc_object_t *p_this, const char * psz_filename,
215                        void * user_data )
216 {
217     input_item_t * p_item = user_data;
218     lua_State *L = init( p_this, p_item, psz_filename );
219     if( !L )
220         return VLC_EGENERIC;
221
222     int ret = run(p_this, psz_filename, L, "fetch_meta");
223     lua_close( L );
224
225     return ret;
226 }
227
228 /*****************************************************************************
229  * Read meta.
230  *****************************************************************************/
231
232 int ReadMeta( vlc_object_t *p_this )
233 {
234     demux_meta_t *p_demux_meta = (demux_meta_t *)p_this;
235     input_item_t *p_item = p_demux_meta->p_item;
236
237     int i_ret = vlclua_scripts_batch_execute( p_this, "meta/reader", &read_meta, p_item );
238
239     return i_ret;
240 }
241
242
243 /*****************************************************************************
244  * Read meta.
245  *****************************************************************************/
246
247 int FetchMeta( vlc_object_t *p_this )
248 {
249     demux_meta_t *p_demux_meta = (demux_meta_t *)p_this;
250     input_item_t *p_item = p_demux_meta->p_item;
251
252     int i_ret = vlclua_scripts_batch_execute( p_this, "meta/fetcher", &fetch_meta, p_item );
253
254     return i_ret;
255 }
256
257
258 /*****************************************************************************
259  * Module entry point for art.
260  *****************************************************************************/
261 int FindArt( vlc_object_t *p_this )
262 {
263     art_finder_t *p_finder = (art_finder_t *)p_this;
264     input_item_t *p_item = p_finder->p_item;
265
266     int i_ret = vlclua_scripts_batch_execute( p_this, "meta/art", &fetch_art, p_item );
267
268     return i_ret;
269 }
270