[tracker] tracker-extract: Use date as album uri identifier



commit fb30a3f66b5cca5324fdc3b0aa129388c67be92d
Author: Marinus Schraal <mschraal src gnome org>
Date:   Thu Oct 27 23:05:19 2016 +0200

    tracker-extract: Use date as album uri identifier
    
    Use the album creation date as part of the album uri identifier if
    available. This should make the separation of similar named albums even
    better. Also port mp3 & gstreamer to use the resource helper functon for
    exctracting album disc data.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=773697

 src/libtracker-extract/tracker-resource-helpers.c |   65 +++++++++++++--------
 src/libtracker-extract/tracker-resource-helpers.h |    2 +-
 src/libtracker-sparql/tracker-uri.c               |   26 ++++++++-
 src/libtracker-sparql/tracker-utils.vala          |    1 +
 src/tracker-extract/tracker-extract-flac.c        |    3 +-
 src/tracker-extract/tracker-extract-gstreamer.c   |   52 +++++-----------
 src/tracker-extract/tracker-extract-libav.c       |   19 +++---
 src/tracker-extract/tracker-extract-mp3.c         |   57 +++---------------
 src/tracker-extract/tracker-extract-vorbis.c      |    3 +-
 9 files changed, 106 insertions(+), 122 deletions(-)
---
diff --git a/src/libtracker-extract/tracker-resource-helpers.c 
b/src/libtracker-extract/tracker-resource-helpers.c
index 3b49c72..cde1a8c 100644
--- a/src/libtracker-extract/tracker-resource-helpers.c
+++ b/src/libtracker-extract/tracker-resource-helpers.c
@@ -229,6 +229,7 @@ tracker_extract_new_location (const char *street_address,
  * @album_title: title of the album
  * @album_artist: (allow none): a #TrackerResource for the album artist, or %NULL
  * @disc_number: disc number of this disc (the first / only disc in a set should be 1, not 0)
+ * @date: (allow none):The release date, or %NULL
  *
  * Create new nmm:MusicAlbumDisc and nmm:MusicAlbum resources. The resources are
  * given fixed URIs based on @album_title and @disc_number, so they will be
@@ -246,47 +247,63 @@ tracker_extract_new_location (const char *street_address,
 TrackerResource *
 tracker_extract_new_music_album_disc (const char      *album_title,
                                       TrackerResource *album_artist,
-                                      int              disc_number)
+                                      int              disc_number,
+                                      const char      *date)
 {
-       gchar *album_uri, *disc_uri;
-       const gchar *album_artist_name;
+       GString *album_uri, *disc_uri;
+       const gchar *album_artist_name = NULL;
+       gchar *tmp_album_uri, *tmp_disc_uri;
 
        TrackerResource *album, *album_disc;
 
        g_return_val_if_fail (album_title != NULL, NULL);
 
-       album_artist_name = tracker_resource_get_first_string (album_artist,
-                                                              "nmm:artistName");
+       if (album_artist)
+               album_artist_name = tracker_resource_get_first_string (album_artist,
+                                                                      "nmm:artistName");
 
-       if (album_artist != NULL) {
-               album_uri = tracker_sparql_escape_uri_printf ("urn:album:%s:%s",
-                                                             album_title,
-                                                             album_artist_name);
-       } else {
-               album_uri = tracker_sparql_escape_uri_printf ("urn:album:%s", album_title);
-       }
-       album = tracker_resource_new (album_uri);
+       album_uri = g_string_new ("urn:album:");
+
+       g_string_append (album_uri, album_title);
+
+       if (album_artist_name)
+               g_string_append_printf (album_uri, ":%s", album_artist_name);
+
+       if (date)
+               g_string_append_printf (album_uri, ":%s", date);
+
+       tmp_album_uri = tracker_sparql_escape_uri (album_uri->str);
+       album = tracker_resource_new (tmp_album_uri);
 
        tracker_resource_set_uri (album, "rdf:type", "nmm:MusicAlbum");
        tracker_resource_set_string (album, "nmm:albumTitle", album_title);
 
-       if (album_artist != NULL) {
+       if (album_artist)
                tracker_resource_add_relation (album, "nmm:albumArtist", album_artist);
-               disc_uri = tracker_sparql_escape_uri_printf ("urn:album-disc:%s:%s:Disc%d",
-                                                            album_title,
-                                                            album_artist_name,
-                                                            disc_number);
-       } else {
-               disc_uri = tracker_sparql_escape_uri_printf ("urn:album-disc:%s:Disc%d", album_title, 
disc_number);
-       }
 
-       album_disc = tracker_resource_new (disc_uri);
+       disc_uri = g_string_new ("urn:album-disc:");
+
+       g_string_append (disc_uri, album_title);
+
+       if (album_artist_name)
+               g_string_append_printf (disc_uri, ":%s", album_artist_name);
+
+       if (date)
+               g_string_append_printf (disc_uri, ":%s", date);
+
+       g_string_append_printf (disc_uri, ":Disc%d", disc_number);
+
+       tmp_disc_uri = tracker_sparql_escape_uri (disc_uri->str);
+       album_disc = tracker_resource_new (tmp_disc_uri);
+
        tracker_resource_set_uri (album_disc, "rdf:type", "nmm:MusicAlbumDisc");
        tracker_resource_set_int (album_disc, "nmm:setNumber", disc_number > 0 ? disc_number : 1);
        tracker_resource_add_relation (album_disc, "nmm:albumDiscAlbum", album);
 
-       g_free (album_uri);
-       g_free (disc_uri);
+       g_free (tmp_album_uri);
+       g_free (tmp_disc_uri);
+       g_string_free (album_uri, TRUE);
+       g_string_free (disc_uri, TRUE);
 
        g_object_unref (album);
 
diff --git a/src/libtracker-extract/tracker-resource-helpers.h 
b/src/libtracker-extract/tracker-resource-helpers.h
index 2051e8e..dc5b0dc 100644
--- a/src/libtracker-extract/tracker-resource-helpers.h
+++ b/src/libtracker-extract/tracker-resource-helpers.h
@@ -33,7 +33,7 @@ TrackerResource *tracker_extract_new_artist (const char *name);
 TrackerResource *tracker_extract_new_contact (const char *fullname);
 TrackerResource *tracker_extract_new_equipment (const char *make, const char *model);
 TrackerResource *tracker_extract_new_location (const char *address, const char *state, const char *city, 
const char *country, const char *gps_altitude, const char *gps_latitude, const char *gps_longitude);
-TrackerResource *tracker_extract_new_music_album_disc (const char *album_title, TrackerResource 
*album_artist, int disc_number);
+TrackerResource *tracker_extract_new_music_album_disc (const char *album_title, TrackerResource 
*album_artist, int disc_number, const char *date);
 TrackerResource *tracker_extract_new_tag (const char *label);
 
 G_END_DECLS
diff --git a/src/libtracker-sparql/tracker-uri.c b/src/libtracker-sparql/tracker-uri.c
index 5e76354..dcbf579 100644
--- a/src/libtracker-sparql/tracker-uri.c
+++ b/src/libtracker-sparql/tracker-uri.c
@@ -53,6 +53,8 @@ tracker_sparql_escape_uri_vprintf (const gchar *format,
 gchar *
 tracker_sparql_escape_uri_printf  (const gchar* format,
                                    ...);
+gchar *
+tracker_sparql_escape_uri         (const gchar *uri);
 
 static const char *
 find_conversion (const char  *format,
@@ -165,7 +167,7 @@ find_conversion (const char  *format,
  *
  * The result is escaped using g_uri_escape_string().
  *
- * Returns: a newly-allocated string holding the result. The returned string
+ * Returns: (transfer-full): a newly-allocated string holding the result. The returned string
  * should be freed with g_free() when no longer needed.
  *
  * Since: 0.10
@@ -269,7 +271,7 @@ cleanup:
  *
  * Calls tracker_sparql_escape_uri_vprintf() with the @... supplied.
  *
- * Returns: a newly-allocated string holding the result.The returned string
+ * Returns: (transfer-full): a newly-allocated string holding the result.The returned string
  * should be freed with g_free() when no longer needed.
  *
  * Since: 0.10
@@ -287,3 +289,23 @@ tracker_sparql_escape_uri_printf (const gchar *format, ...)
        return result;
 }
 
+/**
+ * tracker_sparql_escape_uri:
+ * @uri: a string to be escaped, following the tracker sparql rules
+ *
+ * Calls tracker_sparql_escape_uri_printf().
+ *
+ * Returns: (transfer-full): a newly-allocated string holding the result. The returned string
+ * should be freed with g_free() when no longer needed.
+ *
+ * Since: 1.12
+ */
+gchar *
+tracker_sparql_escape_uri (const gchar *uri)
+{
+       gchar *result;
+
+       result = tracker_sparql_escape_uri_printf ("%s", uri);
+
+       return result;
+}
diff --git a/src/libtracker-sparql/tracker-utils.vala b/src/libtracker-sparql/tracker-utils.vala
index 7e144a9..32aac9a 100644
--- a/src/libtracker-sparql/tracker-utils.vala
+++ b/src/libtracker-sparql/tracker-utils.vala
@@ -33,6 +33,7 @@ namespace Tracker.Sparql {
        // Imported from tracker-uri.c
        public extern string escape_uri_vprintf (string format, va_list args);
        public extern string escape_uri_printf (string format, ...);
+       public extern string escape_uri (string uri);
 
        /**
         * tracker_sparql_escape_string:
diff --git a/src/tracker-extract/tracker-extract-flac.c b/src/tracker-extract/tracker-extract-flac.c
index 56092c6..c29b269 100644
--- a/src/tracker-extract/tracker-extract-flac.c
+++ b/src/tracker-extract/tracker-extract-flac.c
@@ -234,7 +234,8 @@ tracker_extract_get_metadata (TrackerExtractInfo *info)
 
                album_disc = tracker_extract_new_music_album_disc (fd.album,
                                                                   album_artist,
-                                                                  fd.discno ? atoi(fd.discno) : 1);
+                                                                  fd.discno ? atoi(fd.discno) : 1,
+                                                                  fd.date);
 
                album = tracker_resource_get_first_relation (album_disc, "nmm:albumDiscAlbum");
 
diff --git a/src/tracker-extract/tracker-extract-gstreamer.c b/src/tracker-extract/tracker-extract-gstreamer.c
index 74bbb91..daa712d 100644
--- a/src/tracker-extract/tracker-extract-gstreamer.c
+++ b/src/tracker-extract/tracker-extract-gstreamer.c
@@ -163,7 +163,8 @@ intern_artist (MetadataExtractor     *extractor,
        TrackerResource *artist;
        gchar *artist_uri;
 
-       g_return_val_if_fail (artist_name != NULL, NULL);
+       if (artist_name == NULL)
+               return NULL;
 
        artist_uri = tracker_sparql_escape_uri_printf ("urn:artist:%s", artist_name);
 
@@ -635,11 +636,12 @@ static TrackerResource *
 extractor_maybe_get_album_disc (MetadataExtractor *extractor,
                                 GstTagList        *tag_list)
 {
+       GstDateTime *datetime_temp = NULL;
        TrackerResource *album = NULL, *album_artist = NULL, *album_disc = NULL;
-       gchar *album_uri, *album_disc_uri;
        gchar *album_artist_name = NULL;
-       gchar *track_artist_temp = NULL;
+       gchar *album_datetime = NULL;
        gchar *album_title = NULL;
+       gchar *track_artist_temp = NULL;
        gboolean has_it;
        guint volume_number;
 
@@ -650,43 +652,19 @@ extractor_maybe_get_album_disc (MetadataExtractor *extractor,
 
        gst_tag_list_get_string (tag_list, GST_TAG_ALBUM_ARTIST, &album_artist_name);
        gst_tag_list_get_string (tag_list, GST_TAG_ARTIST, &track_artist_temp);
+       gst_tag_list_get_date_time (tag_list, GST_TAG_DATE_TIME, &datetime_temp);
 
-       if (album_artist_name != NULL) {
-               album_artist = intern_artist (extractor, album_artist_name);
-               album_uri = tracker_sparql_escape_uri_printf ("urn:album:%s:%s", album_title, 
album_artist_name);
-       } else {
-               album_uri = tracker_sparql_escape_uri_printf ("urn:album:%s", album_title);
-       }
-
-       album = tracker_resource_new (album_uri);
-       tracker_resource_set_uri (album, "rdf:type", "nmm:MusicAlbum");
-
-       /* FIXME: nmm:albumTitle is now deprecated, should use nie:title ??  */
-       tracker_resource_set_string (album, "nmm:albumTitle", album_title);
-
-       if (album_artist) {
-               tracker_resource_set_relation (album, "nmm:albumArtist", album_artist);
-       }
-
-       set_property_from_gst_tag (album, "nmm:albumTrackCount", tag_list, GST_TAG_TRACK_COUNT);
-
+       album_datetime = gst_date_time_to_iso8601_string (datetime_temp);
+       album_artist = intern_artist (extractor, album_artist_name);
        has_it = gst_tag_list_get_uint (tag_list, GST_TAG_ALBUM_VOLUME_NUMBER, &volume_number);
 
-       if (album_artist) {
-               album_disc_uri = tracker_sparql_escape_uri_printf ("urn:album-disc:%s:%s:Disc%d",
-                                                                  album_title, album_artist_name,
-                                                                  has_it ? volume_number : 1);
-       } else {
-               album_disc_uri = tracker_sparql_escape_uri_printf ("urn:album-disc:%s:Disc%d",
-                                                                  album_title,
-                                                                  has_it ? volume_number : 1);
-       }
-
-       album_disc = tracker_resource_new (album_disc_uri);
-       tracker_resource_set_uri (album_disc, "rdf:type", "nmm:MusicAlbumDisc");
-       tracker_resource_set_int64 (album_disc, "nmm:setNumber", has_it ? volume_number : 1);
-       tracker_resource_set_relation (album_disc, "nmm:albumDiscAlbum", album);
+       album_disc = tracker_extract_new_music_album_disc (album_title,
+                                                          album_artist,
+                                                          has_it ? volume_number : 1,
+                                                          album_datetime);
 
+       album = tracker_resource_get_first_relation (album_disc, "nmm:albumDiscAlbum");
+       set_property_from_gst_tag (album, "nmm:albumTrackCount", tag_list, GST_TAG_TRACK_COUNT);
        set_property_from_gst_tag (album, "nmm:albumGain", extractor->tagcache, GST_TAG_ALBUM_GAIN);
        set_property_from_gst_tag (album, "nmm:albumPeakGain", extractor->tagcache, GST_TAG_ALBUM_PEAK);
 
@@ -695,7 +673,9 @@ extractor_maybe_get_album_disc (MetadataExtractor *extractor,
        extractor->media_art_title = album_title;
 #endif
 
+       gst_date_time_unref (datetime_temp);
        g_free (album_artist_name);
+       g_free (album_datetime);
        g_free (track_artist_temp);
 
        return album_disc;
diff --git a/src/tracker-extract/tracker-extract-libav.c b/src/tracker-extract/tracker-extract-libav.c
index 71a6b94..b2055ba 100644
--- a/src/tracker-extract/tracker-extract-libav.c
+++ b/src/tracker-extract/tracker-extract-libav.c
@@ -41,6 +41,7 @@ tracker_extract_get_metadata (TrackerExtractInfo *info)
        GFile *file;
        TrackerResource *metadata;
        gchar *absolute_file_path;
+       gchar *content_created = NULL;
        gchar *uri;
        AVFormatContext *format = NULL;
        AVStream *audio_stream = NULL;
@@ -84,6 +85,13 @@ tracker_extract_get_metadata (TrackerExtractInfo *info)
 
        metadata = tracker_resource_new (NULL);
 
+       if ((tag = av_dict_get (format->metadata, "creation_time", NULL, 0))) {
+               content_created = tracker_date_guess (tag->value);
+               if (content_created) {
+                       tracker_resource_set_string (metadata, "nie:contentCreated", content_created);
+               }
+       }
+
        if (audio_stream) {
                if (audio_stream->codec->sample_rate > 0) {
                        tracker_resource_set_int64 (metadata, "nfo:sampleRate", 
audio_stream->codec->sample_rate);
@@ -191,7 +199,7 @@ tracker_extract_get_metadata (TrackerExtractInfo *info)
                                disc_number = atoi (tag->value);
                        }
 
-                       album_disc = tracker_extract_new_music_album_disc (album_title, album_artist, 
disc_number);
+                       album_disc = tracker_extract_new_music_album_disc (album_title, album_artist, 
disc_number, content_created);
 
                        tracker_resource_set_relation (metadata, "nmm:musicAlbumDisc", album_disc);
                        tracker_resource_set_relation (metadata, "nmm:musicAlbum", 
tracker_resource_get_first_relation (album_disc, "nmm:albumDiscAlbum"));
@@ -239,14 +247,6 @@ tracker_extract_get_metadata (TrackerExtractInfo *info)
                tracker_resource_set_string (metadata, "nie:copyright", tag->value);
        }
 
-       if ((tag = av_dict_get (format->metadata, "creation_time", NULL, 0))) {
-               gchar *content_created = tracker_date_guess (tag->value);
-               if (content_created) {
-                       tracker_resource_set_string (metadata, "nie:contentCreated", content_created);
-                       g_free (content_created);
-               }
-       }
-
        if ((tag = av_dict_get (format->metadata, "description", NULL, 0))) {
                tracker_resource_set_string (metadata, "nie:description", tag->value);
        }
@@ -265,6 +265,7 @@ tracker_extract_get_metadata (TrackerExtractInfo *info)
 
        tracker_guarantee_resource_title_from_file (metadata, "nie:title", title, uri, NULL);
 
+       g_free (content_created);
        g_free (uri);
 
        avformat_close_input (&format);
diff --git a/src/tracker-extract/tracker-extract-mp3.c b/src/tracker-extract/tracker-extract-mp3.c
index 501b175..da94512 100644
--- a/src/tracker-extract/tracker-extract-mp3.c
+++ b/src/tracker-extract/tracker-extract-mp3.c
@@ -2456,35 +2456,23 @@ tracker_extract_get_metadata (TrackerExtractInfo *info)
        }
 
        if (md.album_name) {
-               gchar *album_uri;
+               TrackerResource *album_disc = NULL;
 
-               if (md.album_artist_name) {
-                       album_uri = tracker_sparql_escape_uri_printf ("urn:album:%s:%s",
-                                                               md.album_name,
-                                                               md.album_artist_name);
-               } else {
-                       album_uri = tracker_sparql_escape_uri_printf ("urn:album:%s",
-                                                               md.album_name);
-               }
+               album_disc = tracker_extract_new_music_album_disc (md.album_name,
+                                                                  md.album_artist,
+                                                                  md.set_number > 0 ? md.set_number : 1,
+                                                                  md.recording_time);
 
-               md.album = tracker_resource_new (album_uri);
+               md.album = tracker_resource_get_first_relation (album_disc, "nmm:albumDiscAlbum");
 
-               tracker_resource_set_uri (md.album, "rdf:type", "nmm:MusicAlbum");
-               /* FIXME: nmm:albumTitle is now deprecated
-                * tracker_sparql_builder_predicate (preupdate, "nie:title");
-                */
-               tracker_resource_set_string (md.album, "nmm:albumTitle", md.album_name);
-
-               if (md.album_artist) {
-                       tracker_resource_set_relation (md.album, "nmm:albumArtist", md.album_artist);
-                       g_object_unref (md.album_artist);
-               }
+               tracker_resource_set_relation (main_resource, "nmm:musicAlbum", md.album);
+               tracker_resource_set_relation (main_resource, "nmm:musicAlbumDisc", album_disc);
 
                if (md.track_count > 0) {
                        tracker_resource_set_int (md.album, "nmm:albumTrackCount", md.track_count);
                }
 
-               g_free (album_uri);
+               g_object_unref (album_disc);
        }
 
        tracker_resource_add_uri (main_resource, "rdf:type", "nmm:MusicPiece");
@@ -2545,33 +2533,6 @@ tracker_extract_get_metadata (TrackerExtractInfo *info)
                tracker_resource_set_int (main_resource, "nmm:trackNumber", md.track_number);
        }
 
-       if (md.album) {
-               TrackerResource *album_disc;
-               gchar *album_disc_uri;
-
-               if (md.album_artist_name) {
-                       album_disc_uri = tracker_sparql_escape_uri_printf ("urn:album-disc:%s:%s:Disc%d",
-                                                                    md.album_name,
-                                                                    md.album_artist_name,
-                                                                    md.set_number > 0 ? md.set_number : 1);
-               } else {
-                       album_disc_uri = tracker_sparql_escape_uri_printf ("urn:album-disc:%s:Disc%d",
-                                                                    md.album_name,
-                                                                    md.set_number > 0 ? md.set_number : 1);
-               }
-
-               album_disc = tracker_resource_new (album_disc_uri);
-               tracker_resource_set_uri (album_disc, "rdf:type", "nmm:MusicAlbumDisc");
-               tracker_resource_set_int (album_disc, "nmm:setNumber", md.set_number > 0 ? md.set_number : 1);
-               tracker_resource_set_relation (album_disc, "nmm:albumDiscAlbum", md.album);
-
-               tracker_resource_set_relation (main_resource, "nmm:musicAlbumDisc", album_disc);
-
-               g_free (album_disc_uri);
-               g_object_unref (album_disc);
-               g_object_unref (md.album);
-       }
-
        /* Get mp3 stream info */
        parsed = mp3_parse (buffer, buffer_size, audio_offset, uri, main_resource, &md);
 
diff --git a/src/tracker-extract/tracker-extract-vorbis.c b/src/tracker-extract/tracker-extract-vorbis.c
index 47fb982..3b2d173 100644
--- a/src/tracker-extract/tracker-extract-vorbis.c
+++ b/src/tracker-extract/tracker-extract-vorbis.c
@@ -184,7 +184,8 @@ tracker_extract_get_metadata (TrackerExtractInfo *info)
 
                album_disc = tracker_extract_new_music_album_disc (vd.album,
                                                                   album_artist,
-                                                                  vd.disc_number ? atoi(vd.disc_number) : 1);
+                                                                  vd.disc_number ? atoi(vd.disc_number) : 1,
+                                                                  vd.date);
 
                g_clear_object (&album_artist);
 


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