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 *****************************************************************************/
33 #include <vlc_input.h>
34 #include <vlc_playlist.h>
37 #include <vlc_strings.h>
38 #include <vlc_stream.h>
39 #include <vlc_charset.h>
41 #ifdef HAVE_SYS_STAT_H
42 # include <sys/stat.h>
45 #include <lua.h> /* Low level lua C API */
46 #include <lauxlib.h> /* Higher level C API */
47 #include <lualib.h> /* Lua libs */
49 /*****************************************************************************
51 *****************************************************************************/
52 static int FindArt( vlc_object_t * );
53 static int FindMeta( vlc_object_t *p_this );
55 /*****************************************************************************
57 *****************************************************************************/
60 set_shortname( N_( "Lua Meta" ) );
61 set_description( _("Fetch Artwork using lua scripts") );
62 set_capability( "meta fetcher", 10 );
63 set_callbacks( FindMeta, NULL );
65 set_capability( "art finder", 10 );
66 set_callbacks( FindArt, NULL );
69 /*****************************************************************************
71 *****************************************************************************/
72 static vlc_object_t * vlclua_get_this( lua_State *p_state )
74 vlc_object_t * p_this;
75 lua_getglobal( p_state, "vlc" );
76 lua_getfield( p_state, lua_gettop( p_state ), "private" );
77 p_this = (vlc_object_t*)lua_topointer( p_state, lua_gettop( p_state ) );
78 lua_pop( p_state, 2 );
82 static int vlclua_stream_new( lua_State *p_state )
84 vlc_object_t * p_this = vlclua_get_this( p_state );
85 int i = lua_gettop( p_state );
89 psz_url = lua_tostring( p_state, 1 );
90 lua_pop( p_state, i );
91 p_stream = stream_UrlNew( p_this, psz_url );
92 lua_pushlightuserdata( p_state, p_stream );
96 static int vlclua_stream_read( lua_State *p_state )
98 int i = lua_gettop( p_state );
104 p_stream = (stream_t *)lua_topointer( p_state, 1 );
105 n = lua_tonumber( p_state, 2 );
106 lua_pop( p_state, i );
107 p_read = malloc( n );
108 if( !p_read ) return 0;
109 i_read = stream_Read( p_stream, p_read, n );
110 lua_pushlstring( p_state, (const char *)p_read, i_read );
115 static int vlclua_stream_readline( lua_State *p_state )
117 int i = lua_gettop( p_state );
120 p_stream = (stream_t *)lua_topointer( p_state, 1 );
121 lua_pop( p_state, i );
122 char *psz_line = stream_ReadLine( p_stream );
125 lua_pushstring( p_state, psz_line );
130 lua_pushnil( p_state );
135 static int vlclua_stream_delete( lua_State *p_state )
137 int i = lua_gettop( p_state );
140 p_stream = (stream_t *)lua_topointer( p_state, 1 );
141 lua_pop( p_state, i );
142 stream_Delete( p_stream );
146 static int vlclua_decode_uri( lua_State *p_state )
148 int i = lua_gettop( p_state );
150 const char *psz_cstring = lua_tostring( p_state, 1 );
151 if( !psz_cstring ) return 0;
152 char *psz_string = strdup( psz_cstring );
153 lua_pop( p_state, i );
154 decode_URI( psz_string );
155 lua_pushstring( p_state, psz_string );
160 static int vlclua_resolve_xml_special_chars( lua_State *p_state )
162 int i = lua_gettop( p_state );
164 const char *psz_cstring = lua_tostring( p_state, 1 );
165 if( !psz_cstring ) return 0;
166 char *psz_string = strdup( psz_cstring );
167 lua_pop( p_state, i );
168 resolve_xml_special_chars( psz_string );
169 lua_pushstring( p_state, psz_string );
174 static int vlclua_msg_dbg( lua_State *p_state )
176 vlc_object_t *p_this = vlclua_get_this( p_state );
177 int i = lua_gettop( p_state );
179 const char *psz_cstring = lua_tostring( p_state, 1 );
180 if( !psz_cstring ) return 0;
181 msg_Dbg( p_this, "%s", psz_cstring );
184 static int vlclua_msg_warn( lua_State *p_state )
186 vlc_object_t *p_this = vlclua_get_this( p_state );
187 int i = lua_gettop( p_state );
189 const char *psz_cstring = lua_tostring( p_state, 1 );
190 if( !psz_cstring ) return 0;
191 msg_Warn( p_this, "%s", psz_cstring );
194 static int vlclua_msg_err( lua_State *p_state )
196 vlc_object_t *p_this = vlclua_get_this( p_state );
197 int i = lua_gettop( p_state );
199 const char *psz_cstring = lua_tostring( p_state, 1 );
200 if( !psz_cstring ) return 0;
201 msg_Err( p_this, "%s", psz_cstring );
204 static int vlclua_msg_info( lua_State *p_state )
206 vlc_object_t *p_this = vlclua_get_this( p_state );
207 int i = lua_gettop( p_state );
209 const char *psz_cstring = lua_tostring( p_state, 1 );
210 if( !psz_cstring ) return 0;
211 msg_Info( p_this, "%s", psz_cstring );
215 /* Functions to register */
216 static luaL_Reg p_reg[] =
218 { "stream_new", vlclua_stream_new },
219 { "stream_read", vlclua_stream_read },
220 { "stream_readline", vlclua_stream_readline },
221 { "stream_delete", vlclua_stream_delete },
222 { "decode_uri", vlclua_decode_uri },
223 { "resolve_xml_special_chars", vlclua_resolve_xml_special_chars },
224 { "msg_dbg", vlclua_msg_dbg },
225 { "msg_warn", vlclua_msg_warn },
226 { "msg_err", vlclua_msg_err },
227 { "msg_info", vlclua_msg_info },
230 /*****************************************************************************
232 *****************************************************************************/
233 static int file_select( const char *file )
235 int i = strlen( file );
236 return i > 4 && !strcmp( file+i-4, ".lua" );
239 static int file_compare( const char **a, const char **b )
241 return strcmp( *a, *b );
244 /*****************************************************************************
246 *****************************************************************************/
247 static lua_State * vlclua_meta_init( vlc_object_t *p_this, input_item_t * p_item )
249 lua_State * p_state = luaL_newstate();
252 msg_Err( p_this, "Could not create new Lua State" );
257 /* Load Lua libraries */
258 luaL_openlibs( p_state ); /* XXX: Don't open all the libs? */
260 luaL_register( p_state, "vlc", p_reg );
262 lua_pushlightuserdata( p_state, p_this );
263 lua_setfield( p_state, lua_gettop( p_state ) - 1, "private" );
265 psz_meta = input_item_GetName( p_item );
266 lua_pushstring( p_state, psz_meta );
267 lua_setfield( p_state, lua_gettop( p_state ) - 1, "name" );
270 psz_meta = input_item_GetArtist( p_item );
271 lua_pushstring( p_state, psz_meta );
272 lua_setfield( p_state, lua_gettop( p_state ) - 1, "artist" );
275 psz_meta = input_item_GetTitle( p_item ) ;
276 lua_pushstring( p_state, psz_meta );
277 lua_setfield( p_state, lua_gettop( p_state ) - 1, "title" );
280 psz_meta = input_item_GetAlbum( p_item );
281 lua_pushstring( p_state, psz_meta );
282 lua_setfield( p_state, lua_gettop( p_state ) - 1, "album" );
285 psz_meta = input_item_GetArtURL( p_item );
286 lua_pushstring( p_state, psz_meta );
287 lua_setfield( p_state, lua_gettop( p_state ) - 1, "arturl" );
289 /* XXX: all should be passed ( could use macro ) */
294 /*****************************************************************************
295 * Will execute func on all scripts in luadirname, and stop if func returns
297 *****************************************************************************/
298 static int vlclua_scripts_batch_execute( vlc_object_t *p_this,
299 const char * luadirname,
300 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
304 int i_ret = VLC_EGENERIC;
307 char **ppsz_filelist = NULL;
308 char **ppsz_fileend = NULL;
311 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
314 if( asprintf( &ppsz_dir_list[0], "%s" DIR_SEP CONFIG_DIR DIR_SEP "%s", p_this->p_libvlc->psz_homedir,
318 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
320 const char *psz_vlcpath = config_GetDataDir();
321 if( asprintf( &ppsz_dir_list[1], "%s" DIR_SEP "%s", psz_vlcpath, luadirname ) < 0 )
324 if( asprintf( &ppsz_dir_list[2], "%s" DIR_SEP "share" DIR_SEP "%s", psz_vlcpath, luadirname ) < 0 )
329 # ifdef HAVE_SYS_STAT_H
330 struct stat stat_info;
331 if( ( utf8_stat( "share/luaplaylist", &stat_info ) == -1 )
332 || !S_ISDIR( stat_info.st_mode ) )
334 if( asprintf( &ppsz_dir_list[1],
335 DATA_PATH DIR_SEP "%s", luadirname ) < 0 )
341 if( asprintf( &ppsz_dir_list[1],
342 "share" DIR_SEP "%s", luadirname ) < 0 )
348 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
354 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
357 free( ppsz_filelist );
358 ppsz_filelist = NULL;
366 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
367 dir = utf8_opendir( *ppsz_dir );
370 i_files = utf8_loaddir( dir, &ppsz_filelist, file_select, file_compare );
371 if( i_files < 1 ) continue;
372 ppsz_fileend = ppsz_filelist + i_files;
374 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
377 asprintf( &psz_filename, "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file );
378 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
380 i_ret = func( p_this, psz_filename, p_state, user_data );
382 free( psz_filename );
384 if( i_ret == VLC_SUCCESS ) break;
386 if( i_ret == VLC_SUCCESS ) break;
391 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
394 free( ppsz_filelist );
396 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
399 if( dir ) closedir( dir );
404 /*****************************************************************************
405 * Meta data setters utility.
406 *****************************************************************************/
407 static inline void read_meta_data( vlc_object_t *p_this,
408 lua_State *p_state, int o, int t,
409 input_item_t *p_input )
411 const char *psz_value;
412 #define TRY_META( a, b ) \
413 lua_getfield( p_state, o, a ); \
414 if( lua_isstring( p_state, t ) ) \
416 psz_value = lua_tostring( p_state, t ); \
417 msg_Dbg( p_this, #b ": %s", psz_value ); \
418 input_item_Set ## b ( p_input, psz_value ); \
420 lua_pop( p_state, 1 ); /* pop a */
421 TRY_META( "title", Title );
422 TRY_META( "artist", Artist );
423 TRY_META( "genre", Genre );
424 TRY_META( "copyright", Copyright );
425 TRY_META( "album", Album );
426 TRY_META( "tracknum", TrackNum );
427 TRY_META( "description", Description );
428 TRY_META( "rating", Rating );
429 TRY_META( "date", Date );
430 TRY_META( "setting", Setting );
431 TRY_META( "url", URL );
432 TRY_META( "language", Language );
433 TRY_META( "nowplaying", NowPlaying );
434 TRY_META( "publisher", Publisher );
435 TRY_META( "encodedby", EncodedBy );
436 TRY_META( "arturl", ArtURL );
437 TRY_META( "trackid", TrackID );
440 static inline void read_custom_meta_data( vlc_object_t *p_this,
441 lua_State *p_state, int o, int t,
442 input_item_t *p_input )
444 lua_getfield( p_state, o, "meta" );
445 if( lua_istable( p_state, t ) )
447 lua_pushnil( p_state );
448 while( lua_next( p_state, t ) )
450 if( !lua_isstring( p_state, t+1 ) )
452 msg_Warn( p_this, "Custom meta data category name must be "
455 else if( !lua_istable( p_state, t+2 ) )
457 msg_Warn( p_this, "Custom meta data category contents "
462 const char *psz_meta_category = lua_tostring( p_state, t+1 );
463 msg_Dbg( p_this, "Found custom meta data category: %s",
465 lua_pushnil( p_state );
466 while( lua_next( p_state, t+2 ) )
468 if( !lua_isstring( p_state, t+3 ) )
470 msg_Warn( p_this, "Custom meta category item name "
471 "must be a string." );
473 else if( !lua_isstring( p_state, t+4 ) )
475 msg_Warn( p_this, "Custom meta category item value "
476 "must be a string." );
480 const char *psz_meta_name =
481 lua_tostring( p_state, t+3 );
482 const char *psz_meta_value =
483 lua_tostring( p_state, t+4 );
484 msg_Dbg( p_this, "Custom meta %s, %s: %s",
485 psz_meta_category, psz_meta_name,
487 input_ItemAddInfo( p_input, psz_meta_category,
488 psz_meta_name, psz_meta_value );
490 lua_pop( p_state, 1 ); /* pop item */
493 lua_pop( p_state, 1 ); /* pop category */
496 lua_pop( p_state, 1 ); /* pop "meta" */
499 /*****************************************************************************
500 * Called through lua_scripts_batch_execute to call 'fetch_art' on the script
501 * pointed by psz_filename.
502 *****************************************************************************/
503 static int fetch_art( vlc_object_t *p_this, const char * psz_filename,
504 lua_State * p_state, void * user_data )
506 int i_ret = VLC_EGENERIC;
507 input_item_t * p_input = user_data;
510 /* Ugly hack to delete previous versions of the fetchart()
512 lua_pushnil( p_state );
513 lua_setglobal( p_state, "fetch_art" );
515 /* Load and run the script(s) */
516 if( luaL_dofile( p_state, psz_filename ) )
518 msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
519 lua_tostring( p_state, lua_gettop( p_state ) ) );
520 lua_pop( p_state, 1 );
524 lua_getglobal( p_state, "fetch_art" );
526 if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
528 msg_Warn( p_this, "Error while runing script %s, "
529 "function fetch_art() not found", psz_filename );
530 lua_pop( p_state, 1 );
534 if( lua_pcall( p_state, 0, 1, 0 ) )
536 msg_Warn( p_this, "Error while runing script %s, "
537 "function fetch_art(): %s", psz_filename,
538 lua_tostring( p_state, lua_gettop( p_state ) ) );
539 lua_pop( p_state, 1 );
543 if((s = lua_gettop( p_state )))
545 const char * psz_value;
547 if( lua_isstring( p_state, s ) )
549 psz_value = lua_tostring( p_state, s );
550 if( psz_value && *psz_value != 0 )
552 msg_Dbg( p_this, "setting arturl: %s", psz_value );
553 input_item_SetArtURL ( p_input, psz_value );
559 msg_Err( p_this, "Lua playlist script %s: "
560 "didn't return a string", psz_filename );
565 msg_Err( p_this, "Script went completely foobar" );
571 /*****************************************************************************
572 * Called through lua_scripts_batch_execute to call 'fetch_meta' on the script
573 * pointed by psz_filename.
574 *****************************************************************************/
575 static int fetch_meta( vlc_object_t *p_this, const char * psz_filename,
576 lua_State * p_state, void * user_data )
578 input_item_t * p_input = user_data;
581 /* Ugly hack to delete previous versions of the fetchmeta()
583 lua_pushnil( p_state );
584 lua_setglobal( p_state, "fetch_meta" );
586 /* Load and run the script(s) */
587 if( luaL_dofile( p_state, psz_filename ) )
589 msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
590 lua_tostring( p_state, lua_gettop( p_state ) ) );
591 lua_pop( p_state, 1 );
595 lua_getglobal( p_state, "fetch_meta" );
597 if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
599 msg_Warn( p_this, "Error while runing script %s, "
600 "function fetch_meta() not found", psz_filename );
601 lua_pop( p_state, 1 );
605 if( lua_pcall( p_state, 0, 1, 0 ) )
607 msg_Warn( p_this, "Error while runing script %s, "
608 "function fetch_meta(): %s", psz_filename,
609 lua_tostring( p_state, lua_gettop( p_state ) ) );
610 lua_pop( p_state, 1 );
615 if((t = lua_gettop( p_state )))
617 if( lua_istable( p_state, t ) )
619 read_meta_data( p_this, p_state, t, t+1, p_input );
620 read_custom_meta_data( p_this, p_state, t, t+1, p_input );
624 msg_Err( p_this, "Lua playlist script %s: "
625 "didn't return a table", psz_filename );
630 msg_Err( p_this, "Script went completely foobar" );
633 /* We tell the batch thing to continue, hence all script
634 * will get the change to add its meta. This behaviour could
639 /*****************************************************************************
640 * Module entry point for meta.
641 *****************************************************************************/
642 static int FindMeta( vlc_object_t *p_this )
644 meta_engine_t *p_me = (meta_engine_t *)p_this;
645 input_item_t *p_item = p_me->p_item;
646 lua_State *p_state = vlclua_meta_init( p_this, p_item );
648 int i_ret = vlclua_scripts_batch_execute( p_this, "luameta", &fetch_meta, p_state, p_item );
649 lua_close( p_state );
653 /*****************************************************************************
654 * Module entry point for art.
655 *****************************************************************************/
656 static int FindArt( vlc_object_t *p_this )
658 playlist_t *p_playlist = (playlist_t *)p_this;
659 input_item_t *p_item = (input_item_t *)(p_playlist->p_private);
660 lua_State *p_state = vlclua_meta_init( p_this, p_item );
662 int i_ret = vlclua_scripts_batch_execute( p_this, "luameta", &fetch_art, p_state, p_item );
663 lua_close( p_state );