[PATCH 1/2] local-metadata: add series support



From: Lionel Landwerlin <lionel g landwerlin linux intel com>

Based on a patch for Tracker by Iain Holmes :

http://build.meego.com/package/view_file?file=0002-Tracker-extract-Parse-the-video-filename-to-obtain-e.patch&package=tracker&project=devel%3Acontentfw&srcmd5=c91b724488ee9ae476a9ce65755d8152

Signed-off-by: Lionel Landwerlin <lionel g landwerlin linux intel com>
---
 src/metadata/local-metadata/grl-local-metadata.c |  300 +++++++++++++++++++++-
 1 files changed, 295 insertions(+), 5 deletions(-)

diff --git a/src/metadata/local-metadata/grl-local-metadata.c b/src/metadata/local-metadata/grl-local-metadata.c
index 62737ba..c9a8e96 100644
--- a/src/metadata/local-metadata/grl-local-metadata.c
+++ b/src/metadata/local-metadata/grl-local-metadata.c
@@ -24,6 +24,9 @@
 #include "config.h"
 #endif
 
+#include <string.h>
+#include <stdlib.h>
+
 #include <grilo.h>
 #include <gio/gio.h>
 
@@ -42,6 +45,37 @@ GRL_LOG_DOMAIN_STATIC(local_metadata_log_domain);
 #define LICENSE     "LGPL"
 #define SITE        "http://www.igalia.com";
 
+/**/
+
+#define TV_REGEX \
+  "(?<showname>.*)\\.(?<season>(?:\\d{1,2})|(?:[sS]\\K\\d{1,2}))(?<episode>(?:\\d{2})|(?:[eE]\\K\\d{1,2}))\\.?(?<name>.*)?"
+#define MOVIE_REGEX "(?<name>.*)\\.?[\\(\\[](?<year>[12][90]\\d{2})[\\)\\]]"
+
+/**/
+
+enum {
+  VIDEO_TITLE    = 0x1,
+  VIDEO_SHOWNAME = 0x2,
+  VIDEO_DATE     = 0x4,
+  VIDEO_SEASON   = 0x8,
+  VIDEO_EPISODE  = 0x10,
+};
+
+const gchar *video_blacklisted_prefix[] = {
+  "tpz-", NULL
+};
+
+
+const char *video_blacklisted_words[] = {
+  "720p", "1080p",
+  "ws", "WS", "proper", "PROPER",
+  "repack", "real.repack",
+  "hdtv", "HDTV", "pdtv", "PDTV", "notv", "NOTV",
+  "dsr", "DSR", "DVDRip", "divx", "DIVX", "xvid", "Xvid",
+  NULL
+};
+
+/**/
 
 static GrlLocalMetadataSource *grl_local_metadata_source_new (void);
 
@@ -115,6 +149,191 @@ G_DEFINE_TYPE (GrlLocalMetadataSource,
                GRL_TYPE_METADATA_SOURCE);
 
 /* ======================= Utilities ==================== */
+
+static gchar *
+video_sanitise_string (const gchar *str)
+{
+  int    i;
+  gchar *line;
+
+  line = (gchar *) str;
+  for (i = 0; video_blacklisted_prefix[i]; i++) {
+    if (g_str_has_prefix (str, video_blacklisted_prefix[i])) {
+      int len = strlen (video_blacklisted_prefix[i]);
+
+      line = (gchar *) str + len;
+    }
+  }
+
+  for (i = 0; video_blacklisted_words[i]; i++) {
+    gchar *end;
+
+    end = strstr (line, video_blacklisted_words[i]);
+    if (end) {
+      return g_strndup (line, end - line);
+    }
+  }
+
+  return g_strdup (line);
+}
+
+/* tidies strings before we run them through the regexes */
+static gchar *
+video_uri_to_metadata (const gchar *uri)
+{
+  gchar *ext, *basename, *name, *whitelisted;
+
+  basename = g_path_get_basename (uri);
+  ext = strrchr (basename, '.');
+  if (ext) {
+    name = g_strndup (basename, ext - basename);
+    g_free (basename);
+  } else {
+    name = basename;
+  }
+
+  /* Replace _ <space> with . */
+  g_strdelimit (name, "_ ", '.');
+  whitelisted = video_sanitise_string (name);
+  g_free (name);
+
+  return whitelisted;
+}
+
+static void
+video_guess_values_from_uri (const gchar *uri,
+                             gchar      **title,
+                             gchar      **showname,
+                             GDateTime  **date,
+                             gint        *season,
+                             gint        *episode)
+{
+  gchar      *metadata;
+  GRegex     *regex;
+  GMatchInfo *info;
+
+  metadata = video_uri_to_metadata (uri);
+
+  regex = g_regex_new (MOVIE_REGEX, 0, 0, NULL);
+  g_regex_match (regex, metadata, 0, &info);
+
+  if (g_match_info_matches (info)) {
+    if (title) {
+      *title = g_match_info_fetch_named (info, "name");
+      /* Replace "." with <space> */
+      g_strdelimit (*title, ".", ' ');
+    }
+
+    if (date) {
+      gchar *year = g_match_info_fetch_named (info, "year");
+
+      *date = g_date_time_new_utc (atoi (year), 1, 1, 0, 0, 0.0);
+      g_free (year);
+    }
+
+    if (showname) {
+      *showname = NULL;
+    }
+
+    if (season) {
+      *season = 0;
+    }
+
+    if (episode) {
+      *episode = 0;
+    }
+
+    g_regex_unref (regex);
+    g_match_info_free (info);
+    g_free (metadata);
+
+    return;
+  }
+
+  g_regex_unref (regex);
+  g_match_info_free (info);
+
+  regex = g_regex_new (TV_REGEX, 0, 0, NULL);
+  g_regex_match (regex, metadata, 0, &info);
+
+  if (g_match_info_matches (info)) {
+    if (title) {
+      *title = g_match_info_fetch_named (info, "name");
+      g_strdelimit (*title, ".", ' ');
+    }
+
+    if (showname) {
+      *showname = g_match_info_fetch_named (info, "showname");
+      g_strdelimit (*showname, ".", ' ');
+    }
+
+    if (season) {
+      gchar *s = g_match_info_fetch_named (info, "season");
+      if (s) {
+        if (*s == 's' || *s == 'S') {
+          *season = atoi (s + 1);
+        } else {
+          *season = atoi (s);
+        }
+      } else {
+        *season = 0;
+      }
+
+      g_free (s);
+    }
+
+    if (episode) {
+      gchar *e = g_match_info_fetch_named (info, "episode");
+      if (e) {
+        if (*e == 'e' || *e == 'E') {
+          *episode = atoi (e + 1);
+        } else {
+          *episode = atoi (e);
+        }
+      } else {
+        *episode = 0;
+      }
+
+      g_free (e);
+    }
+
+    if (date) {
+      *date = NULL;
+    }
+
+    g_regex_unref (regex);
+    g_match_info_free (info);
+    g_free (metadata);
+
+    return;
+  }
+
+  g_regex_unref (regex);
+  g_match_info_free (info);
+
+  /* The filename doesn't look like a movie or a TV show, just use the
+     filename without extension as the title */
+  if (title) {
+    *title = g_strdelimit (metadata, ".", ' ');
+  }
+
+  if (showname) {
+    *showname = NULL;
+  }
+
+  if (date) {
+    *date = NULL;
+  }
+
+  if (season) {
+    *season = 0;
+  }
+
+  if (episode) {
+    *episode = 0;
+  }
+}
+
 static void
 got_file_info (GFile *file, GAsyncResult *result,
                GrlMetadataSourceResolveSpec *rs)
@@ -130,7 +349,7 @@ got_file_info (GFile *file, GAsyncResult *result,
     goto error;
 
   thumbnail_path =
-      g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
+    g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
 
 
   if (thumbnail_path) {
@@ -168,6 +387,73 @@ exit:
 }
 
 static void
+resolve_video (GrlMetadataSourceResolveSpec *rs)
+{
+  gchar *title, *showname;
+  GDateTime *date;
+  gint season, episode;
+  GrlData *data = GRL_DATA (rs->media);
+  guint flags = 0;
+
+  GRL_DEBUG ("%s", __FUNCTION__);
+
+  flags |= grl_data_key_is_known (data, GRL_METADATA_KEY_TITLE) ?
+    0 : VIDEO_TITLE;
+  flags |= grl_data_key_is_known (data, GRL_METADATA_KEY_SHOW) ?
+    0 : VIDEO_SHOWNAME;
+  flags |= grl_data_key_is_known (data, GRL_METADATA_KEY_DATE) ?
+    0 : VIDEO_DATE;
+  flags |= grl_data_key_is_known (data, GRL_METADATA_KEY_SEASON) ?
+    0 : VIDEO_SEASON;
+  flags |= grl_data_key_is_known (data, GRL_METADATA_KEY_EPISODE) ?
+    0 : VIDEO_EPISODE;
+
+  if (!flags)
+    return;
+
+  video_guess_values_from_uri (grl_media_get_url (rs->media),
+                               &title, &showname, &date,
+                               &season, &episode);
+
+  GRL_DEBUG ("\tfound title=%s/showname=%s/year=%i/season=%i/episode=%i",
+             title, showname,
+             date != NULL ? g_date_time_get_year (date) : 0,
+             season, episode);
+
+  /* As this is just a guess, don't erase already provided values. */
+  if (title) {
+    if (flags & VIDEO_TITLE) {
+      grl_data_set_string (data, GRL_METADATA_KEY_TITLE, title);
+    }
+    g_free (title);
+  }
+
+  if (showname) {
+    if (flags & VIDEO_SHOWNAME) {
+      grl_data_set_string (data, GRL_METADATA_KEY_SHOW, showname);
+    }
+    g_free (showname);
+  }
+
+  if (date) {
+    if (flags & VIDEO_DATE) {
+      gchar *str_date = g_date_time_format (date, "%F");
+      grl_data_set_string (data, GRL_METADATA_KEY_DATE, str_date);
+      g_free (str_date);
+    }
+    g_date_time_unref (date);
+  }
+
+  if (season && (flags & VIDEO_SEASON)) {
+    grl_data_set_int (data, GRL_METADATA_KEY_SEASON, season);
+  }
+
+  if (episode && (flags & VIDEO_EPISODE)) {
+    grl_data_set_int (data, GRL_METADATA_KEY_EPISODE, episode);
+  }
+}
+
+static void
 resolve_image (GrlMetadataSourceResolveSpec *rs)
 {
   GFile *file;
@@ -249,11 +535,11 @@ grl_local_metadata_source_may_resolve (GrlMetadataSource *source,
 
 static void
 grl_local_metadata_source_resolve (GrlMetadataSource *source,
-                                  GrlMetadataSourceResolveSpec *rs)
+                                   GrlMetadataSourceResolveSpec *rs)
 {
   GError *error = NULL;
 
-  GRL_DEBUG ("grl_local_metadata_source_resolve");
+  GRL_DEBUG ("%s", __FUNCTION__);
 
   if (!has_compatible_media_url (rs->media))
     error = g_error_new (GRL_CORE_ERROR, GRL_CORE_ERROR_RESOLVE_FAILED,
@@ -269,8 +555,12 @@ grl_local_metadata_source_resolve (GrlMetadataSource *source,
     return;
   }
 
-  if (GRL_IS_MEDIA_VIDEO (rs->media)
-      || GRL_IS_MEDIA_IMAGE (rs->media)) {
+  GRL_DEBUG ("\ttrying to resolve for: %s", grl_media_get_url (rs->media));
+
+  if (GRL_IS_MEDIA_VIDEO (rs->media)) {
+    resolve_video (rs);
+    resolve_image (rs);
+  } else if (GRL_IS_MEDIA_IMAGE (rs->media)) {
     resolve_image (rs);
   } else if (GRL_IS_MEDIA_AUDIO (rs->media)) {
     resolve_album_art (rs);
-- 
1.7.4.1



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