]> git.sesse.net Git - vlc/blobdiff - modules/misc/lua/meta.c
lua_meta: factorize.
[vlc] / modules / misc / lua / meta.c
index def7c5e37bc866b3f5ff39766f902b6dc0652283..650fb0d98db1dd76651624949dc987346f1834cd 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * meta.c: Get meta/artwork using lua scripts
  *****************************************************************************
- * Copyright (C) 2007 the VideoLAN team
+ * Copyright (C) 2007-2008 the VideoLAN team
  * $Id$
  *
  * Authors: Antoine Cellerier <dionoea at videolan tod org>
 
 #include <vlc_common.h>
 #include <vlc_input.h>
-#include <vlc_playlist.h>
 #include <vlc_meta.h>
+#include <vlc_demux.h>
+#include <vlc_art_finder.h>
 #include <vlc_url.h>
 #include <vlc_strings.h>
 #include <vlc_stream.h>
-#include <vlc_charset.h>
 
 #include "vlc.h"
-
-
-/*****************************************************************************
- * Local prototypes
- *****************************************************************************/
-static int fetch_meta( vlc_object_t *p_this, const char * psz_filename,
-                       lua_State * L, void * user_data );
-static int fetch_art( vlc_object_t *p_this, const char * psz_filename,
-                      lua_State * L, void * user_data );
-static lua_State *vlclua_meta_init( vlc_object_t *p_this,
-                                    input_item_t * p_item );
-
-
-/*****************************************************************************
- * Lua function bridge
- *****************************************************************************/
-/* Functions to register */
-static luaL_Reg p_reg[] =
-{
-    /* TODO: make an object out of the stream stuff, so we can do something
-     * like:
-     *
-     * s = vlc.stream_new( "http://www.videolan.org" )
-     * page = s:read( 2^16 )
-     * s:delete()
-     *
-     * instead of (which could be problematic since we don't check if
-     * s is really a stream object):
-     *
-     * s = vlc.stream_new( "http://www.videolan.org" )
-     * page = vlc.stream_read( s, 2^16 )
-     * vlc.stream_delete( s )
-     */
-    { "stream_new", vlclua_stream_new },
-    { "stream_read", vlclua_stream_read },
-    { "stream_readline", vlclua_stream_readline },
-    { "stream_delete", vlclua_stream_delete },
-    { "decode_uri", vlclua_decode_uri },
-    { "resolve_xml_special_chars", vlclua_resolve_xml_special_chars },
-    { "msg_dbg", vlclua_msg_dbg },
-    { "msg_warn", vlclua_msg_warn },
-    { "msg_err", vlclua_msg_err },
-    { "msg_info", vlclua_msg_info },
-    { NULL, NULL }
-};
+#include "libs.h"
 
 /*****************************************************************************
  * Init lua
  *****************************************************************************/
-static lua_State * vlclua_meta_init( vlc_object_t *p_this, input_item_t * p_item )
+static const luaL_Reg p_reg[] = { { NULL, NULL } };
+
+static lua_State * init( vlc_object_t *p_this, input_item_t * p_item, const char *psz_filename )
 {
     lua_State * L = luaL_newstate();
     if( !L )
@@ -100,104 +58,113 @@ static lua_State * vlclua_meta_init( vlc_object_t *p_this, input_item_t * p_item
         msg_Err( p_this, "Could not create new Lua State" );
         return NULL;
     }
-    char *psz_meta;
+
+    vlclua_set_this( L, p_this );
 
     /* Load Lua libraries */
     luaL_openlibs( L ); /* XXX: Don't open all the libs? */
 
     luaL_register( L, "vlc", p_reg );
 
-    lua_pushlightuserdata( L, p_this );
-    lua_setfield( L, -2, "private" );
-
-    psz_meta = input_item_GetName( p_item );
-    lua_pushstring( L, psz_meta );
-    lua_setfield( L, -2, "name" );
-    free( psz_meta );
-
-    psz_meta = input_item_GetArtist( p_item );
-    lua_pushstring( L, psz_meta );
-    lua_setfield( L, -2, "artist" );
-    free( psz_meta );
-
-    psz_meta = input_item_GetTitle( p_item ) ;
-    lua_pushstring( L, psz_meta );
-    lua_setfield( L, -2, "title" );
-    free( psz_meta );
-
-    psz_meta = input_item_GetAlbum( p_item );
-    lua_pushstring( L, psz_meta );
-    lua_setfield( L, -2, "album" );
-    free( psz_meta );
-
-    psz_meta = input_item_GetArtURL( p_item );
-    lua_pushstring( L, psz_meta );
-    lua_setfield( L, -2, "arturl" );
-    free( psz_meta );
-    /* XXX: all should be passed ( could use macro ) */
+    luaopen_msg( L );
+    luaopen_stream( L );
+    luaopen_strings( L );
+    luaopen_variables( L );
+    luaopen_object( L );
+    luaopen_misc( L );
+    luaopen_xml( L );
+    luaopen_md5( L );
+    luaopen_input_item( L, p_item );
+
+    if( vlclua_add_modules_path( p_this, L, psz_filename ) )
+    {
+        msg_Warn( p_this, "Error while setting the module search path for %s",
+                  psz_filename );
+        lua_close( L );
+        return NULL;
+    }
 
     return L;
 }
 
 /*****************************************************************************
- * Called through lua_scripts_batch_execute to call 'fetch_art' on the script
- * pointed by psz_filename.
+ * Run a lua entry point function
  *****************************************************************************/
-static int fetch_art( vlc_object_t *p_this, const char * psz_filename,
-                      lua_State * L, void * user_data )
+static int run( vlc_object_t *p_this, const char * psz_filename,
+                lua_State * L, const char *luafunction )
 {
-    int i_ret = VLC_EGENERIC;
-    input_item_t * p_input = user_data;
-    int s;
-
     /* Ugly hack to delete previous versions of the fetchart()
-    * functions. */
+     * functions. */
     lua_pushnil( L );
-    lua_setglobal( L, "fetch_art" );
+    lua_setglobal( L, luafunction );
 
     /* Load and run the script(s) */
     if( luaL_dofile( L, psz_filename ) )
     {
         msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
-                  lua_tostring( L, lua_gettop( L ) ) );
-        lua_pop( L, 1 );
-        return VLC_EGENERIC;
+                 lua_tostring( L, lua_gettop( L ) ) );
+        goto error;
     }
 
-    lua_getglobal( L, "fetch_art" );
+    lua_getglobal( L, luafunction );
 
     if( !lua_isfunction( L, lua_gettop( L ) ) )
     {
-        msg_Warn( p_this, "Error while runing script %s, "
-                  "function fetch_art() not found", psz_filename );
-        lua_pop( L, 1 );
-        return VLC_EGENERIC;
+        msg_Warn( p_this, "Error while running script %s, "
+                 "function %s() not found", psz_filename, luafunction );
+        goto error;
     }
 
     if( lua_pcall( L, 0, 1, 0 ) )
     {
-        msg_Warn( p_this, "Error while runing script %s, "
-                  "function fetch_art(): %s", psz_filename,
-                  lua_tostring( L, lua_gettop( L ) ) );
-        lua_pop( L, 1 );
+        msg_Warn( p_this, "Error while running script %s, "
+                 "function %s(): %s", psz_filename, luafunction,
+                 lua_tostring( L, lua_gettop( L ) ) );
+        goto error;
+    }
+    return VLC_SUCCESS;
+
+error:
+    lua_pop( L, 1 );
+    return VLC_EGENERIC;
+}
+
+/*****************************************************************************
+ * Called through lua_scripts_batch_execute to call 'fetch_art' on the script
+ * pointed by psz_filename.
+ *****************************************************************************/
+static int fetch_art( vlc_object_t *p_this, const char * psz_filename,
+                      void * user_data )
+{
+    input_item_t * p_item = user_data;
+
+    lua_State *L = init( p_this, p_item, psz_filename );
+    if( !L )
         return VLC_EGENERIC;
+
+    int i_ret = run(p_this, psz_filename, L, "fetch_art");
+    if(i_ret != VLC_SUCCESS)
+    {
+        lua_close( L );
+        return i_ret;
     }
 
-    if((s = lua_gettop( L )))
+    if(lua_gettop( L ))
     {
         const char * psz_value;
 
-        if( lua_isstring( L, s ) )
+        if( lua_isstring( L, -1 ) )
         {
-            psz_value = lua_tostring( L, s );
+            psz_value = lua_tostring( L, -1 );
             if( psz_value && *psz_value != 0 )
             {
                 lua_Dbg( p_this, "setting arturl: %s", psz_value );
-                input_item_SetArtURL ( p_input, psz_value );
-                i_ret = VLC_SUCCESS;
+                input_item_SetArtURL ( p_item, psz_value );
+                lua_close( L );
+                return VLC_SUCCESS;
             }
         }
-        else if( !lua_isnil( L, s ) )
+        else if( !lua_isnoneornil( L, -1 ) )
         {
             msg_Err( p_this, "Lua art fetcher script %s: "
                  "didn't return a string", psz_filename );
@@ -208,87 +175,85 @@ static int fetch_art( vlc_object_t *p_this, const char * psz_filename,
         msg_Err( p_this, "Script went completely foobar" );
     }
 
-    return i_ret;
+    lua_close( L );
+    return VLC_EGENERIC;
+}
+
+/*****************************************************************************
+ * Called through lua_scripts_batch_execute to call 'read_meta' on the script
+ * pointed by psz_filename.
+ *****************************************************************************/
+static int read_meta( vlc_object_t *p_this, const char * psz_filename,
+                      void * user_data )
+{
+    input_item_t * p_item = user_data;
+    lua_State *L = init( p_this, p_item, psz_filename );
+    if( !L )
+        return VLC_EGENERIC;
+
+    int i_ret = run(p_this, psz_filename, L, "read_meta");
+    lua_close( L );
+
+    // Continue even if an error occured: all "meta reader" are always run.
+    return i_ret == VLC_SUCCESS ? VLC_EGENERIC : i_ret;
 }
 
+
 /*****************************************************************************
  * Called through lua_scripts_batch_execute to call 'fetch_meta' on the script
  * pointed by psz_filename.
  *****************************************************************************/
 static int fetch_meta( vlc_object_t *p_this, const char * psz_filename,
-                       lua_State * L, void * user_data )
+                       void * user_data )
 {
-    input_item_t * p_input = user_data;
+    input_item_t * p_item = user_data;
+    lua_State *L = init( p_this, p_item, psz_filename );
+    if( !L )
+        return VLC_EGENERIC;
 
-    /* In lua, setting a variable to nil is equivalent to deleting it */
-    lua_pushnil( L );
-    lua_setglobal( L, "fetch_meta" );
+    int ret = run(p_this, psz_filename, L, "fetch_meta");
+    lua_close( L );
 
-    /* Load and run the script(s) */
-    if( luaL_dofile( L, psz_filename ) )
-    {
-        msg_Warn( p_this, "Error loading script %s: %s", psz_filename,
-                  lua_tostring( L, lua_gettop( L ) ) );
-        lua_pop( L, 1 );
-        return VLC_EGENERIC;
-    }
+    return ret;
+}
 
-    lua_getglobal( L, "fetch_meta" );
+/*****************************************************************************
+ * Read meta.
+ *****************************************************************************/
 
-    if( !lua_isfunction( L, lua_gettop( L ) ) )
-    {
-        msg_Warn( p_this, "Error while runing script %s, "
-                  "function fetch_meta() not found", psz_filename );
-        lua_pop( L, 1 );
-        return VLC_EGENERIC;
-    }
+int ReadMeta( vlc_object_t *p_this )
+{
+    demux_meta_t *p_demux_meta = (demux_meta_t *)p_this;
+    input_item_t *p_item = p_demux_meta->p_item;
 
-    if( lua_pcall( L, 0, 1, 0 ) )
-    {
-        msg_Warn( p_this, "Error while runing script %s, "
-                  "function fetch_meta(): %s", psz_filename,
-                  lua_tostring( L, lua_gettop( L ) ) );
-        lua_pop( L, 1 );
-        return VLC_EGENERIC;
-    }
+    return vlclua_scripts_batch_execute( p_this, "meta"DIR_SEP"reader",
+                                         &read_meta, p_item );
+}
 
 
-    if( lua_gettop( L ) )
-    {
-        if( lua_istable( L, -1 ) )
-        {
-            /* ... item */
-            vlclua_read_meta_data( p_this, L, p_input );
-            vlclua_read_custom_meta_data( p_this, L, p_input );
-        }
-        else
-        {
-            msg_Err( p_this, "Lua playlist script %s: "
-                     "didn't return a table", psz_filename );
-        }
-    }
-    else
-    {
-        msg_Err( p_this, "Script went completely foobar" );
-    }
+/*****************************************************************************
+ * Read meta.
+ *****************************************************************************/
 
-    /* We tell the batch thing to continue, hence all script
-     * will get the change to add its meta. This behaviour could
-     * be changed. */
-    return VLC_EGENERIC;
+int FetchMeta( vlc_object_t *p_this )
+{
+    demux_meta_t *p_demux_meta = (demux_meta_t *)p_this;
+    input_item_t *p_item = p_demux_meta->p_item;
+
+    return vlclua_scripts_batch_execute( p_this, "meta"DIR_SEP"fetcher",
+                                         &fetch_meta, p_item );
 }
 
+
 /*****************************************************************************
  * Module entry point for art.
  *****************************************************************************/
 int FindArt( vlc_object_t *p_this )
 {
-    playlist_t *p_playlist = (playlist_t *)p_this;
-    input_item_t *p_item = (input_item_t *)(p_playlist->p_private);
-    lua_State *L = vlclua_meta_init( p_this, p_item );
+    art_finder_t *p_finder = (art_finder_t *)p_this;
+    input_item_t *p_item = p_finder->p_item;
 
-    int i_ret = vlclua_scripts_batch_execute( p_this, "meta", &fetch_art, L, p_item );
-    lua_close( L );
-    return i_ret;
+    return vlclua_scripts_batch_execute( p_this, "meta"DIR_SEP"art",
+                                         &fetch_art, p_item );
 }