[libgdata/libgdata-0-6] Parse new format YouTube URIs
- From: Philip Withnall <pwithnall src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgdata/libgdata-0-6] Parse new format YouTube URIs
- Date: Fri, 19 Mar 2010 21:34:03 +0000 (UTC)
commit 46ef92dbc5816b13abcfd08cfdc2502ad578c0d9
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 f89a4a6..a61eb2e 100644
--- a/gdata/services/youtube/gdata-youtube-video.c
+++ b/gdata/services/youtube/gdata-youtube-video.c
@@ -1331,8 +1331,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);
@@ -1341,15 +1340,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 755779f..3884733 100644
--- a/gdata/tests/youtube.c
+++ b/gdata/tests/youtube.c
@@ -645,6 +645,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]