[libgdata] [youtube] Added a convenience function to extract video IDs from video URIs



commit fdcfed6f126a622e946532dba12a3cc3596d562a
Author: Philip Withnall <philip tecnocode co uk>
Date:   Sun Jul 5 23:15:15 2009 +0100

    [youtube] Added a convenience function to extract video IDs from video URIs

 docs/reference/gdata-sections.txt            |    1 +
 gdata/gdata.symbols                          |    1 +
 gdata/services/youtube/gdata-youtube-video.c |   43 ++++++++++++++++++++++++++
 gdata/services/youtube/gdata-youtube-video.h |    2 +
 gdata/tests/youtube.c                        |   14 ++++++++
 5 files changed, 61 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/gdata-sections.txt b/docs/reference/gdata-sections.txt
index 505021a..94c3b76 100644
--- a/docs/reference/gdata-sections.txt
+++ b/docs/reference/gdata-sections.txt
@@ -234,6 +234,7 @@ gdata_youtube_video_get_uploaded
 gdata_youtube_video_get_recorded
 gdata_youtube_video_set_recorded
 gdata_youtube_video_get_video_id
+gdata_youtube_video_get_video_id_from_uri
 <SUBSECTION Standard>
 GDATA_IS_YOUTUBE_VIDEO
 GDATA_IS_YOUTUBE_VIDEO_CLASS
diff --git a/gdata/gdata.symbols b/gdata/gdata.symbols
index 50e2e34..180ee44 100644
--- a/gdata/gdata.symbols
+++ b/gdata/gdata.symbols
@@ -140,6 +140,7 @@ gdata_youtube_video_set_is_draft
 gdata_youtube_video_get_state
 gdata_youtube_video_get_recorded
 gdata_youtube_video_set_recorded
+gdata_youtube_video_get_video_id_from_uri
 gdata_youtube_format_get_type
 gdata_youtube_safe_search_get_type
 gdata_youtube_sort_order_get_type
diff --git a/gdata/services/youtube/gdata-youtube-video.c b/gdata/services/youtube/gdata-youtube-video.c
index 4a262fe..3ea48f8 100644
--- a/gdata/services/youtube/gdata-youtube-video.c
+++ b/gdata/services/youtube/gdata-youtube-video.c
@@ -1305,3 +1305,46 @@ gdata_youtube_video_set_recorded (GDataYouTubeVideo *self, GTimeVal *recorded)
 		self->priv->recorded = *recorded;
 	}
 }
+
+/**
+ * gdata_youtube_video_get_video_id_from_uri:
+ * @video_uri: a YouTube video player URI
+ *
+ * Extracts a video ID from a YouTube video player URI. The video ID is in the same form as returned by
+ * gdata_youtube_video_get_video_id(), and the @video_uri should be in the same form as returned by
+ * gdata_youtube_video_get_player_uri().
+ *
+ * For example:
+ * <informalexample><programlisting>
+ * video_id = gdata_youtube_video_get_video_id_from_uri ("http://www.youtube.com/watch?v=BH_vwsyCrTc&feature=featured";);
+ * g_message ("Video ID: %s", video_id); /\* Should print: BH_vwsyCrTc *\/
+ * g_free (video_id);
+ * </programlisting></informalexample>
+ *
+ * Since: 0.4.0
+ **/
+gchar *
+gdata_youtube_video_get_video_id_from_uri (const gchar *video_uri)
+{
+	GHashTable *params;
+	gchar *video_id;
+	SoupURI *uri;
+
+	g_return_val_if_fail (video_uri != NULL && *video_uri != '\0', NULL);
+
+	/* Extract the query string from the URI */
+	uri = soup_uri_new (video_uri);
+	if (uri == NULL)
+		return NULL;
+	else if (uri->query == NULL) {
+		soup_uri_free (uri);
+		return NULL;
+	}
+
+	params = soup_form_decode (uri->query);
+	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/services/youtube/gdata-youtube-video.h b/gdata/services/youtube/gdata-youtube-video.h
index 37396b1..d377b87 100644
--- a/gdata/services/youtube/gdata-youtube-video.h
+++ b/gdata/services/youtube/gdata-youtube-video.h
@@ -95,6 +95,8 @@ GDataYouTubeState *gdata_youtube_video_get_state (GDataYouTubeVideo *self);
 void gdata_youtube_video_get_recorded (GDataYouTubeVideo *self, GTimeVal *recorded);
 void gdata_youtube_video_set_recorded (GDataYouTubeVideo *self, GTimeVal *recorded);
 
+gchar *gdata_youtube_video_get_video_id_from_uri (const gchar *video_uri);
+
 G_END_DECLS
 
 #endif /* !GDATA_YOUTUBE_VIDEO_H */
diff --git a/gdata/tests/youtube.c b/gdata/tests/youtube.c
index 4d15b71..af3f411 100644
--- a/gdata/tests/youtube.c
+++ b/gdata/tests/youtube.c
@@ -625,6 +625,19 @@ test_query_single_async (GDataService *service)
 	g_main_loop_unref (main_loop);
 }
 
+static void
+test_parsing_video_id_from_uri (void)
+{
+	gchar *video_id;
+
+	video_id = gdata_youtube_video_get_video_id_from_uri ("http://www.youtube.com/watch?v=BH_vwsyCrTc&feature=featured";);
+	g_assert_cmpstr (video_id, ==, "BH_vwsyCrTc");
+	g_free (video_id);
+
+	video_id = gdata_youtube_video_get_video_id_from_uri ("http://foobar.com/not/real";);
+	g_assert (video_id == NULL);
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -658,6 +671,7 @@ main (int argc, char *argv[])
 	g_test_add_data_func ("/youtube/query/single", service, test_query_single);
 	if (g_test_slow () == TRUE)
 		g_test_add_data_func ("/youtube/query/single_async", service, test_query_single_async);
+	g_test_add_func ("/youtube/parsing/video_id_from_uri", test_parsing_video_id_from_uri);
 
 	retval = g_test_run ();
 	g_object_unref (service);



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