]> git.sesse.net Git - vlc/blob - modules/misc/lua/meta.c
Allow lua "meta reader" and implement a meta reader from filename.
[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 #include <vlc_charset.h>
46
47 #include "vlc.h"
48 #include "libs.h"
49
50 /*****************************************************************************
51  * Init lua
52  *****************************************************************************/
53 static const luaL_Reg p_reg[] = { { NULL, NULL } };
54
55 static lua_State * init( vlc_object_t *p_this, input_item_t * p_item )
56 {
57     lua_State * L = luaL_newstate();
58     if( !L )
59     {
60         msg_Err( p_this, "Could not create new Lua State" );
61         return NULL;
62     }
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_misc( L );
75     luaopen_input_item( L, p_item );
76
77     lua_pushlightuserdata( L, p_this );
78     lua_setfield( L, -2, "private" );
79
80     return L;
81 }
82
83 /*****************************************************************************
84  * Run a lua entry point function
85  *****************************************************************************/
86 static int run( vlc_object_t *p_this, const char * psz_filename,
87                 lua_State * L, const char *luafunction )
88 {
89     /* Ugly hack to delete previous versions of the fetchart()
90      * functions. */
91     lua_pushnil( L );
92     lua_setglobal( L, luafunction );
93
94     /* Load and run the script(s) */
95     if( luaL_dofile( L, psz_filename ) )
96     {
97         msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
98                  lua_tostring( L, lua_gettop( L ) ) );
99         goto error;
100     }
101
102     lua_getglobal( L, luafunction );
103
104     if( !lua_isfunction( L, lua_gettop( L ) ) )
105     {
106         msg_Warn( p_this, "Error while runing script %s, "
107                  "function %s() not found", psz_filename, luafunction );
108         goto error;
109     }
110
111     if( lua_pcall( L, 0, 1, 0 ) )
112     {
113         msg_Warn( p_this, "Error while runing script %s, "
114                  "function %s(): %s", psz_filename, luafunction,
115                  lua_tostring( L, lua_gettop( L ) ) );
116         goto error;
117     }
118     return VLC_SUCCESS;
119
120 error:
121     lua_pop( L, 1 );
122     return VLC_EGENERIC;
123 }
124
125 /*****************************************************************************
126  * Called through lua_scripts_batch_execute to call 'fetch_art' on the script
127  * pointed by psz_filename.
128  *****************************************************************************/
129 static int fetch_art( vlc_object_t *p_this, const char * psz_filename,
130                       lua_State * L, void * user_data )
131 {
132     input_item_t * p_input = user_data;
133     int s;
134
135     int i_ret = run(p_this, psz_filename, L, "fetch_art");
136     if(i_ret != VLC_SUCCESS)
137         return i_ret;
138
139     if((s = lua_gettop( L )))
140     {
141         const char * psz_value;
142
143         if( lua_isstring( L, s ) )
144         {
145             psz_value = lua_tostring( L, s );
146             if( psz_value && *psz_value != 0 )
147             {
148                 lua_Dbg( p_this, "setting arturl: %s", psz_value );
149                 input_item_SetArtURL ( p_input, psz_value );
150                 return VLC_SUCCESS;
151             }
152         }
153         else if( !lua_isnil( L, s ) )
154         {
155             msg_Err( p_this, "Lua art fetcher script %s: "
156                  "didn't return a string", psz_filename );
157         }
158     }
159     else
160     {
161         msg_Err( p_this, "Script went completely foobar" );
162     }
163
164     return VLC_EGENERIC;
165 }
166
167 /*****************************************************************************
168  * Called through lua_scripts_batch_execute to call 'fetch_art' on the script
169  * pointed by psz_filename.
170  *****************************************************************************/
171 static int read_meta( vlc_object_t *p_this, const char * psz_filename,
172                      lua_State * L, void * user_data )
173 {
174     VLC_UNUSED(user_data);
175
176     int i_ret = run(p_this, psz_filename, L, "read_meta");
177     if(i_ret != VLC_SUCCESS)
178         return i_ret;
179
180     // Continue, all "meta reader" are always run.
181     return 1;
182 }
183
184 /*****************************************************************************
185  * Read meta.
186  *****************************************************************************/
187
188 int ReadMeta( vlc_object_t *p_this )
189 {
190     demux_meta_t *p_demux_meta = (demux_meta_t *)p_this;
191     input_item_t *p_item = p_demux_meta->p_item;
192
193     lua_State *L = init( p_this, p_item );
194     int i_ret = vlclua_scripts_batch_execute( p_this, "meta/reader", &read_meta, L, NULL );
195     lua_close( L );
196
197     return i_ret;
198 }
199
200 /*****************************************************************************
201  * Module entry point for art.
202  *****************************************************************************/
203 int FindArt( vlc_object_t *p_this )
204 {
205     art_finder_t *p_finder = (art_finder_t *)p_this;
206     input_item_t *p_item = p_finder->p_item;
207     playlist_t *p_playlist = pl_Hold( p_this );
208     if( !p_playlist )
209         return VLC_EGENERIC;
210
211     lua_State *L = init( p_this, p_item );
212     int i_ret = vlclua_scripts_batch_execute( p_this, "meta/art", &fetch_art, L, p_item );
213     lua_close( L );
214
215     pl_Release( p_this );
216     return i_ret;
217 }
218