]> git.sesse.net Git - vlc/blob - modules/meta_engine/luameta.c
f3b68bc381dfb906c4ad9a9c1412c62bec1277d6
[vlc] / modules / meta_engine / luameta.c
1 /*****************************************************************************
2  * luameta.c: Get meta/artwork using lua scripts
3  *****************************************************************************
4  * Copyright (C) 2006 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 #include <stdio.h>                                       /*asprintf()*/
32
33 #include <vlc/vlc.h>
34 #include <vlc_input.h>
35 #include <vlc_playlist.h>
36 #include <vlc_meta.h>
37 #include <vlc_url.h>
38 #include <vlc_strings.h>
39 #include <vlc_stream.h>
40 #include <vlc_charset.h>
41
42 #ifdef HAVE_SYS_STAT_H
43 #   include <sys/stat.h>
44 #endif
45
46 #include <lua.h>        /* Low level lua C API */
47 #include <lauxlib.h>    /* Higher level C API */
48 #include <lualib.h>     /* Lua libs */
49
50 /*****************************************************************************
51  * Local prototypes
52  *****************************************************************************/
53 static int FindArt( vlc_object_t * );
54 static int FindMeta( vlc_object_t *p_this );
55
56 /*****************************************************************************
57  * Module descriptor
58  *****************************************************************************/
59
60 vlc_module_begin();
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 );
65     add_submodule();
66         set_capability( "art finder", 10 );
67         set_callbacks( FindArt, NULL );
68 vlc_module_end();
69
70 /*****************************************************************************
71  * Lua function bridge
72  *****************************************************************************/
73 static vlc_object_t * vlclua_get_this( lua_State *p_state )
74 {
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 );
80     return p_this;
81 }
82
83 static int vlclua_stream_new( lua_State *p_state )
84 {
85     vlc_object_t * p_this = vlclua_get_this( p_state );
86     int i = lua_gettop( p_state );
87     stream_t * p_stream;
88     const char * psz_url;
89     if( !i ) return 0;
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 );
94     return 1;
95 }
96
97 static int vlclua_stream_read( lua_State *p_state )
98 {
99     int i = lua_gettop( p_state );
100     stream_t * p_stream;
101     int n;
102     byte_t *p_read;
103     int i_read;
104     if( !i ) return 0;
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 );
112     free( p_read );
113     return 1;
114 }
115
116 static int vlclua_stream_readline( lua_State *p_state )
117 {
118     int i = lua_gettop( p_state );
119     stream_t * p_stream;
120     if( !i ) return 0;
121     p_stream = (stream_t *)lua_topointer( p_state, 1 );
122     lua_pop( p_state, i );
123     char *psz_line = stream_ReadLine( p_stream );
124     if( psz_line )
125     {
126         lua_pushstring( p_state, psz_line );
127         free( psz_line );
128     }
129     else
130     {
131         lua_pushnil( p_state );
132     }
133     return 1;
134 }
135
136 static int vlclua_stream_delete( lua_State *p_state )
137 {
138     int i = lua_gettop( p_state );
139     stream_t * p_stream;
140     if( !i ) return 0;
141     p_stream = (stream_t *)lua_topointer( p_state, 1 );
142     lua_pop( p_state, i );
143     stream_Delete( p_stream );
144     return 1;
145 }
146
147 static int vlclua_decode_uri( lua_State *p_state )
148 {
149     int i = lua_gettop( p_state );
150     if( !i ) return 0;
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 );
157     free( psz_string );
158     return 1;
159 }
160
161 static int vlclua_resolve_xml_special_chars( lua_State *p_state )
162 {
163     int i = lua_gettop( p_state );
164     if( !i ) return 0;
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 );
171     free( psz_string );
172     return 1;
173 }
174
175 static int vlclua_msg_dbg( lua_State *p_state )
176 {
177     vlc_object_t *p_this = vlclua_get_this( p_state );
178     int i = lua_gettop( p_state );
179     if( !i ) return 0;
180     const char *psz_cstring = lua_tostring( p_state, 1 );
181     if( !psz_cstring ) return 0;
182     msg_Dbg( p_this, "%s", psz_cstring );
183     return 0;
184 }
185 static int vlclua_msg_warn( lua_State *p_state )
186 {
187     vlc_object_t *p_this = vlclua_get_this( p_state );
188     int i = lua_gettop( p_state );
189     if( !i ) return 0;
190     const char *psz_cstring = lua_tostring( p_state, 1 );
191     if( !psz_cstring ) return 0;
192     msg_Warn( p_this, "%s", psz_cstring );
193     return 0;
194 }
195 static int vlclua_msg_err( lua_State *p_state )
196 {
197     vlc_object_t *p_this = vlclua_get_this( p_state );
198     int i = lua_gettop( p_state );
199     if( !i ) return 0;
200     const char *psz_cstring = lua_tostring( p_state, 1 );
201     if( !psz_cstring ) return 0;
202     msg_Err( p_this, "%s", psz_cstring );
203     return 0;
204 }
205 static int vlclua_msg_info( lua_State *p_state )
206 {
207     vlc_object_t *p_this = vlclua_get_this( p_state );
208     int i = lua_gettop( p_state );
209     if( !i ) return 0;
210     const char *psz_cstring = lua_tostring( p_state, 1 );
211     if( !psz_cstring ) return 0;
212     msg_Info( p_this, "%s", psz_cstring );
213     return 0;
214 }
215
216 /* Functions to register */
217 static luaL_Reg p_reg[] =
218 {
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 },
229     { NULL, NULL }
230 };
231 /*****************************************************************************
232  *
233  *****************************************************************************/
234 static int file_select( const char *file )
235 {
236     int i = strlen( file );
237     return i > 4 && !strcmp( file+i-4, ".lua" );
238 }
239
240 static int file_compare( const char **a, const char **b )
241 {
242     return strcmp( *a, *b );
243 }
244
245 /*****************************************************************************
246  * Init lua
247  *****************************************************************************/
248 static lua_State * vlclua_meta_init( vlc_object_t *p_this, input_item_t * p_item )
249 {
250     lua_State * p_state = luaL_newstate();
251     if( !p_state )
252     {
253         msg_Err( p_this, "Could not create new Lua State" );
254         return NULL;
255     }
256     char *psz_meta;
257
258     /* Load Lua libraries */
259     luaL_openlibs( p_state ); /* XXX: Don't open all the libs? */
260     
261     luaL_register( p_state, "vlc", p_reg );
262     
263     lua_pushlightuserdata( p_state, p_this );
264     lua_setfield( p_state, lua_gettop( p_state ) - 1, "private" );
265    
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" );
269     free( psz_meta );
270    
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" );
274     free( psz_meta );
275    
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" );
279     free( psz_meta );
280    
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" );
284     free( psz_meta );
285
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" );
289     free( psz_meta );
290     /* XXX: all should be passed ( could use macro ) */
291
292     return p_state;
293 }
294
295 /*****************************************************************************
296  * Will execute func on all scripts in luadirname, and stop if func returns
297  * success.
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 *),
302                                          lua_State * p_state,
303                                          void * user_data)
304 {
305     int i_ret = VLC_EGENERIC;
306
307     DIR   *dir           = NULL;
308     char **ppsz_filelist = NULL;
309     char **ppsz_fileend  = NULL;
310     char **ppsz_file;
311
312     char  *ppsz_dir_list[] = { NULL, NULL, NULL, NULL };
313     char **ppsz_dir;
314
315     if( asprintf( &ppsz_dir_list[0], "%s" DIR_SEP CONFIG_DIR DIR_SEP "%s", p_this->p_libvlc->psz_homedir,
316             luadirname ) < 0 )
317         return VLC_ENOMEM;
318
319 #   if defined(__APPLE__) || defined(SYS_BEOS) || defined(WIN32)
320     {
321         const char *psz_vlcpath = config_GetDataDir();
322         if( asprintf( &ppsz_dir_list[1], "%s" DIR_SEP "%s", psz_vlcpath, luadirname )  < 0 )
323             return VLC_ENOMEM;
324
325         if( asprintf( &ppsz_dir_list[2], "%s" DIR_SEP "share" DIR_SEP "%s", psz_vlcpath, luadirname )  < 0 )
326             return VLC_ENOMEM;
327     }
328 #   else
329     {
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 ) )
334         {
335             if( asprintf( &ppsz_dir_list[1], 
336                           DATA_PATH DIR_SEP "%s", luadirname ) < 0 )
337                 return VLC_ENOMEM;
338         }
339         else
340 #   endif
341         {
342             if( asprintf( &ppsz_dir_list[1], 
343                           "share" DIR_SEP "%s", luadirname ) < 0 )
344                 return VLC_ENOMEM;
345         }
346     }
347 #   endif
348
349     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
350     {
351         int i_files;
352
353         if( ppsz_filelist )
354         {
355             for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
356                  ppsz_file++ )
357                 free( *ppsz_file );
358             free( ppsz_filelist );
359             ppsz_filelist = NULL;
360         }
361
362         if( dir )
363         {
364             closedir( dir );
365         }
366
367         msg_Dbg( p_this, "Trying Lua scripts in %s", *ppsz_dir );
368         dir = utf8_opendir( *ppsz_dir );
369
370         if( !dir ) continue;
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;
374
375         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend; ppsz_file++ )
376         {
377             char  *psz_filename;
378             asprintf( &psz_filename, "%s" DIR_SEP "%s", *ppsz_dir, *ppsz_file );
379             msg_Dbg( p_this, "Trying Lua playlist script %s", psz_filename );
380             
381             i_ret = func( p_this, psz_filename, p_state, user_data );
382             
383             free( psz_filename );
384
385             if( i_ret == VLC_SUCCESS ) break;
386         }
387         if( i_ret == VLC_SUCCESS ) break;
388     }
389
390     if( ppsz_filelist )
391     {
392         for( ppsz_file = ppsz_filelist; ppsz_file < ppsz_fileend;
393              ppsz_file++ )
394             free( *ppsz_file );
395         free( ppsz_filelist );
396     }
397     for( ppsz_dir = ppsz_dir_list; *ppsz_dir; ppsz_dir++ )
398         free( *ppsz_dir );
399
400     if( dir ) closedir( dir );
401
402     return i_ret;
403 }
404
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 )
411 {
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 ) )                        \
416     {                                                       \
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 );   \
420     }                                                       \
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 );
439 }
440
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 )
444 {
445     lua_getfield( p_state, o, "meta" );
446     if( lua_istable( p_state, t ) )
447     {
448         lua_pushnil( p_state );
449         while( lua_next( p_state, t ) )
450         {
451             if( !lua_isstring( p_state, t+1 ) )
452             {
453                 msg_Warn( p_this, "Custom meta data category name must be "
454                                    "a string" );
455             }
456             else if( !lua_istable( p_state, t+2 ) )
457             {
458                 msg_Warn( p_this, "Custom meta data category contents "
459                                    "must be a table" );
460             }
461             else
462             {
463                 const char *psz_meta_category = lua_tostring( p_state, t+1 );
464                 msg_Dbg( p_this, "Found custom meta data category: %s",
465                          psz_meta_category );
466                 lua_pushnil( p_state );
467                 while( lua_next( p_state, t+2 ) )
468                 {
469                     if( !lua_isstring( p_state, t+3 ) )
470                     {
471                         msg_Warn( p_this, "Custom meta category item name "
472                                            "must be a string." );
473                     }
474                     else if( !lua_isstring( p_state, t+4 ) )
475                     {
476                         msg_Warn( p_this, "Custom meta category item value "
477                                            "must be a string." );
478                     }
479                     else
480                     {
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,
487                                  psz_meta_value );
488                         input_ItemAddInfo( p_input, psz_meta_category,
489                                            psz_meta_name, psz_meta_value );
490                     }
491                     lua_pop( p_state, 1 ); /* pop item */
492                 }
493             }
494             lua_pop( p_state, 1 ); /* pop category */
495         }
496     }
497     lua_pop( p_state, 1 ); /* pop "meta" */
498 }
499
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 )
506 {
507     int i_ret = VLC_EGENERIC;
508     input_item_t * p_input = user_data;
509     int s;
510
511     /* Ugly hack to delete previous versions of the fetchart()
512     * functions. */
513     lua_pushnil( p_state );
514     lua_setglobal( p_state, "fetch_art" );
515     
516     /* Load and run the script(s) */
517     if( luaL_dofile( p_state, psz_filename ) )
518     {
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 );
522         return VLC_EGENERIC;
523     }
524
525     lua_getglobal( p_state, "fetch_art" );
526
527     if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
528     {
529         msg_Warn( p_this, "Error while runing script %s, "
530                   "function fetch_art() not found", psz_filename );
531         lua_pop( p_state, 1 );
532         return VLC_EGENERIC;
533     }
534
535     if( lua_pcall( p_state, 0, 1, 0 ) )
536     {
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 );
541         return VLC_EGENERIC;
542     }
543
544     if((s = lua_gettop( p_state )))
545     {
546         const char * psz_value;
547
548         if( lua_isstring( p_state, s ) )
549         {
550             psz_value = lua_tostring( p_state, s );
551             if( psz_value && *psz_value != 0 )
552             {
553                 msg_Dbg( p_this, "setting arturl: %s", psz_value );
554                 input_item_SetArtURL ( p_input, psz_value );
555                 i_ret = VLC_SUCCESS;
556             }
557         }
558         else
559         {
560             msg_Err( p_this, "Lua playlist script %s: "
561                  "didn't return a string", psz_filename );
562         }
563     }
564     else
565     {
566         msg_Err( p_this, "Script went completely foobar" );
567     }
568
569     return i_ret;
570 }
571
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 )
578 {
579     input_item_t * p_input = user_data;
580     int t;
581
582     /* Ugly hack to delete previous versions of the fetchmeta()
583     * functions. */
584     lua_pushnil( p_state );
585     lua_setglobal( p_state, "fetch_meta" );
586     
587     /* Load and run the script(s) */
588     if( luaL_dofile( p_state, psz_filename ) )
589     {
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 );
593         return VLC_EGENERIC;
594     }
595     
596     lua_getglobal( p_state, "fetch_meta" );
597     
598     if( !lua_isfunction( p_state, lua_gettop( p_state ) ) )
599     {
600         msg_Warn( p_this, "Error while runing script %s, "
601                   "function fetch_meta() not found", psz_filename );
602         lua_pop( p_state, 1 );
603         return VLC_EGENERIC;
604     }
605     
606     if( lua_pcall( p_state, 0, 1, 0 ) )
607     {
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 );
612         return VLC_EGENERIC;
613     }
614     
615
616     if((t = lua_gettop( p_state )))
617     {
618         if( lua_istable( p_state, t ) )
619         {
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 );
622         }
623         else
624         {
625             msg_Err( p_this, "Lua playlist script %s: "
626                  "didn't return a table", psz_filename );
627         }
628     }
629     else
630     {
631         msg_Err( p_this, "Script went completely foobar" );
632     }
633
634     /* We tell the batch thing to continue, hence all script
635      * will get the change to add its meta. This behaviour could
636      * be changed. */
637     return VLC_EGENERIC;
638 }
639
640 /*****************************************************************************
641  * Module entry point for meta.
642  *****************************************************************************/
643 static int FindMeta( vlc_object_t *p_this )
644 {
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 );
648     
649     int i_ret = vlclua_scripts_batch_execute( p_this, "luameta", &fetch_meta, p_state, p_item );
650     lua_close( p_state );
651     return i_ret;
652 }
653
654 /*****************************************************************************
655  * Module entry point for art.
656  *****************************************************************************/
657 static int FindArt( vlc_object_t *p_this )
658 {
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 );
662     
663     int i_ret = vlclua_scripts_batch_execute( p_this, "luameta", &fetch_art, p_state, p_item );
664     lua_close( p_state );
665     return i_ret;
666 }
667