[totem-pl-parser] Add support for Last.fm's new XSPF extensions



commit c1e5c2f15f4b0c523743cfeae0c7e636d9d468d1
Author: Bastien Nocera <hadess hadess net>
Date:   Mon Aug 2 14:22:56 2010 +0100

    Add support for Last.fm's new XSPF extensions
    
    Support the TOTEM_PL_PARSER_FIELD_ID and
    TOTEM_PL_PARSER_FIELD_DOWNLOAD_URI fields in newer last.fm XSPF
    playlists.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=625823

 plparse/tests/Makefile.am            |    4 +-
 plparse/tests/new-lastfm-output.xspf |   37 +++++++++++++++
 plparse/tests/old-lastfm-output.xspf |   25 ++++++++++
 plparse/tests/parser.c               |   83 ++++++++++++++++++++++++++++++++++
 plparse/totem-pl-parser-xspf.c       |   14 ++++++
 5 files changed, 162 insertions(+), 1 deletions(-)
---
diff --git a/plparse/tests/Makefile.am b/plparse/tests/Makefile.am
index 2cea063..c553016 100644
--- a/plparse/tests/Makefile.am
+++ b/plparse/tests/Makefile.am
@@ -56,4 +56,6 @@ EXTRA_DIST =			\
 	remote_xspf.php		\
 	HackerMedley		\
 	missing-items.pls	\
-	playlist.xspf
+	playlist.xspf		\
+	new-lastfm-output.xspf	\
+	old-lastfm-output.xspf
diff --git a/plparse/tests/new-lastfm-output.xspf b/plparse/tests/new-lastfm-output.xspf
new file mode 100644
index 0000000..36539d7
--- /dev/null
+++ b/plparse/tests/new-lastfm-output.xspf
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="utf-8"?>
+<playlist version="1" xmlns="http://xspf.org/ns/0/";>
+<title>Free Download Tag Radio</title>
+<creator>Last.fm</creator>
+<date>2010-08-02T13:02:22</date>
+<link rel="http://www.last.fm/expiry";>3600</link>
+<trackList>
+    <track>
+        <location>http://play.last.fm/user/6a6f102641afeff59b14dfaea19cf0a5.mp3</location>
+        <title>Yellow</title>
+        <identifier>402599273</identifier>
+        <album>&quot;Yellow&quot; Single</album>
+        <creator>Scarred Sun</creator>
+        <duration>194000</duration>
+        <image>http://userserve-ak.last.fm/serve/174s/42500811.jpg</image>        
+        
+        <extension application="http://www.last.fm";>
+            <trackauth>20a82</trackauth>
+            <albumid>213205050</albumid>
+            <artistid>42814619</artistid>        
+            <recording>121553129</recording>
+                
+            <artistpage>http://www.last.fm/music/Scarred+Sun</artistpage>
+            <albumpage>http://www.last.fm/music/Scarred+Sun/%22Yellow%22+Single</albumpage>
+            <trackpage>http://www.last.fm/music/Scarred+Sun/_/Yellow</trackpage>
+            <buyTrackURL></buyTrackURL>
+            <buyAlbumURL></buyAlbumURL>
+            <freeTrackURL>http://freedownloads.last.fm/download/402599273/Yellow.mp3</freeTrackURL>
+            
+            <explicit>0</explicit>
+            <loved>0</loved>
+            <streamid>293a7dcdfc306ae2</streamid>
+            
+        </extension>        
+    </track>
+</trackList>
+</playlist>
diff --git a/plparse/tests/old-lastfm-output.xspf b/plparse/tests/old-lastfm-output.xspf
new file mode 100644
index 0000000..e35a389
--- /dev/null
+++ b/plparse/tests/old-lastfm-output.xspf
@@ -0,0 +1,25 @@
+<playlist version="1" xmlns:lastfm="http://www.audioscrobbler.net/dtd/xspf-lastfm";>
+<title>Free Download Tag Radio</title>
+<creator>Last.fm</creator>
+<link rel="http://www.last.fm/skipsLeft";>9999</link>
+<trackList>
+    <track>
+        <location>http://play.last.fm/user/78c01bd77418a90b5452eb089da642e9.mp3</location>
+        <title>Kondratiev Winter</title>
+        <id>188024406</id>
+        <album>Unsolicited Advice</album>
+        <creator>Mezzanine Stairs</creator>
+        <duration>209000</duration>
+        <image>http://userserve-ak.last.fm/serve/174s/39475261.png</image>
+        <lastfm:trackauth>d092a</lastfm:trackauth>
+        <lastfm:albumId>214851300</lastfm:albumId>
+        <lastfm:artistId>10539097</lastfm:artistId>        
+                <link rel="http://www.last.fm/artistpage";>http://www.last.fm/music/Mezzanine+Stairs</link>
+        <link rel="http://www.last.fm/albumpage";>http://www.last.fm/music/Mezzanine+Stairs/Unsolicited+Advice</link>
+        <link rel="http://www.last.fm/trackpage";>http://www.last.fm/music/Mezzanine+Stairs/_/Kondratiev+Winter</link>
+        <link rel="http://www.last.fm/buyTrackURL";></link>
+        <link rel="http://www.last.fm/buyAlbumURL";></link>
+        <link rel="http://www.last.fm/freeTrackURL";>http://freedownloads.last.fm/download/188024406/Kondratiev%2BWinter.mp3</link>
+    </track>
+</trackList>
+</playlist>
diff --git a/plparse/tests/parser.c b/plparse/tests/parser.c
index ca80b4d..9a09ad6 100644
--- a/plparse/tests/parser.c
+++ b/plparse/tests/parser.c
@@ -438,6 +438,88 @@ test_itms_parsing (void)
 }
 
 static void
+entry_parsed_id_cb (TotemPlParser *parser,
+		 const char *uri,
+		 GHashTable *metadata,
+		 char **ret)
+{
+	if (*ret == NULL)
+		*ret = g_strdup (g_hash_table_lookup (metadata, TOTEM_PL_PARSER_FIELD_ID));
+}
+
+static char *
+parser_test_get_id (const char *uri)
+{
+	TotemPlParserResult retval;
+	char *ret = NULL;
+	TotemPlParser *pl = totem_pl_parser_new ();
+
+	g_object_set (pl, "recurse", !option_no_recurse,
+			  "debug", option_debug,
+			  "force", option_force,
+			  "disable-unsafe", option_disable_unsafe,
+			  NULL);
+	g_signal_connect (G_OBJECT (pl), "entry-parsed",
+			  G_CALLBACK (entry_parsed_id_cb), &ret);
+
+	retval = totem_pl_parser_parse_with_base (pl, uri, option_base_uri, FALSE);
+	g_test_message ("Got retval %d for uri '%s'", retval, uri);
+	g_object_unref (pl);
+
+	return ret;
+}
+
+static void
+entry_parsed_download_cb (TotemPlParser *parser,
+		 const char *uri,
+		 GHashTable *metadata,
+		 char **ret)
+{
+	if (*ret == NULL)
+		*ret = g_strdup (g_hash_table_lookup (metadata, TOTEM_PL_PARSER_FIELD_DOWNLOAD_URI));
+}
+
+static char *
+parser_test_get_download_uri (const char *uri)
+{
+	TotemPlParserResult retval;
+	char *ret = NULL;
+	TotemPlParser *pl = totem_pl_parser_new ();
+
+	g_object_set (pl, "recurse", !option_no_recurse,
+			  "debug", option_debug,
+			  "force", option_force,
+			  "disable-unsafe", option_disable_unsafe,
+			  NULL);
+	g_signal_connect (G_OBJECT (pl), "entry-parsed",
+			  G_CALLBACK (entry_parsed_download_cb), &ret);
+
+	retval = totem_pl_parser_parse_with_base (pl, uri, option_base_uri, FALSE);
+	g_test_message ("Got retval %d for uri '%s'", retval, uri);
+	g_object_unref (pl);
+
+	return ret;
+}
+
+static void
+test_lastfm_parsing (void)
+{
+	char *uri;
+
+	g_test_bug ("625823");
+
+	uri = get_relative_uri (TEST_SRCDIR "old-lastfm-output.xspf");
+	g_assert_cmpstr (parser_test_get_download_uri (uri), ==, "http://freedownloads.last.fm/download/188024406/Kondratiev%2BWinter.mp3";);
+	g_assert_cmpstr (parser_test_get_id (uri), ==, "d092a");
+	g_free (uri);
+
+	uri = get_relative_uri (TEST_SRCDIR "new-lastfm-output.xspf");
+	g_assert_cmpstr (parser_test_get_download_uri (uri), ==, "http://freedownloads.last.fm/download/402599273/Yellow.mp3";);
+	g_assert_cmpstr (parser_test_get_id (uri), ==, "20a82");
+	g_free (uri);
+}
+
+static void
 test_parsing_xspf_genre (void)
 {
 	char *uri;
@@ -771,6 +853,7 @@ main (int argc, char *argv[])
 		g_test_add_func ("/parser/parsing/num_items_in_pls", test_parsing_num_entries);
 		g_test_add_func ("/parser/parsing/xspf_genre", test_parsing_xspf_genre);
 		g_test_add_func ("/parser/parsing/itms_link", test_itms_parsing);
+		g_test_add_func ("/parser/parsing/lastfm-attributes", test_lastfm_parsing);
 
 		return g_test_run ();
 	}
diff --git a/plparse/totem-pl-parser-xspf.c b/plparse/totem-pl-parser-xspf.c
index 647b57c..3a8375c 100644
--- a/plparse/totem-pl-parser-xspf.c
+++ b/plparse/totem-pl-parser-xspf.c
@@ -266,6 +266,20 @@ parse_xspf_track (TotemPlParser *parser, GFile *base_file, xmlDocPtr doc,
 						break;
 					}
 				}
+			} else if (app != NULL && g_ascii_strcasecmp ((char *) app, "http://www.last.fm";) == 0) {
+				xmlNodePtr child;
+				for (child = node->xmlChildrenNode ; child; child = child->next) {
+					if (child->name != NULL) {
+						if (g_ascii_strcasecmp ((char *)child->name, "trackauth") == 0) {
+							id = xmlNodeListGetString (doc, child->xmlChildrenNode, 0);
+							continue;
+						}
+						if (g_ascii_strcasecmp ((char *)child->name, "freeTrackURL") == 0) {
+							download_uri = xmlNodeListGetString (doc, child->xmlChildrenNode, 0);
+							continue;
+						}
+					}
+				}
 			}
 		} else if (g_ascii_strcasecmp ((char *)node->name, "album") == 0)
 			album = xmlNodeListGetString (doc, node->xmlChildrenNode, 1);



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