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 <stdio.h> /*asprintf()*/
34 #include <vlc_input.h>
35 #include <vlc_playlist.h>
38 #include <vlc_strings.h>
39 #include <vlc_stream.h>
40 #include <vlc_charset.h>
42 #ifdef HAVE_SYS_STAT_H
43 # include <sys/stat.h>
46 #include <lua.h> /* Low level lua C API */
47 #include <lauxlib.h> /* Higher level C API */
48 #include <lualib.h> /* Lua libs */
50 /*****************************************************************************
52 *****************************************************************************/
53 static int FindArt( vlc_object_t * );
54 static int FindMeta( vlc_object_t *p_this );
56 /*****************************************************************************
58 *****************************************************************************/
61 set_shortname( N_( "Lua Meta" ) );
62 set_description( _("Fetch Artwork using lua scripts") );
63 set_capability( "meta fetcher", 10 );
64 set_callbacks( FindMeta, NULL );
66 set_capability( "art finder", 10 );
67 set_callbacks( FindArt, NULL );
70 /*****************************************************************************
72 *****************************************************************************/
73 static vlc_object_t * vlclua_get_this( lua_State *p_state )
75 vlc_object_t * p_this;
76 lua_getglobal( p_state, "vlc" );
77 lua_getfield( p_state, lua_gettop( p_state ), "private" );
78 p_this = (vlc_object_t*)lua_topointer( p_state, lua_gettop( p_state ) );
79 lua_pop( p_state, 2 );
83 static int vlclua_stream_new( lua_State *p_state )
85 vlc_object_t * p_this = vlclua_get_this( p_state );
86 int i = lua_gettop( p_state );
90 psz_url = lua_tostring( p_state, 1 );
91 lua_pop( p_state, i );
92 p_stream = stream_UrlNew( p_this, psz_url );
93 lua_pushlightuserdata( p_state, p_stream );
97 static int vlclua_stream_read( lua_State *p_state )
99 int i = lua_gettop( p_state );
105 p_stream = (stream_t *)lua_topointer( p_state, 1 );
106 n = lua_tonumber( p_state, 2 );
107 lua_pop( p_state, i );
108 p_read = malloc( n );
109 if( !p_read ) return 0;
110 i_read = stream_Read( p_stream, p_read, n );
111 lua_pushlstring( p_state, (const char *)p_read, i_read );
116 static int vlclua_stream_readline( lua_State *p_state )
118 int i = lua_gettop( p_state );
121 p_stream = (stream_t *)lua_topointer( p_state, 1 );
122 lua_pop( p_state, i );
123 char *psz_line = stream_ReadLine( p_stream );
126 lua_pushstring( p_state, psz_line );
131 lua_pushnil( p_state );
136 static int vlclua_stream_delete( lua_State *p_state )
138 int i = lua_gettop( p_state );
141 p_stream = (stream_t *)lua_topointer( p_state, 1 );
142 lua_pop( p_state, i );
143 stream_Delete( p_stream );
147 static int vlclua_decode_uri( lua_State *p_state )
149 int i = lua_gettop( p_state );
151 const char *psz_cstring = lua_tostring( p_state, 1 );
152 if( !psz_cstring ) return 0;
153 char *psz_string = strdup( psz_cstring );
154 lua_pop( p_state, i );
155 decode_URI( psz_string );
156 lua_pushstring( p_state, psz_string );
161 static int vlclua_resolve_xml_special_chars( lua_State *p_state )
163 int i = lua_gettop( p_state );
165 const char *psz_cstring = lua_tostring( p_state, 1 );
166 if( !psz_cstring ) return 0;
167 char *psz_string = strdup( psz_cstring );
168 lua_pop( p_state, i );
169 resolve_xml_special_chars( psz_string );
170 lua_pushstring( p_state, psz_string );
175 static int vlclua_msg_dbg( lua_State *p_state )
177 vlc_object_t *p_this = vlclua_get_this( p_state );
178 int i = lua_gettop( p_state );
180 const char *psz_cstring = lua_tostring( p_state, 1 );
181 if( !psz_cstring ) return 0;
182 msg_Dbg( p_this, "%s", psz_cstring );
185 static int vlclua_msg_warn( lua_State *p_state )
187 vlc_object_t *p_this = vlclua_get_this( p_state );
188 int i = lua_gettop( p_state );
190 const char *psz_cstring = lua_tostring( p_state, 1 );
191 if( !psz_cstring ) return 0;
192 msg_Warn( p_this, "%s", psz_cstring );
195 static int vlclua_msg_err( lua_State *p_state )
197 vlc_object_t *p_this = vlclua_get_this( p_state );
198 int i = lua_gettop( p_state );
200 const char *psz_cstring = lua_tostring( p_state, 1 );
201 if( !psz_cstring ) return 0;
202 msg_Err( p_this, "%s", psz_cstring );
205 static int vlclua_msg_info( lua_State *p_state )
207 vlc_object_t *p_this = vlclua_get_this( p_state );
208 int i = lua_gettop( p_state );
210 const char *psz_cstring = lua_tostring( p_state, 1 );
211 if( !psz_cstring ) return 0;
212 msg_Info( p_this, "%s", psz_cstring );
216 /* Functions to register */
217 static luaL_Reg p_reg[] =
219 { "stream_new", vlclua_stream_new },
220 { "stream_read", vlclua_stream_read },
221 { "stream_readline", vlclua_stream_readline },
222 { "stream_delete", vlclua_stream_delete },
223 { "decode_uri", vlclua_decode_uri },
224 { "resolve_xml_special_chars", vlclua_resolve_xml_special_chars },
225 { "msg_dbg", vlclua_msg_dbg },
226 { "msg_warn", vlclua_msg_warn },
227 { "msg_err", vlclua_msg_err },
228 { "msg_info", vlclua_msg_info },
231 /*****************************************************************************
233 *****************************************************************************/
234 static int file_select( const char *file )
236 int i = strlen( file );
237 return i > 4 && !strcmp( file+i-4, ".lua" );
240 static int file_compare( const char **a, const char **b )
242 return strcmp( *a, *b );
245 /*****************************************************************************
247 *****************************************************************************/
248 static lua_State * vlclua_meta_init( vlc_object_t *p_this, input_item_t * p_item )
250 lua_State * p_state = luaL_newstate();
253 msg_Err( p_this, "Could not create new Lua State" );
258 /* Load Lua libraries */
259 luaL_openlibs( p_state ); /* XXX: Don't open all the libs? */
261 luaL_register( p_state, "vlc", p_reg );
263 lua_pushlightuserdata( p_state, p_this );
264 lua_setfield( p_state, lua_gettop( p_state ) - 1, "private" );
266 psz_meta = input_item_GetName( p_item );
267 lua_pushstring( p_state, psz_meta );
268 lua_setfield( p_state, lua_gettop( p_state ) - 1, "name" );
271 psz_meta = input_item_GetArtist( p_item );
272 lua_pushstring( p_state, psz_meta );
273 lua_setfield( p_state, lua_gettop( p_state ) - 1, "artist" );
276 psz_meta = input_item_GetTitle( p_item ) ;
277 lua_pushstring( p_state, psz_meta );
278 lua_setfield( p_state, lua_gettop( p_state ) - 1, "title" );
281 psz_meta = input_item_GetAlbum( p_item );
282 lua_pushstring( p_state, psz_meta );
283 lua_setfield( p_state, lua_gettop( p_state ) - 1, "album" );
286 psz_meta = input_item_GetArtURL( p_item );
287 lua_pushstring( p_state, psz_meta );
288 lua_setfield( p_state, lua_gettop( p_state ) - 1, "arturl" );
290 /* XXX: all should be passed ( could use macro ) */
295 /*****************************************************************************
296 * Will execute func on all scripts in luadirname, and stop if func returns
298 *****************************************************************************/
299 static int vlclua_scripts_batch_execute( vlc_object_t *p_this,
300 const char * luadirname,
301 int (*func)(vlc_object_t *, const char *, lua_State *, void *),
305 int i_ret = VLC_EGENERIC;
308 char **ppsz_filelist = NULL;
309 char **ppsz_fileend = NULL;
312 char *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
315 if( asprintf( &ppsz_dir_list[0], "%s" DIR_SEP CONFIG_DIR DIR_SEP "%s", p_this->p_libvlc->psz_homedir,
319 # if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
321 const char *psz_vlcpath = config_GetDataDir();
322 if( asprintf( &ppsz_dir_list[1], "%s" DIR_SEP "%s", psz_vlcpath, luadirname ) < 0 )
325 if( asprintf( &ppsz_dir_list[2], "%s" DIR_SEP "share" DIR_SEP "%s", psz_vlcpath, luadirname ) < 0 )
330 # ifdef HAVE_SYS_STAT_H
331 struct stat stat_info;
332 if( ( utf8_stat( "share/luaplaylist", &stat_info ) == -1 )
333 || !S_ISDIR( stat_info.st_mode ) )
335 if( asprintf( &ppsz_dir_list[1],
336 DATA_PATH DIR_SEP "%s", luadirname ) < 0 )
342 if( asprintf( &ppsz_dir_list[1],
343 "share" DIR_SEP "%s", luadirname ) < 0 )
349 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
355 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
358 free( ppsz_filelist );
359 ppsz_filelist = NULL;
367 msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
368 dir = utf8_opendir( *ppsz_dir );
371 i_files = utf8_loaddir( dir, &ppsz_filelist, file_select, file_compare );
372 if( i_files < 1 ) continue;
373 ppsz_fileend = ppsz_filelist + i_files;
375 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
378 asprintf( &psz_filename, "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file );
379 msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
381 i_ret = func( p_this, psz_filename, p_state, user_data );
383 free( psz_filename );
385 if( i_ret == VLC_SUCCESS ) break;
387 if( i_ret == VLC_SUCCESS ) break;
392 for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
395 free( ppsz_filelist );
397 for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
400 if( dir ) closedir( dir );
405 /*****************************************************************************
406 * Meta data setters utility.
407 *****************************************************************************/
408 static inline void read_meta_data( vlc_object_t *p_this,
409 lua_State *p_state, int o, int t,
410 input_item_t *p_input )
412 const char *psz_value;
413 #define TRY_META( a, b ) \
414 lua_getfield( p_state, o, a ); \
415 if( lua_isstring( p_state, t ) ) \
417 psz_value = lua_tostring( p_state, t ); \
418 msg_Dbg( p_this, #b ": %s", psz_value ); \
419 input_item_Set ## b ( p_input, psz_value ); \
421 lua_pop( p_state, 1 ); /* pop a */
422 TRY_META( "title", Title );
423 TRY_META( "artist", Artist );
424 TRY_META( "genre", Genre );
425 TRY_META( "copyright", Copyright );
426 TRY_META( "album", Album );
427 TRY_META( "tracknum", TrackNum );
428 TRY_META( "description", Description );
429 TRY_META( "rating", Rating );
430 TRY_META( "date", Date );
431 TRY_META( "setting", Setting );
432 TRY_META( "url", URL );
433 TRY_META( "language", Language );
434 TRY_META( "nowplaying", NowPlaying );
435 TRY_META( "publisher", Publisher );
436 TRY_META( "encodedby", EncodedBy );
437 TRY_META( "arturl", ArtURL );
438 TRY_META( "trackid", TrackID );
441 static inline void read_custom_meta_data( vlc_object_t *p_this,
442 lua_State *p_state, int o, int t,
443 input_item_t *p_input )
445 lua_getfield( p_state, o, "meta" );
446 if( lua_istable( p_state, t ) )
448 lua_pushnil( p_state );
449 while( lua_next( p_state, t ) )
451 if( !lua_isstring( p_state, t+1 ) )
453 msg_Warn( p_this, "Custom meta data category name must be "
456 else if( !lua_istable( p_state, t+2 ) )
458 msg_Warn( p_this, "Custom meta data category contents "
463 const char *psz_meta_category = lua_tostring( p_state, t+1 );
464 msg_Dbg( p_this, "Found custom meta data category: %s",
466 lua_pushnil( p_state );
467 while( lua_next( p_state, t+2 ) )
469 if( !lua_isstring( p_state, t+3 ) )
471 msg_Warn( p_this, "Custom meta category item name "
472 "must be a string." );
474 else if( !lua_isstring( p_state, t+4 ) )
476 msg_Warn( p_this, "Custom meta category item value "
477 "must be a string." );
481 const char *psz_meta_name =
482 lua_tostring( p_state, t+3 );
483 const char *psz_meta_value =
484 lua_tostring( p_state, t+4 );
485 msg_Dbg( p_this, "Custom meta %s, %s: %s",
486 psz_meta_category, psz_meta_name,
488 input_ItemAddInfo( p_input, psz_meta_category,
489 psz_meta_name, psz_meta_value );
491 lua_pop( p_state, 1 ); /* pop item */
494 lua_pop( p_state, 1 ); /* pop category */
497 lua_pop( p_state, 1 ); /* pop "meta" */
500 /*****************************************************************************
501 * Called through lua_scripts_batch_execute to call 'fetch_art' on the script
502 * pointed by psz_filename.
503 *****************************************************************************/
504 static int fetch_art( vlc_object_t *p_this, const char * psz_filename,
505 lua_State * p_state, void * user_data )
507 int i_ret = VLC_EGENERIC;
508 input_item_t * p_input = user_data;
511 /* Ugly hack to delete previous versions of the fetchart()
513 lua_pushnil( p_state );
514 lua_setglobal( p_state, "fetch_art" );
516 /* Load and run the script(s) */
517 if( luaL_dofile( p_state, psz_filename ) )
519 msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
520 lua_tostring( p_state, lua_gettop( p_state ) ) );
521 lua_pop( p_state, 1 );
525 lua_getglobal( p_state, "fetch_art" );
527 if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
529 msg_Warn( p_this, "Error while runing script %s, "
530 "function fetch_art() not found", psz_filename );
531 lua_pop( p_state, 1 );
535 if( lua_pcall( p_state, 0, 1, 0 ) )
537 msg_Warn( p_this, "Error while runing script %s, "
538 "function fetch_art(): %s", psz_filename,
539 lua_tostring( p_state, lua_gettop( p_state ) ) );
540 lua_pop( p_state, 1 );
544 if((s = lua_gettop( p_state )))
546 const char * psz_value;
548 if( lua_isstring( p_state, s ) )
550 psz_value = lua_tostring( p_state, s );
551 if( psz_value && *psz_value != 0 )
553 msg_Dbg( p_this, "setting arturl: %s", psz_value );
554 input_item_SetArtURL ( p_input, psz_value );
560 msg_Err( p_this, "Lua playlist script %s: "
561 "didn't return a string", psz_filename );
566 msg_Err( p_this, "Script went completely foobar" );
572 /*****************************************************************************
573 * Called through lua_scripts_batch_execute to call 'fetch_meta' on the script
574 * pointed by psz_filename.
575 *****************************************************************************/
576 static int fetch_meta( vlc_object_t *p_this, const char * psz_filename,
577 lua_State * p_state, void * user_data )
579 input_item_t * p_input = user_data;
582 /* Ugly hack to delete previous versions of the fetchmeta()
584 lua_pushnil( p_state );
585 lua_setglobal( p_state, "fetch_meta" );
587 /* Load and run the script(s) */
588 if( luaL_dofile( p_state, psz_filename ) )
590 msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
591 lua_tostring( p_state, lua_gettop( p_state ) ) );
592 lua_pop( p_state, 1 );
596 lua_getglobal( p_state, "fetch_meta" );
598 if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
600 msg_Warn( p_this, "Error while runing script %s, "
601 "function fetch_meta() not found", psz_filename );
602 lua_pop( p_state, 1 );
606 if( lua_pcall( p_state, 0, 1, 0 ) )
608 msg_Warn( p_this, "Error while runing script %s, "
609 "function fetch_meta(): %s", psz_filename,
610 lua_tostring( p_state, lua_gettop( p_state ) ) );
611 lua_pop( p_state, 1 );
616 if((t = lua_gettop( p_state )))
618 if( lua_istable( p_state, t ) )
620 read_meta_data( p_this, p_state, t, t+1, p_input );
621 read_custom_meta_data( p_this, p_state, t, t+1, p_input );
625 msg_Err( p_this, "Lua playlist script %s: "
626 "didn't return a table", psz_filename );
631 msg_Err( p_this, "Script went completely foobar" );
634 /* We tell the batch thing to continue, hence all script
635 * will get the change to add its meta. This behaviour could
640 /*****************************************************************************
641 * Module entry point for meta.
642 *****************************************************************************/
643 static int FindMeta( vlc_object_t *p_this )
645 meta_engine_t *p_me = (meta_engine_t *)p_this;
646 input_item_t *p_item = p_me->p_item;
647 lua_State *p_state = vlclua_meta_init( p_this, p_item );
649 int i_ret = vlclua_scripts_batch_execute( p_this, "luameta", &fetch_meta, p_state, p_item );
650 lua_close( p_state );
654 /*****************************************************************************
655 * Module entry point for art.
656 *****************************************************************************/
657 static int FindArt( vlc_object_t *p_this )
659 playlist_t *p_playlist = (playlist_t *)p_this;
660 input_item_t *p_item = (input_item_t *)(p_playlist->p_private);
661 lua_State *p_state = vlclua_meta_init( p_this, p_item );
663 int i_ret = vlclua_scripts_batch_execute( p_this, "luameta", &fetch_art, p_state, p_item );
664 lua_close( p_state );