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