1 /*****************************************************************************
2 * luameta.c: Get meta/artwork using lua scripts
3 *****************************************************************************
4 * Copyright (C) 2006 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 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
31 #include <vlc_input.h>
32 #include <vlc_playlist.h>
35 #include <vlc_strings.h>
36 #include <vlc_stream.h>
37 #include <vlc_charset.h>
39 #ifdef HAVE_SYS_STAT_H
40 # include <sys/stat.h>
43 #include <lua.h> /* Low level lua C API */
44 #include <lauxlib.h> /* Higher level C API */
45 #include <lualib.h> /* Lua libs */
47 /*****************************************************************************
49 *****************************************************************************/
50 static int FindArt( vlc_object_t * );
51 static int FindMeta( vlc_object_t *p_this );
53 /*****************************************************************************
55 *****************************************************************************/
58 set_shortname( N_( "Lua Meta" ) );
59 set_description( _("Fetch Artwork using lua scripts") );
60 set_capability( "meta fetcher", 10 );
61 set_callbacks( FindMeta, NULL );
63 set_capability( "art finder", 10 );
64 set_callbacks( FindArt, NULL );
67 /*****************************************************************************
69 *****************************************************************************/
70 static vlc_object_t * vlclua_get_this( lua_State *p_state )
72 vlc_object_t * p_this;
73 lua_getglobal( p_state, "vlc" );
74 lua_getfield( p_state, lua_gettop( p_state ), "private" );
75 p_this = (vlc_object_t*)lua_topointer( p_state, lua_gettop( p_state ) );
76 lua_pop( p_state, 2 );
80 static int vlclua_stream_new( lua_State *p_state )
82 vlc_object_t * p_this = vlclua_get_this( p_state );
83 int i = lua_gettop( p_state );
87 psz_url = lua_tostring( p_state, 1 );
88 lua_pop( p_state, i );
89 p_stream = stream_UrlNew( p_this, psz_url );
90 lua_pushlightuserdata( p_state, p_stream );
94 static int vlclua_stream_read( lua_State *p_state )
96 int i = lua_gettop( p_state );
102 p_stream = (stream_t *)lua_topointer( p_state, 1 );
103 n = lua_tonumber( p_state, 2 );
104 lua_pop( p_state, i );
105 p_read = malloc( n );
106 if( !p_read ) return 0;
107 i_read = stream_Read( p_stream, p_read, n );
108 lua_pushlstring( p_state, (const char *)p_read, i_read );
113 static int vlclua_stream_readline( lua_State *p_state )
115 int i = lua_gettop( p_state );
118 p_stream = (stream_t *)lua_topointer( p_state, 1 );
119 lua_pop( p_state, i );
120 char *psz_line = stream_ReadLine( p_stream );
123 lua_pushstring( p_state, psz_line );
128 lua_pushnil( p_state );
133 static int vlclua_stream_delete( lua_State *p_state )
135 int i = lua_gettop( p_state );
138 p_stream = (stream_t *)lua_topointer( p_state, 1 );
139 lua_pop( p_state, i );
140 stream_Delete( p_stream );
144 static int vlclua_decode_uri( lua_State *p_state )
146 int i = lua_gettop( p_state );
148 const char *psz_cstring = lua_tostring( p_state, 1 );
149 if( !psz_cstring ) return 0;
150 char *psz_string = strdup( psz_cstring );
151 lua_pop( p_state, i );
152 decode_URI( psz_string );
153 lua_pushstring( p_state, psz_string );
158 static int vlclua_resolve_xml_special_chars( lua_State *p_state )
160 int i = lua_gettop( p_state );
162 const char *psz_cstring = lua_tostring( p_state, 1 );
163 if( !psz_cstring ) return 0;
164 char *psz_string = strdup( psz_cstring );
165 lua_pop( p_state, i );
166 resolve_xml_special_chars( psz_string );
167 lua_pushstring( p_state, psz_string );
172 static int vlclua_msg_dbg( lua_State *p_state )
174 vlc_object_t *p_this = vlclua_get_this( p_state );
175 int i = lua_gettop( p_state );
177 const char *psz_cstring = lua_tostring( p_state, 1 );
178 if( !psz_cstring ) return 0;
179 msg_Dbg( p_this, "%s", psz_cstring );
182 static int vlclua_msg_warn( lua_State *p_state )
184 vlc_object_t *p_this = vlclua_get_this( p_state );
185 int i = lua_gettop( p_state );
187 const char *psz_cstring = lua_tostring( p_state, 1 );
188 if( !psz_cstring ) return 0;
189 msg_Warn( p_this, "%s", psz_cstring );
192 static int vlclua_msg_err( lua_State *p_state )
194 vlc_object_t *p_this = vlclua_get_this( p_state );
195 int i = lua_gettop( p_state );
197 const char *psz_cstring = lua_tostring( p_state, 1 );
198 if( !psz_cstring ) return 0;
199 msg_Err( p_this, "%s", psz_cstring );
202 static int vlclua_msg_info( lua_State *p_state )
204 vlc_object_t *p_this = vlclua_get_this( p_state );
205 int i = lua_gettop( p_state );
207 const char *psz_cstring = lua_tostring( p_state, 1 );
208 if( !psz_cstring ) return 0;
209 msg_Info( p_this, "%s", psz_cstring );
213 /* Functions to register */
214 static luaL_Reg p_reg[] =
216 { "stream_new", vlclua_stream_new },
217 { "stream_read", vlclua_stream_read },
218 { "stream_readline", vlclua_stream_readline },
219 { "stream_delete", vlclua_stream_delete },
220 { "decode_uri", vlclua_decode_uri },
221 { "resolve_xml_special_chars", vlclua_resolve_xml_special_chars },
222 { "msg_dbg", vlclua_msg_dbg },
223 { "msg_warn", vlclua_msg_warn },
224 { "msg_err", vlclua_msg_err },
225 { "msg_info", vlclua_msg_info },
228 /*****************************************************************************
230 *****************************************************************************/
231 static int file_select( const char *file )
233 int i = strlen( file );
234 return i > 4 && !strcmp( file+i-4, ".lua" );
237 static int file_compare( const char **a, const char **b )
239 return strcmp( *a, *b );
242 /*****************************************************************************
244 *****************************************************************************/
245 static lua_State * vlclua_meta_init( vlc_object_t *p_this, input_item_t * p_item )
247 lua_State * p_state = luaL_newstate();
250 msg_Err( p_this, "Could not create new Lua State" );
254 /* Load Lua libraries */
255 luaL_openlibs( p_state ); /* XXX: Don't open all the libs? */
257 luaL_register( p_state, "vlc", p_reg );
259 lua_pushlightuserdata( p_state, p_this );
260 lua_setfield( p_state, lua_gettop( p_state ) - 1, "private" );
262 lua_pushstring( p_state, p_item->psz_name );
263 lua_setfield( p_state, lua_gettop( p_state ) - 1, "name" );
265 lua_pushstring( p_state, input_item_GetTitle( p_item ) );
266 lua_setfield( p_state, lua_gettop( p_state ) - 1, "title" );
268 lua_pushstring( p_state, input_item_GetAlbum( p_item ) );
269 lua_setfield( p_state, lua_gettop( p_state ) - 1, "album" );
271 lua_pushstring( p_state, input_item_GetArtURL( p_item ) );
272 lua_setfield( p_state, lua_gettop( p_state ) - 1, "arturl" );
273 /* XXX: all should be passed */
278 /*****************************************************************************
279 * Will execute func on all scripts in luadirname, and stop if func returns
281 *****************************************************************************/
282 static int vlclua_scripts_batch_execute( vlc_object_t *p_this,
283 const char * luadirname,
284 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
288 int i_ret = VLC_EGENERIC;
291 char **ppsz_filelist = NULL;
292 char **ppsz_fileend = NULL;
295 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
298 if( asprintf( &ppsz_dir_list[0], "%s" DIR_SEP CONFIG_DIR DIR_SEP "%s", p_this->p_libvlc->psz_homedir,
302 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
304 const char *psz_vlcpath = config_GetDataDir();
305 if( asprintf( &ppsz_dir_list[1], "%s" DIR_SEP "%s", psz_vlcpath, luadirname ) < 0 )
308 if( asprintf( &ppsz_dir_list[2], "%s" DIR_SEP "share" DIR_SEP "%s", psz_vlcpath, luadirname ) < 0 )
313 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
319 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
322 free( ppsz_filelist );
323 ppsz_filelist = NULL;
331 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
332 dir = utf8_opendir( *ppsz_dir );
335 i_files = utf8_loaddir( dir, &ppsz_filelist, file_select, file_compare );
336 if( i_files < 1 ) continue;
337 ppsz_fileend = ppsz_filelist + i_files;
339 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
342 asprintf( &psz_filename, "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file );
343 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
345 i_ret = func( p_this, psz_filename, p_state, user_data );
347 free( psz_filename );
349 if( i_ret == VLC_SUCCESS ) break;
351 if( i_ret == VLC_SUCCESS ) break;
356 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
359 free( ppsz_filelist );
361 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
364 if( dir ) closedir( dir );
369 /*****************************************************************************
370 * Meta data setters utility.
371 *****************************************************************************/
372 static inline void read_meta_data( vlc_object_t *p_this,
373 lua_State *p_state, int o, int t,
374 input_item_t *p_input )
376 const char *psz_value;
377 #define TRY_META( a, b ) \
378 lua_getfield( p_state, o, a ); \
379 if( lua_isstring( p_state, t ) ) \
381 psz_value = lua_tostring( p_state, t ); \
382 msg_Dbg( p_this, #b ": %s", psz_value ); \
383 input_item_Set ## b ( p_input, psz_value ); \
385 lua_pop( p_state, 1 ); /* pop a */
386 TRY_META( "title", Title );
387 TRY_META( "artist", Artist );
388 TRY_META( "genre", Genre );
389 TRY_META( "copyright", Copyright );
390 TRY_META( "album", Album );
391 TRY_META( "tracknum", TrackNum );
392 TRY_META( "description", Description );
393 TRY_META( "rating", Rating );
394 TRY_META( "date", Date );
395 TRY_META( "setting", Setting );
396 TRY_META( "url", URL );
397 TRY_META( "language", Language );
398 TRY_META( "nowplaying", NowPlaying );
399 TRY_META( "publisher", Publisher );
400 TRY_META( "encodedby", EncodedBy );
401 TRY_META( "arturl", ArtURL );
402 TRY_META( "trackid", TrackID );
405 static inline void read_custom_meta_data( vlc_object_t *p_this,
406 lua_State *p_state, int o, int t,
407 input_item_t *p_input )
409 lua_getfield( p_state, o, "meta" );
410 if( lua_istable( p_state, t ) )
412 lua_pushnil( p_state );
413 while( lua_next( p_state, t ) )
415 if( !lua_isstring( p_state, t+1 ) )
417 msg_Warn( p_this, "Custom meta data category name must be "
420 else if( !lua_istable( p_state, t+2 ) )
422 msg_Warn( p_this, "Custom meta data category contents "
427 const char *psz_meta_category = lua_tostring( p_state, t+1 );
428 msg_Dbg( p_this, "Found custom meta data category: %s",
430 lua_pushnil( p_state );
431 while( lua_next( p_state, t+2 ) )
433 if( !lua_isstring( p_state, t+3 ) )
435 msg_Warn( p_this, "Custom meta category item name "
436 "must be a string." );
438 else if( !lua_isstring( p_state, t+4 ) )
440 msg_Warn( p_this, "Custom meta category item value "
441 "must be a string." );
445 const char *psz_meta_name =
446 lua_tostring( p_state, t+3 );
447 const char *psz_meta_value =
448 lua_tostring( p_state, t+4 );
449 msg_Dbg( p_this, "Custom meta %s, %s: %s",
450 psz_meta_category, psz_meta_name,
452 input_ItemAddInfo( p_input, psz_meta_category,
453 psz_meta_name, psz_meta_value );
455 lua_pop( p_state, 1 ); /* pop item */
458 lua_pop( p_state, 1 ); /* pop category */
461 lua_pop( p_state, 1 ); /* pop "meta" */
464 /*****************************************************************************
465 * Called through lua_scripts_batch_execute to call 'fetch_art' on the script
466 * pointed by psz_filename.
467 *****************************************************************************/
468 static int fetch_art( vlc_object_t *p_this, const char * psz_filename,
469 lua_State * p_state, void * user_data )
471 int i_ret = VLC_EGENERIC;
472 input_item_t * p_input = user_data;
475 /* Ugly hack to delete previous versions of the fetchart()
477 lua_pushnil( p_state );
478 lua_setglobal( p_state, "fetch_art" );
480 /* Load and run the script(s) */
481 if( luaL_dofile( p_state, psz_filename ) )
483 msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
484 lua_tostring( p_state, lua_gettop( p_state ) ) );
485 lua_pop( p_state, 1 );
489 lua_getglobal( p_state, "fetch_art" );
491 if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
493 msg_Warn( p_this, "Error while runing script %s, "
494 "function fetch_art() not found", psz_filename );
495 lua_pop( p_state, 1 );
499 if( lua_pcall( p_state, 0, 1, 0 ) )
501 msg_Warn( p_this, "Error while runing script %s, "
502 "function fetch_art(): %s", psz_filename,
503 lua_tostring( p_state, lua_gettop( p_state ) ) );
504 lua_pop( p_state, 1 );
508 if((s = lua_gettop( p_state )))
510 const char * psz_value;
512 if( lua_isstring( p_state, s ) )
514 psz_value = lua_tostring( p_state, s );
515 if( psz_value && *psz_value != 0 )
517 msg_Dbg( p_this, "setting arturl: %s", psz_value );
518 input_item_SetArtURL ( p_input, psz_value );
524 msg_Err( p_this, "Lua playlist script %s: "
525 "didn't return a string", psz_filename );
530 msg_Err( p_this, "Script went completely foobar" );
536 /*****************************************************************************
537 * Called through lua_scripts_batch_execute to call 'fetch_meta' on the script
538 * pointed by psz_filename.
539 *****************************************************************************/
540 static int fetch_meta( vlc_object_t *p_this, const char * psz_filename,
541 lua_State * p_state, void * user_data )
543 input_item_t * p_input = user_data;
546 /* Ugly hack to delete previous versions of the fetchmeta()
548 lua_pushnil( p_state );
549 lua_setglobal( p_state, "fetch_meta" );
551 /* Load and run the script(s) */
552 if( luaL_dofile( p_state, psz_filename ) )
554 msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
555 lua_tostring( p_state, lua_gettop( p_state ) ) );
556 lua_pop( p_state, 1 );
560 lua_getglobal( p_state, "fetch_meta" );
562 if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
564 msg_Warn( p_this, "Error while runing script %s, "
565 "function fetch_meta() not found", psz_filename );
566 lua_pop( p_state, 1 );
570 if( lua_pcall( p_state, 0, 1, 0 ) )
572 msg_Warn( p_this, "Error while runing script %s, "
573 "function fetch_meta(): %s", psz_filename,
574 lua_tostring( p_state, lua_gettop( p_state ) ) );
575 lua_pop( p_state, 1 );
580 if((t = lua_gettop( p_state )))
582 if( lua_istable( p_state, t ) )
584 read_meta_data( p_this, p_state, t, t+1, p_input );
585 read_custom_meta_data( p_this, p_state, t, t+1, p_input );
589 msg_Err( p_this, "Lua playlist script %s: "
590 "didn't return a table", psz_filename );
595 msg_Err( p_this, "Script went completely foobar" );
598 /* We tell the batch thing to continue, hence all script
599 * will get the change to add its meta. This behaviour could
604 /*****************************************************************************
605 * Module entry point for meta.
606 *****************************************************************************/
607 static int FindMeta( vlc_object_t *p_this )
609 meta_engine_t *p_me = (meta_engine_t *)p_this;
610 input_item_t *p_item = p_me->p_item;
611 lua_State *p_state = vlclua_meta_init( p_this, p_item );
613 int i_ret = vlclua_scripts_batch_execute( p_this, "luameta", &fetch_meta, p_state, p_item );
614 lua_close( p_state );
618 /*****************************************************************************
619 * Module entry point for art.
620 *****************************************************************************/
621 static int FindArt( vlc_object_t *p_this )
623 playlist_t *p_playlist = (playlist_t *)p_this;
624 input_item_t *p_item = (input_item_t *)(p_playlist->p_private);
625 lua_State *p_state = vlclua_meta_init( p_this, p_item );
627 int i_ret = vlclua_scripts_batch_execute( p_this, "luameta", &fetch_art, p_state, p_item );
628 lua_close( p_state );