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 *****************************************************************************/
31 #include <stdlib.h> /* malloc(), free() */
32 #include <stdio.h> /*asprintf()*/
35 #include <vlc_input.h>
36 #include <vlc_playlist.h>
39 #include <vlc_strings.h>
40 #include <vlc_stream.h>
41 #include <vlc_charset.h>
43 #ifdef HAVE_SYS_STAT_H
44 # include <sys/stat.h>
47 #include <lua.h> /* Low level lua C API */
48 #include <lauxlib.h> /* Higher level C API */
49 #include <lualib.h> /* Lua libs */
51 /*****************************************************************************
53 *****************************************************************************/
54 static int FindArt( vlc_object_t * );
55 static int FindMeta( vlc_object_t *p_this );
57 /*****************************************************************************
59 *****************************************************************************/
62 set_shortname( N_( "Lua Meta" ) );
63 set_description( _("Fetch Artwork using lua scripts") );
64 set_capability( "meta fetcher", 10 );
65 set_callbacks( FindMeta, NULL );
67 set_capability( "art finder", 10 );
68 set_callbacks( FindArt, NULL );
71 /*****************************************************************************
73 *****************************************************************************/
74 static vlc_object_t * vlclua_get_this( lua_State *p_state )
76 vlc_object_t * p_this;
77 lua_getglobal( p_state, "vlc" );
78 lua_getfield( p_state, lua_gettop( p_state ), "private" );
79 p_this = (vlc_object_t*)lua_topointer( p_state, lua_gettop( p_state ) );
80 lua_pop( p_state, 2 );
84 static int vlclua_stream_new( lua_State *p_state )
86 vlc_object_t * p_this = vlclua_get_this( p_state );
87 int i = lua_gettop( p_state );
91 psz_url = lua_tostring( p_state, 1 );
92 lua_pop( p_state, i );
93 p_stream = stream_UrlNew( p_this, psz_url );
94 lua_pushlightuserdata( p_state, p_stream );
98 static int vlclua_stream_read( lua_State *p_state )
100 int i = lua_gettop( p_state );
106 p_stream = (stream_t *)lua_topointer( p_state, 1 );
107 n = lua_tonumber( p_state, 2 );
108 lua_pop( p_state, i );
109 p_read = malloc( n );
110 if( !p_read ) return 0;
111 i_read = stream_Read( p_stream, p_read, n );
112 lua_pushlstring( p_state, (const char *)p_read, i_read );
117 static int vlclua_stream_readline( lua_State *p_state )
119 int i = lua_gettop( p_state );
122 p_stream = (stream_t *)lua_topointer( p_state, 1 );
123 lua_pop( p_state, i );
124 char *psz_line = stream_ReadLine( p_stream );
127 lua_pushstring( p_state, psz_line );
132 lua_pushnil( p_state );
137 static int vlclua_stream_delete( lua_State *p_state )
139 int i = lua_gettop( p_state );
142 p_stream = (stream_t *)lua_topointer( p_state, 1 );
143 lua_pop( p_state, i );
144 stream_Delete( p_stream );
148 static int vlclua_decode_uri( lua_State *p_state )
150 int i = lua_gettop( p_state );
152 const char *psz_cstring = lua_tostring( p_state, 1 );
153 if( !psz_cstring ) return 0;
154 char *psz_string = strdup( psz_cstring );
155 lua_pop( p_state, i );
156 decode_URI( psz_string );
157 lua_pushstring( p_state, psz_string );
162 static int vlclua_resolve_xml_special_chars( lua_State *p_state )
164 int i = lua_gettop( p_state );
166 const char *psz_cstring = lua_tostring( p_state, 1 );
167 if( !psz_cstring ) return 0;
168 char *psz_string = strdup( psz_cstring );
169 lua_pop( p_state, i );
170 resolve_xml_special_chars( psz_string );
171 lua_pushstring( p_state, psz_string );
176 static int vlclua_msg_dbg( lua_State *p_state )
178 vlc_object_t *p_this = vlclua_get_this( p_state );
179 int i = lua_gettop( p_state );
181 const char *psz_cstring = lua_tostring( p_state, 1 );
182 if( !psz_cstring ) return 0;
183 msg_Dbg( p_this, "%s", psz_cstring );
186 static int vlclua_msg_warn( lua_State *p_state )
188 vlc_object_t *p_this = vlclua_get_this( p_state );
189 int i = lua_gettop( p_state );
191 const char *psz_cstring = lua_tostring( p_state, 1 );
192 if( !psz_cstring ) return 0;
193 msg_Warn( p_this, "%s", psz_cstring );
196 static int vlclua_msg_err( lua_State *p_state )
198 vlc_object_t *p_this = vlclua_get_this( p_state );
199 int i = lua_gettop( p_state );
201 const char *psz_cstring = lua_tostring( p_state, 1 );
202 if( !psz_cstring ) return 0;
203 msg_Err( p_this, "%s", psz_cstring );
206 static int vlclua_msg_info( lua_State *p_state )
208 vlc_object_t *p_this = vlclua_get_this( p_state );
209 int i = lua_gettop( p_state );
211 const char *psz_cstring = lua_tostring( p_state, 1 );
212 if( !psz_cstring ) return 0;
213 msg_Info( p_this, "%s", psz_cstring );
217 /* Functions to register */
218 static luaL_Reg p_reg[] =
220 { "stream_new", vlclua_stream_new },
221 { "stream_read", vlclua_stream_read },
222 { "stream_readline", vlclua_stream_readline },
223 { "stream_delete", vlclua_stream_delete },
224 { "decode_uri", vlclua_decode_uri },
225 { "resolve_xml_special_chars", vlclua_resolve_xml_special_chars },
226 { "msg_dbg", vlclua_msg_dbg },
227 { "msg_warn", vlclua_msg_warn },
228 { "msg_err", vlclua_msg_err },
229 { "msg_info", vlclua_msg_info },
232 /*****************************************************************************
234 *****************************************************************************/
235 static int file_select( const char *file )
237 int i = strlen( file );
238 return i > 4 && !strcmp( file+i-4, ".lua" );
241 static int file_compare( const char **a, const char **b )
243 return strcmp( *a, *b );
246 /*****************************************************************************
248 *****************************************************************************/
249 static lua_State * vlclua_meta_init( vlc_object_t *p_this, input_item_t * p_item )
251 lua_State * p_state = luaL_newstate();
254 msg_Err( p_this, "Could not create new Lua State" );
259 /* Load Lua libraries */
260 luaL_openlibs( p_state ); /* XXX: Don't open all the libs? */
262 luaL_register( p_state, "vlc", p_reg );
264 lua_pushlightuserdata( p_state, p_this );
265 lua_setfield( p_state, lua_gettop( p_state ) - 1, "private" );
267 psz_meta = input_item_GetName( p_item );
268 lua_pushstring( p_state, psz_meta );
269 lua_setfield( p_state, lua_gettop( p_state ) - 1, "name" );
272 psz_meta = input_item_GetArtist( p_item );
273 lua_pushstring( p_state, psz_meta );
274 lua_setfield( p_state, lua_gettop( p_state ) - 1, "artist" );
277 psz_meta = input_item_GetTitle( p_item ) ;
278 lua_pushstring( p_state, psz_meta );
279 lua_setfield( p_state, lua_gettop( p_state ) - 1, "title" );
282 psz_meta = input_item_GetAlbum( p_item );
283 lua_pushstring( p_state, psz_meta );
284 lua_setfield( p_state, lua_gettop( p_state ) - 1, "album" );
287 psz_meta = input_item_GetArtURL( p_item );
288 lua_pushstring( p_state, psz_meta );
289 lua_setfield( p_state, lua_gettop( p_state ) - 1, "arturl" );
291 /* XXX: all should be passed ( could use macro ) */
296 /*****************************************************************************
297 * Will execute func on all scripts in luadirname, and stop if func returns
299 *****************************************************************************/
300 static int vlclua_scripts_batch_execute( vlc_object_t *p_this,
301 const char * luadirname,
302 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
306 int i_ret = VLC_EGENERIC;
309 char **ppsz_filelist = NULL;
310 char **ppsz_fileend = NULL;
313 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
316 if( asprintf( &ppsz_dir_list[0], "%s" DIR_SEP CONFIG_DIR DIR_SEP "%s", p_this->p_libvlc->psz_homedir,
320 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
322 const char *psz_vlcpath = config_GetDataDir();
323 if( asprintf( &ppsz_dir_list[1], "%s" DIR_SEP "%s", psz_vlcpath, luadirname ) < 0 )
326 if( asprintf( &ppsz_dir_list[2], "%s" DIR_SEP "share" DIR_SEP "%s", psz_vlcpath, luadirname ) < 0 )
331 # ifdef HAVE_SYS_STAT_H
332 struct stat stat_info;
333 if( ( utf8_stat( "share/luaplaylist", &stat_info ) == -1 )
334 || !S_ISDIR( stat_info.st_mode ) )
336 if( asprintf( &ppsz_dir_list[1],
337 DATA_PATH DIR_SEP "%s", luadirname ) < 0 )
343 if( asprintf( &ppsz_dir_list[1],
344 "share" DIR_SEP "%s", luadirname ) < 0 )
350 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
356 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
359 free( ppsz_filelist );
360 ppsz_filelist = NULL;
368 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
369 dir = utf8_opendir( *ppsz_dir );
372 i_files = utf8_loaddir( dir, &ppsz_filelist, file_select, file_compare );
373 if( i_files < 1 ) continue;
374 ppsz_fileend = ppsz_filelist + i_files;
376 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
379 asprintf( &psz_filename, "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file );
380 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
382 i_ret = func( p_this, psz_filename, p_state, user_data );
384 free( psz_filename );
386 if( i_ret == VLC_SUCCESS ) break;
388 if( i_ret == VLC_SUCCESS ) break;
393 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
396 free( ppsz_filelist );
398 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
401 if( dir ) closedir( dir );
406 /*****************************************************************************
407 * Meta data setters utility.
408 *****************************************************************************/
409 static inline void read_meta_data( vlc_object_t *p_this,
410 lua_State *p_state, int o, int t,
411 input_item_t *p_input )
413 const char *psz_value;
414 #define TRY_META( a, b ) \
415 lua_getfield( p_state, o, a ); \
416 if( lua_isstring( p_state, t ) ) \
418 psz_value = lua_tostring( p_state, t ); \
419 msg_Dbg( p_this, #b ": %s", psz_value ); \
420 input_item_Set ## b ( p_input, psz_value ); \
422 lua_pop( p_state, 1 ); /* pop a */
423 TRY_META( "title", Title );
424 TRY_META( "artist", Artist );
425 TRY_META( "genre", Genre );
426 TRY_META( "copyright", Copyright );
427 TRY_META( "album", Album );
428 TRY_META( "tracknum", TrackNum );
429 TRY_META( "description", Description );
430 TRY_META( "rating", Rating );
431 TRY_META( "date", Date );
432 TRY_META( "setting", Setting );
433 TRY_META( "url", URL );
434 TRY_META( "language", Language );
435 TRY_META( "nowplaying", NowPlaying );
436 TRY_META( "publisher", Publisher );
437 TRY_META( "encodedby", EncodedBy );
438 TRY_META( "arturl", ArtURL );
439 TRY_META( "trackid", TrackID );
442 static inline void read_custom_meta_data( vlc_object_t *p_this,
443 lua_State *p_state, int o, int t,
444 input_item_t *p_input )
446 lua_getfield( p_state, o, "meta" );
447 if( lua_istable( p_state, t ) )
449 lua_pushnil( p_state );
450 while( lua_next( p_state, t ) )
452 if( !lua_isstring( p_state, t+1 ) )
454 msg_Warn( p_this, "Custom meta data category name must be "
457 else if( !lua_istable( p_state, t+2 ) )
459 msg_Warn( p_this, "Custom meta data category contents "
464 const char *psz_meta_category = lua_tostring( p_state, t+1 );
465 msg_Dbg( p_this, "Found custom meta data category: %s",
467 lua_pushnil( p_state );
468 while( lua_next( p_state, t+2 ) )
470 if( !lua_isstring( p_state, t+3 ) )
472 msg_Warn( p_this, "Custom meta category item name "
473 "must be a string." );
475 else if( !lua_isstring( p_state, t+4 ) )
477 msg_Warn( p_this, "Custom meta category item value "
478 "must be a string." );
482 const char *psz_meta_name =
483 lua_tostring( p_state, t+3 );
484 const char *psz_meta_value =
485 lua_tostring( p_state, t+4 );
486 msg_Dbg( p_this, "Custom meta %s, %s: %s",
487 psz_meta_category, psz_meta_name,
489 input_ItemAddInfo( p_input, psz_meta_category,
490 psz_meta_name, psz_meta_value );
492 lua_pop( p_state, 1 ); /* pop item */
495 lua_pop( p_state, 1 ); /* pop category */
498 lua_pop( p_state, 1 ); /* pop "meta" */
501 /*****************************************************************************
502 * Called through lua_scripts_batch_execute to call 'fetch_art' on the script
503 * pointed by psz_filename.
504 *****************************************************************************/
505 static int fetch_art( vlc_object_t *p_this, const char * psz_filename,
506 lua_State * p_state, void * user_data )
508 int i_ret = VLC_EGENERIC;
509 input_item_t * p_input = user_data;
512 /* Ugly hack to delete previous versions of the fetchart()
514 lua_pushnil( p_state );
515 lua_setglobal( p_state, "fetch_art" );
517 /* Load and run the script(s) */
518 if( luaL_dofile( p_state, psz_filename ) )
520 msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
521 lua_tostring( p_state, lua_gettop( p_state ) ) );
522 lua_pop( p_state, 1 );
526 lua_getglobal( p_state, "fetch_art" );
528 if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
530 msg_Warn( p_this, "Error while runing script %s, "
531 "function fetch_art() not found", psz_filename );
532 lua_pop( p_state, 1 );
536 if( lua_pcall( p_state, 0, 1, 0 ) )
538 msg_Warn( p_this, "Error while runing script %s, "
539 "function fetch_art(): %s", psz_filename,
540 lua_tostring( p_state, lua_gettop( p_state ) ) );
541 lua_pop( p_state, 1 );
545 if((s = lua_gettop( p_state )))
547 const char * psz_value;
549 if( lua_isstring( p_state, s ) )
551 psz_value = lua_tostring( p_state, s );
552 if( psz_value && *psz_value != 0 )
554 msg_Dbg( p_this, "setting arturl: %s", psz_value );
555 input_item_SetArtURL ( p_input, psz_value );
561 msg_Err( p_this, "Lua playlist script %s: "
562 "didn't return a string", psz_filename );
567 msg_Err( p_this, "Script went completely foobar" );
573 /*****************************************************************************
574 * Called through lua_scripts_batch_execute to call 'fetch_meta' on the script
575 * pointed by psz_filename.
576 *****************************************************************************/
577 static int fetch_meta( vlc_object_t *p_this, const char * psz_filename,
578 lua_State * p_state, void * user_data )
580 input_item_t * p_input = user_data;
583 /* Ugly hack to delete previous versions of the fetchmeta()
585 lua_pushnil( p_state );
586 lua_setglobal( p_state, "fetch_meta" );
588 /* Load and run the script(s) */
589 if( luaL_dofile( p_state, psz_filename ) )
591 msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
592 lua_tostring( p_state, lua_gettop( p_state ) ) );
593 lua_pop( p_state, 1 );
597 lua_getglobal( p_state, "fetch_meta" );
599 if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
601 msg_Warn( p_this, "Error while runing script %s, "
602 "function fetch_meta() not found", psz_filename );
603 lua_pop( p_state, 1 );
607 if( lua_pcall( p_state, 0, 1, 0 ) )
609 msg_Warn( p_this, "Error while runing script %s, "
610 "function fetch_meta(): %s", psz_filename,
611 lua_tostring( p_state, lua_gettop( p_state ) ) );
612 lua_pop( p_state, 1 );
617 if((t = lua_gettop( p_state )))
619 if( lua_istable( p_state, t ) )
621 read_meta_data( p_this, p_state, t, t+1, p_input );
622 read_custom_meta_data( p_this, p_state, t, t+1, p_input );
626 msg_Err( p_this, "Lua playlist script %s: "
627 "didn't return a table", psz_filename );
632 msg_Err( p_this, "Script went completely foobar" );
635 /* We tell the batch thing to continue, hence all script
636 * will get the change to add its meta. This behaviour could
641 /*****************************************************************************
642 * Module entry point for meta.
643 *****************************************************************************/
644 static int FindMeta( vlc_object_t *p_this )
646 meta_engine_t *p_me = (meta_engine_t *)p_this;
647 input_item_t *p_item = p_me->p_item;
648 lua_State *p_state = vlclua_meta_init( p_this, p_item );
650 int i_ret = vlclua_scripts_batch_execute( p_this, "luameta", &fetch_meta, p_state, p_item );
651 lua_close( p_state );
655 /*****************************************************************************
656 * Module entry point for art.
657 *****************************************************************************/
658 static int FindArt( vlc_object_t *p_this )
660 playlist_t *p_playlist = (playlist_t *)p_this;
661 input_item_t *p_item = (input_item_t *)(p_playlist->p_private);
662 lua_State *p_state = vlclua_meta_init( p_this, p_item );
664 int i_ret = vlclua_scripts_batch_execute( p_this, "luameta", &fetch_art, p_state, p_item );
665 lua_close( p_state );