[grilo-plugins] lua-factory: Add title parsing Lua source



commit ba849446abcc512a7cff07b3e975d409139bc898
Author: Victor Toso <me victortoso com>
Date:   Sun Mar 1 02:22:15 2015 +0100

    lua-factory: Add title parsing Lua source
    
    This source is an alternative to local-metadata and it uses lua patterns
    in order to get metadata from the title of the file.
    
    The test suite for video-title-parsing is the same for the
    local-metadata
    
    https://bugzilla.gnome.org/show_bug.cgi?id=741607

 src/lua-factory/sources/Makefile.am                |    3 +-
 .../sources/grl-video-title-parsing.lua            |  135 ++++++++++++++++++++
 tests/local-metadata/Makefile.am                   |    4 +-
 tests/local-metadata/test_local_metadata.c         |   33 ++++-
 4 files changed, 168 insertions(+), 7 deletions(-)
---
diff --git a/src/lua-factory/sources/Makefile.am b/src/lua-factory/sources/Makefile.am
index 91e7417..a44b27b 100644
--- a/src/lua-factory/sources/Makefile.am
+++ b/src/lua-factory/sources/Makefile.am
@@ -15,7 +15,8 @@ lua_sources_DATA =                                    \
        grl-musicbrainz.lua                             \
        grl-euronews.gresource.xml                      \
        grl-guardianvideos.gresource.xml                \
-       grl-radiofrance.gresource.xml
+       grl-radiofrance.gresource.xml \
+       grl-video-title-parsing.lua
 
 lua_sourcesdir = $(datadir)/$(LUA_FACTORY_SOURCE_LOCATION)
 
diff --git a/src/lua-factory/sources/grl-video-title-parsing.lua 
b/src/lua-factory/sources/grl-video-title-parsing.lua
new file mode 100644
index 0000000..3bf51cf
--- /dev/null
+++ b/src/lua-factory/sources/grl-video-title-parsing.lua
@@ -0,0 +1,135 @@
+--[[
+ * Copyright (C) 2014 Grilo Project
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+--]]
+
+---------------------------
+-- Source initialization --
+---------------------------
+
+source = {
+  id = "grl-video-title-parsing",
+  name = "video-title-parsing",
+  description = "Video title parsing",
+  supported_keys = { "episode-title", 'show', 'publication-date', 'season', 'episode', 'title' },
+  supported_media = 'video',
+  resolve_keys = {
+    ["type"] = "video",
+    required = { "title" },
+  },
+}
+
+blacklisted_words = {
+  "720p", "1080p", "x264",
+  "ws", "proper",
+  "real.repack", "repack",
+  "hdtv", "pdtv", "notv",
+  "dsr", "DVDRip", "divx", "xvid",
+}
+
+parsers = {
+  tvshow = {
+    "(.-)[sS](%d+)[%s.]*[eE][pP]?(%d+)(.+)",
+    "(.-)(%d+)[xX.](%d+)(.+)",
+  },
+  movies = {
+    "(.-)(19%d%d)",
+    "(.-)(20%d%d)",
+  }
+}
+
+function clean_title(title)
+  return title:gsub("^[%s%W]*(.-)[%s%W]*$", "%1"):gsub("%.", " ")
+end
+
+function clean_title_from_blacklist(title)
+  local s = title:lower()
+  local last_index
+
+  -- remove movie sufix
+  s = s:gsub("(.+)%..-$", "%1")
+
+  -- ignore everything after the first blacklisted word
+  last_index = #s
+  for i, word in ipairs (blacklisted_words) do
+    local index = s:find(word:lower())
+    if index and index < last_index then
+      last_index = index - 1
+    end
+  end
+  return title:sub(1, last_index)
+end
+
+function parse_as_movie(media)
+  local title, date
+  local str = clean_title_from_blacklist (media.title)
+  for i, parser in ipairs(parsers.movies) do
+    title, date = str:match(parser)
+    if title and date then
+      media.title = clean_title(title)
+      media.publication_date = date
+      return true
+    end
+  end
+  return false
+end
+
+function parse_as_episode(media)
+  local show, season, episode, title
+  for i, parser in ipairs(parsers.tvshow) do
+    show, season, episode, title = media.title:match(parser)
+    if show and season and episode and tonumber(season) < 50 then
+      media.show = clean_title(show)
+      media.season = season
+      media.episode = episode
+      media.episode_title = clean_title(clean_title_from_blacklist(title))
+      return true
+    end
+  end
+  return false
+end
+
+function grl_source_resolve()
+  local req
+  local media = {}
+
+  req = grl.get_media_keys()
+  if not req or not req.title then
+    grl.callback()
+    return
+  end
+
+  media.title = req.title
+
+  -- It is easier to identify a tv show due information
+  -- related to episode and season number
+  if parse_as_episode(media) then
+    grl.debug(req.title .. " is an EPISODE")
+    grl.callback(media, 0)
+    return
+  end
+
+  if parse_as_movie(media) then
+    grl.debug(req.title .. " is a MOVIE")
+    grl.callback(media, 0)
+    return
+  end
+
+  grl.debug("Fail to identify video: " .. req.title)
+  grl.callback()
+end
diff --git a/tests/local-metadata/Makefile.am b/tests/local-metadata/Makefile.am
index 49e6640..668673b 100644
--- a/tests/local-metadata/Makefile.am
+++ b/tests/local-metadata/Makefile.am
@@ -11,7 +11,9 @@ TEST_PROGS += \
    test_local_metadata
 
 test_local_metadata_defines =                                                       \
-   -DLOCAL_METADATA_PLUGIN_PATH=\""$(abs_top_builddir)/src/local-metadata/.libs/"\"
+   -DLOCAL_METADATA_PLUGIN_PATH=\""$(abs_top_builddir)/src/local-metadata/.libs/"\"    \
+   -DLUA_FACTORY_PLUGIN_PATH=\""$(abs_top_builddir)/src/lua-factory/.libs/"\"    \
+   -DLUA_SOURCES_PATH=\""$(abs_top_builddir)/src/lua-factory/sources/"\"
 
 test_local_metadata_SOURCES =  \
        test_local_metadata.c
diff --git a/tests/local-metadata/test_local_metadata.c b/tests/local-metadata/test_local_metadata.c
index 7dbf42c..6f8d329 100644
--- a/tests/local-metadata/test_local_metadata.c
+++ b/tests/local-metadata/test_local_metadata.c
@@ -23,6 +23,7 @@
 #include <grilo.h>
 
 #define LOCAL_METADATA_ID "grl-local-metadata"
+#define LUA_FACTORY_ID "grl-lua-factory"
 
 static void
 test_setup (void)
@@ -60,7 +61,7 @@ get_show_for_title (GrlSource  *source,
                                    GRL_METADATA_KEY_EPISODE_TITLE,
                                    NULL);
   options = grl_operation_options_new (NULL);
-  grl_operation_options_set_resolution_flags (options, GRL_RESOLVE_FULL);
+  grl_operation_options_set_resolution_flags (options, GRL_RESOLVE_NORMAL);
 
   grl_source_resolve_sync (source,
                           media,
@@ -83,7 +84,8 @@ get_show_for_title (GrlSource  *source,
 }
 
 static void
-test_episodes (void)
+test_episodes_by_source (const gchar *source_name,
+                         gboolean check_for_uris)
 {
   GrlRegistry *registry;
   GrlSource *source;
@@ -113,7 +115,6 @@ test_episodes (void)
     { NULL, "file:///home/hadess/.cache/totem/media/140127Mata-16x9%20(bug%20723166).mp4", NULL, NULL, 0, 0 }
   };
 
-
   registry = grl_registry_get_default ();
   source = grl_registry_lookup_source (registry, "grl-local-metadata");
   g_assert (source);
@@ -122,6 +123,10 @@ test_episodes (void)
     char *show, *new_title;
     int season, episode;
 
+    /* grl-video-title-parsing needs title */
+    if (episode_tests[i].title == NULL && check_for_uris == FALSE)
+      continue;
+
     show = get_show_for_title (source, episode_tests[i].title, episode_tests[i].url, &new_title, &season, 
&episode);
     g_assert_cmpstr (episode_tests[i].show, ==, show);
     if (show != NULL) {
@@ -135,6 +140,18 @@ test_episodes (void)
 }
 
 static void
+test_episodes (void)
+{
+  test_episodes_by_source (LOCAL_METADATA_ID, TRUE);
+}
+
+static void
+test_episodes_lua (void)
+{
+  test_episodes_by_source (LUA_FACTORY_ID, FALSE);
+}
+
+static void
 test_title_override (void)
 {
   GrlRegistry *registry;
@@ -188,8 +205,13 @@ test_title_override (void)
 int
 main(int argc, char **argv)
 {
-  g_setenv ("GRL_PLUGIN_PATH", LOCAL_METADATA_PLUGIN_PATH, TRUE);
-  g_setenv ("GRL_PLUGIN_LIST", LOCAL_METADATA_ID, TRUE);
+  gchar *plugins = g_strdup_printf ("%s:%s", LOCAL_METADATA_ID, LUA_FACTORY_ID);
+  gchar *plugins_path = g_strdup_printf ("%s:%s", LOCAL_METADATA_PLUGIN_PATH, LUA_FACTORY_PLUGIN_PATH);
+  g_setenv ("GRL_PLUGIN_PATH", plugins_path, TRUE);
+  g_setenv ("GRL_PLUGIN_LIST", plugins, TRUE);
+  g_setenv ("GRL_LUA_SOURCES_PATH", LUA_SOURCES_PATH, TRUE);
+  g_free (plugins);
+  g_free (plugins_path);
 
   grl_init (&argc, &argv);
   g_test_init (&argc, &argv, NULL);
@@ -202,6 +224,7 @@ main(int argc, char **argv)
 
   g_test_add_func ("/local-metadata/resolve/episodes", test_episodes);
   g_test_add_func ("/local-metadata/resolve/title-override", test_title_override);
+  g_test_add_func ("/lua-factory/video-title-parsing/resolve/episodes", test_episodes_lua);
 
   gint result = g_test_run ();
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]