[libgdata] Parse new format YouTube URIs



commit 57df327231a48bb7a92e2c0d01393d7a8b2fac7b
Author: Philip Withnall <philip tecnocode co uk>
Date:   Wed Mar 10 22:36:12 2010 +0000

    Parse new format YouTube URIs
    
    Extend gdata_youtube_video_get_video_id_from_uri() to deal with the new
    format URIs introduced recently:
    http://apiblog.youtube.com/2010/03/upcoming-change-to-youtube-video-page.html

 gdata/services/youtube/gdata-youtube-video.c |   31 +++++++++++++++++++++-----
 gdata/tests/youtube.c                        |   14 +++++++++++
 2 files changed, 39 insertions(+), 6 deletions(-)
---
diff --git a/gdata/services/youtube/gdata-youtube-video.c b/gdata/services/youtube/gdata-youtube-video.c
index 0379d5c..812fce5 100644
--- a/gdata/services/youtube/gdata-youtube-video.c
+++ b/gdata/services/youtube/gdata-youtube-video.c
@@ -1405,8 +1405,7 @@ gdata_youtube_video_set_recorded (GDataYouTubeVideo *self, GTimeVal *recorded)
 gchar *
 gdata_youtube_video_get_video_id_from_uri (const gchar *video_uri)
 {
-	GHashTable *params;
-	gchar *video_id;
+	gchar *video_id = NULL;
 	SoupURI *uri;
 
 	g_return_val_if_fail (video_uri != NULL && *video_uri != '\0', NULL);
@@ -1415,15 +1414,35 @@ gdata_youtube_video_get_video_id_from_uri (const gchar *video_uri)
 	uri = soup_uri_new (video_uri);
 	if (uri == NULL)
 		return NULL;
-	else if (uri->query == NULL || uri->host == NULL || strstr (uri->host, "youtube") == NULL) {
+	else if (uri->host == NULL || strstr (uri->host, "youtube") == NULL) {
 		soup_uri_free (uri);
 		return NULL;
 	}
 
-	params = soup_form_decode (uri->query);
+	/* Try the "v" parameter (e.g. format is: http://www.youtube.com/watch?v=ylLzyHk54Z0) */
+	if (uri->query != NULL) {
+		GHashTable *params = soup_form_decode (uri->query);
+		video_id = g_strdup (g_hash_table_lookup (params, "v"));
+		g_hash_table_destroy (params);
+	}
+
+	/* Try the "v" fragment component (e.g. format is: http://www.youtube.com/watch#!v=ylLzyHk54Z0).
+	 * YouTube introduced this new URI format in March 2010:
+	 * http://apiblog.youtube.com/2010/03/upcoming-change-to-youtube-video-page.html */
+	if (video_id == NULL && uri->fragment != NULL) {
+		gchar **components, **i;
+
+		components = g_strsplit (uri->fragment, "!", -1);
+		for (i = components; *i != NULL; i++) {
+			if (**i == 'v' && *((*i) + 1) == '=') {
+				video_id = g_strdup ((*i) + 2);
+				break;
+			}
+		}
+		g_strfreev (components);
+	}
+
 	soup_uri_free (uri);
-	video_id = g_strdup (g_hash_table_lookup (params, "v"));
-	g_hash_table_destroy (params);
 
 	return video_id;
 }
diff --git a/gdata/tests/youtube.c b/gdata/tests/youtube.c
index 8b09807..d22a799 100644
--- a/gdata/tests/youtube.c
+++ b/gdata/tests/youtube.c
@@ -737,6 +737,20 @@ test_parsing_video_id_from_uri (void)
 
 	video_id = gdata_youtube_video_get_video_id_from_uri ("http://foobar.com/not/real";);
 	g_assert (video_id == NULL);
+
+	video_id = gdata_youtube_video_get_video_id_from_uri ("http://www.youtube.com/watch#!v=ylLzyHk54Z0";);
+	g_assert_cmpstr (video_id, ==, "ylLzyHk54Z0");
+	g_free (video_id);
+
+	video_id = gdata_youtube_video_get_video_id_from_uri ("http://www.youtube.com/watch#!foo=bar!v=ylLzyHk54Z0";);
+	g_assert_cmpstr (video_id, ==, "ylLzyHk54Z0");
+	g_free (video_id);
+
+	video_id = gdata_youtube_video_get_video_id_from_uri ("http://www.youtube.com/watch#!foo=bar";);
+	g_assert (video_id == NULL);
+
+	video_id = gdata_youtube_video_get_video_id_from_uri ("http://www.youtube.com/watch#random-fragment";);
+	g_assert (video_id == NULL);
 }
 
 int



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