Top |
Functions
Description
GDataYouTubeService is a subclass of GDataService for communicating with the GData API of YouTube. It supports querying for and uploading videos using version 3 of the API.
The YouTube API supports returning different sets of properties for
GDataYouTubeVideos depending on the specific query. For search results, only
‘snippet’ properties are returned (including “title”,
“summary” and the set of thumbnails). For querying single videos,
a more complete set of properties are returned — so use
gdata_service_query_single_entry_async()
to get further details on a video.
For more details of YouTube's GData API, see the online documentation.
Example 11. Getting a Localized List of YouTube Categories
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
GDataYouTubeService *service; GDataAPPCategories *app_categories; GList *categories, *i; /* Create a service and set its locale to Italian, which localizes the categories to Italian */ service = create_youtube_service (); gdata_service_set_locale (GDATA_SERVICE (service), "it"); /* Query the server for the current list of YouTube categories (in Italian) */ app_categories = gdata_youtube_service_get_categories (service, NULL, NULL); categories = gdata_app_categories_get_categories (app_categories); /* Iterate through the categories */ for (i = categories; i != NULL; i = i->next) { GDataYouTubeCategory *category = GDATA_YOUTUBE_CATEGORY (i->data); if (gdata_youtube_category_is_deprecated (category) == FALSE && gdata_youtube_category_is_browsable (category, "IT") == TRUE) { /* Do something with the category here, as it's not deprecated, and is browsable in the given region */ add_to_ui (gdata_category_get_term (GDATA_CATEGORY (category)), gdata_category_get_label (GDATA_CATEGORY (category))); } } g_object_unref (app_categories); g_object_unref (service); |
Example 12. Uploading a Video from Disk
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
GDataYouTubeService *service; GDataYouTubeVideo *video, *uploaded_video; GDataMediaCategory *category; const gchar * const tags[] = { "tag1", "tag2", NULL }; GFile *video_file; GFileInfo *file_info; const gchar *slug, *content_type; GFileInputStream *file_stream; GDataUploadStream *upload_stream; GError *error = NULL; /* Create a service */ service = create_youtube_service (); /* Get the video file to upload */ video_file = g_file_new_for_path ("sample.ogg"); /* Get the file's display name and content type */ file_info = g_file_query_info (video_file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME "," G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, G_FILE_QUERY_INFO_NONE, NULL, &error); if (error != NULL) { g_error ("Error getting video file information: %s", error->message); g_error_free (error); g_object_unref (video_file); g_object_unref (service); return; } slug = g_file_info_get_display_name (file_info); content_type = g_file_info_get_content_type (file_info); /* Get an input stream for the file */ file_stream = g_file_read (video_file, NULL, &error); g_object_unref (video_file); if (error != NULL) { g_error ("Error getting video file stream: %s", error->message); g_error_free (error); g_object_unref (file_info); g_object_unref (service); return; } /* Create the video to upload */ video = gdata_youtube_video_new (NULL); gdata_entry_set_title (GDATA_ENTRY (video), "Video Title"); gdata_youtube_video_set_description (video, "Video description."); gdata_youtube_video_set_keywords (video, video_tags); category = gdata_media_category_new ("People", "http://gdata.youtube.com/schemas/2007/categories.cat", NULL); gdata_youtube_video_set_category (video, category); g_object_unref (category); /* Get an upload stream for the video */ upload_stream = gdata_youtube_service_upload_video (service, video, slug, content_type, NULL, &error); g_object_unref (video); g_object_unref (file_info); if (error != NULL) { g_error ("Error getting upload stream: %s", error->message); g_error_free (error); g_object_unref (file_stream); g_object_unref (service); return; } /* Upload the video. This is a blocking operation, and should normally be done asynchronously. */ g_output_stream_splice (G_OUTPUT_STREAM (upload_stream), G_INPUT_STREAM (file_stream), G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET, NULL, &error); g_object_unref (file_stream); if (error != NULL) { g_error ("Error splicing streams: %s", error->message); g_error_free (error); g_object_unref (upload_stream); g_object_unref (service); return; } /* Finish off the upload by parsing the returned updated video entry */ uploaded_video = gdata_youtube_service_finish_video_upload (service, upload_stream, &error); g_object_unref (upload_stream); g_object_unref (service); if (error != NULL) { g_error ("Error uploading video: %s", error->message); g_error_free (error); return; } /* Do something with the uploaded video */ g_object_unref (uploaded_video); |
Example 13. Querying for Videos from a Standard Feed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
GDataYouTubeService *service; GDataFeed *feed; GList *i; GError *error = NULL; /* Create a service */ service = create_youtube_service (); /* Query for the top page of videos in the most popular feed */ feed = gdata_youtube_service_query_standard_feed (service, GDATA_YOUTUBE_MOST_POPULAR_FEED, NULL, NULL, NULL, NULL, &error); g_object_unref (service); if (error != NULL) { g_error ("Error querying for most popular videos: %s", error->message); g_error_free (error); return; } /* Iterate through the videos */ for (i = gdata_feed_get_entries (feed); i != NULL; i = i->next) { GDataYouTubeVideo *video = GDATA_YOUTUBE_VIDEO (i->data); /* Do something with the video, like insert it into the UI */ } g_object_unref (feed); |
Example 14. Querying for Videos using Search Terms
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
GDataYouTubeService *service; GDataYouTubeQuery *query; GDataFeed *feed; GList *i; GError *error = NULL; /* Create a service */ service = create_youtube_service (); /* Build a query with the given search terms, also matching only videos which are CC-licensed */ query = gdata_youtube_query_new (my_space_separated_search_terms); gdata_youtube_query_set_license (query, GDATA_YOUTUBE_LICENSE_CC); /* Query for the videos matching the query parameters */ feed = gdata_youtube_service_query_videos (service, query, NULL, NULL, NULL, &error); g_object_unref (query); g_object_unref (service); if (error != NULL) { g_error ("Error querying for videos matching search terms ‘%s’: %s", my_space_separated_search_terms, error->message); g_error_free (error); return; } /* Iterate through the videos */ for (i = gdata_feed_get_entries (feed); i != NULL; i = i->next) { GDataYouTubeVideo *video = GDATA_YOUTUBE_VIDEO (i->data); /* Do something with the video, like insert it into the UI */ } g_object_unref (feed); |
Functions
gdata_youtube_service_new ()
GDataYouTubeService * gdata_youtube_service_new (const gchar *developer_key
,GDataAuthorizer *authorizer
);
Creates a new GDataYouTubeService using the given GDataAuthorizer. If authorizer
is NULL
, all requests are made as an unauthenticated user.
The developer_key
must be unique for your application, and as
Parameters
developer_key |
your application's developer API key |
|
authorizer |
a GDataAuthorizer to authorize the service's requests, or |
[allow-none] |
Since: 0.9.0
gdata_youtube_service_get_primary_authorization_domain ()
GDataAuthorizationDomain *
gdata_youtube_service_get_primary_authorization_domain
(void
);
The primary GDataAuthorizationDomain for interacting with YouTube. This will not normally need to be used, as it's used internally by the GDataYouTubeService methods. However, if using the plain GDataService methods to implement custom queries or requests which libgdata does not support natively, then this domain may be needed to authorize the requests.
The domain never changes, and is interned so that pointer comparison can be used to differentiate it from other authorization domains.
Since: 0.9.0
gdata_youtube_service_query_videos ()
GDataFeed * gdata_youtube_service_query_videos (GDataYouTubeService *self
,GDataQuery *query
,GCancellable *cancellable
,GDataQueryProgressCallback progress_callback
,gpointer progress_user_data
,GError **error
);
Queries the service for videos matching the parameters set on the GDataQuery. This searches site-wide, and imposes no other restrictions or parameters on the query.
Parameters and errors are as for gdata_service_query()
.
Parameters
self |
||
query |
a GDataQuery with the query parameters, or |
[allow-none] |
cancellable |
optional GCancellable object, or |
[allow-none] |
progress_callback |
a GDataQueryProgressCallback to call when an entry is loaded, or |
[allow-none][scope call][closure progress_user_data] |
progress_user_data |
data to pass to the |
[closure] |
error |
gdata_youtube_service_query_videos_async ()
void gdata_youtube_service_query_videos_async (GDataYouTubeService *self
,GDataQuery *query
,GCancellable *cancellable
,GDataQueryProgressCallback progress_callback
,gpointer progress_user_data
,GDestroyNotify destroy_progress_user_data
,GAsyncReadyCallback callback
,gpointer user_data
);
Queries the service for videos matching the parameters set on the GDataQuery. This searches site-wide, and imposes no other restrictions or
parameters on the query. self
and query
are both reffed when this function is called, so can safely be freed after this function returns.
For more details, see gdata_youtube_service_query_videos()
, which is the synchronous version of this function.
When the operation is finished, callback
will be called. You can then call gdata_service_query_finish()
to get the results of the operation.
Parameters
self |
||
query |
a GDataQuery with the query parameters, or |
[allow-none] |
cancellable |
optional GCancellable object, or |
[allow-none] |
progress_callback |
a GDataQueryProgressCallback to call when an entry is loaded, or |
[allow-none][closure progress_user_data] |
progress_user_data |
data to pass to the |
[closure] |
destroy_progress_user_data |
the function to call when |
[allow-none] |
callback |
a GAsyncReadyCallback to call when authentication is finished |
|
user_data |
data to pass to the |
[closure] |
Since: 0.9.1
gdata_youtube_service_query_related ()
GDataFeed * gdata_youtube_service_query_related (GDataYouTubeService *self
,GDataYouTubeVideo *video
,GDataQuery *query
,GCancellable *cancellable
,GDataQueryProgressCallback progress_callback
,gpointer progress_user_data
,GError **error
);
Queries the service for videos related to video
. The algorithm determining which videos are related is on the server side.
Parameters and other errors are as for gdata_service_query()
.
Parameters
self |
||
video |
a GDataYouTubeVideo for which to find related videos |
|
query |
a GDataQuery with the query parameters, or |
[allow-none] |
cancellable |
optional GCancellable object, or |
[allow-none] |
progress_callback |
a GDataQueryProgressCallback to call when an entry is loaded, or |
[allow-none][scope call][closure progress_user_data] |
progress_user_data |
data to pass to the |
[closure] |
error |
gdata_youtube_service_query_related_async ()
void gdata_youtube_service_query_related_async (GDataYouTubeService *self
,GDataYouTubeVideo *video
,GDataQuery *query
,GCancellable *cancellable
,GDataQueryProgressCallback progress_callback
,gpointer progress_user_data
,GDestroyNotify destroy_progress_user_data
,GAsyncReadyCallback callback
,gpointer user_data
);
Queries the service for videos related to video
. The algorithm determining which videos are related is on the server side.
self
and query
are both reffed when this function is called, so can safely be freed after this function returns.
For more details, see gdata_youtube_service_query_related()
, which is the synchronous version of this function.
When the operation is finished, callback
will be called. You can then call gdata_service_query_finish()
to get the results of the operation.
Parameters
self |
||
video |
a GDataYouTubeVideo for which to find related videos |
|
query |
a GDataQuery with the query parameters, or |
[allow-none] |
cancellable |
optional GCancellable object, or |
[allow-none] |
progress_callback |
a GDataQueryProgressCallback to call when an entry is loaded, or |
[allow-none][closure progress_user_data] |
progress_user_data |
data to pass to the |
[closure] |
destroy_progress_user_data |
the function to call when |
[allow-none] |
callback |
a GAsyncReadyCallback to call when authentication is finished |
|
user_data |
data to pass to the |
[closure] |
Since: 0.9.1
gdata_youtube_service_query_standard_feed ()
GDataFeed * gdata_youtube_service_query_standard_feed (GDataYouTubeService *self
,GDataYouTubeStandardFeedType feed_type
,GDataQuery *query
,GCancellable *cancellable
,GDataQueryProgressCallback progress_callback
,gpointer progress_user_data
,GError **error
);
Queries the service's standard feed_type
feed to build a GDataFeed.
Note that with the port from v2 to v3 of the YouTube API in libgdata
0.17.0, all feed types except GDATA_YOUTUBE_MOST_POPULAR_FEED
have been
deprecated. Other feed types will now transparently return
GDATA_YOUTUBE_MOST_POPULAR_FEED
, limited to the past 24 hours.
Parameters and errors are as for gdata_service_query()
.
Parameters
self |
||
feed_type |
the feed type to query, from GDataYouTubeStandardFeedType |
|
query |
a GDataQuery with the query parameters, or |
[allow-none] |
cancellable |
optional GCancellable object, or |
[allow-none] |
progress_callback |
a GDataQueryProgressCallback to call when an entry is loaded, or |
[allow-none][scope call][closure progress_user_data] |
progress_user_data |
data to pass to the |
[closure] |
error |
gdata_youtube_service_query_standard_feed_async ()
void gdata_youtube_service_query_standard_feed_async (GDataYouTubeService *self
,GDataYouTubeStandardFeedType feed_type
,GDataQuery *query
,GCancellable *cancellable
,GDataQueryProgressCallback progress_callback
,gpointer progress_user_data
,GDestroyNotify destroy_progress_user_data
,GAsyncReadyCallback callback
,gpointer user_data
);
Queries the service's standard feed_type
feed to build a GDataFeed. self
and
query
are both reffed when this function is called, so can safely be freed after this function returns.
For more details, see gdata_youtube_service_query_standard_feed()
, which is the synchronous version of this function.
When the operation is finished, callback
will be called. You can then call gdata_service_query_finish()
to get the results of the operation.
Parameters
self |
||
feed_type |
the feed type to query, from GDataYouTubeStandardFeedType |
|
query |
a GDataQuery with the query parameters, or |
[allow-none] |
cancellable |
optional GCancellable object, or |
[allow-none] |
progress_callback |
a GDataQueryProgressCallback to call when an entry is loaded, or |
[allow-none][closure progress_user_data] |
progress_user_data |
data to pass to the |
[closure] |
destroy_progress_user_data |
the function to call when |
[allow-none] |
callback |
a GAsyncReadyCallback to call when authentication is finished |
|
user_data |
data to pass to the |
[closure] |
Since: 0.9.1
gdata_youtube_service_upload_video ()
GDataUploadStream * gdata_youtube_service_upload_video (GDataYouTubeService *self
,GDataYouTubeVideo *video
,const gchar *slug
,const gchar *content_type
,GCancellable *cancellable
,GError **error
);
Uploads a video to YouTube, using the properties from video
and the file data written to the resulting GDataUploadStream.
If video
has already been inserted, a GDATA_SERVICE_ERROR_ENTRY_ALREADY_INSERTED
error will be returned. If no user is authenticated
with the service, GDATA_SERVICE_ERROR_AUTHENTICATION_REQUIRED
will be returned.
The stream returned by this function should be written to using the standard GOutputStream methods, asychronously or synchronously. Once the stream
is closed (using g_output_stream_close()
), gdata_youtube_service_finish_video_upload()
should be called on it to parse and return the updated
GDataYouTubeVideo for the uploaded video. This must be done, as video
isn't updated in-place.
In order to cancel the upload, a GCancellable passed in to cancellable
must be cancelled using g_cancellable_cancel()
. Cancelling the individual
GOutputStream operations on the GDataUploadStream will not cancel the entire upload; merely the write or close operation in question. See the
“cancellable” for more details.
Any upload errors will be thrown by the stream methods, and may come from the GDataServiceError domain.
Parameters
self |
||
video |
a GDataYouTubeVideo to insert |
|
slug |
the filename to give to the uploaded file |
|
content_type |
the content type of the uploaded data |
|
cancellable |
a GCancellable for the entire upload stream, or |
[allow-none] |
error |
Returns
a GDataUploadStream to write the video data to, or NULL
; unref with g_object_unref()
.
[transfer full]
Since: 0.8.0
gdata_youtube_service_finish_video_upload ()
GDataYouTubeVideo * gdata_youtube_service_finish_video_upload (GDataYouTubeService *self
,GDataUploadStream *upload_stream
,GError **error
);
Finish off a video upload operation started by gdata_youtube_service_upload_video()
, parsing the result and returning the new GDataYouTubeVideo.
If an error occurred during the upload operation, it will have been returned during the operation (e.g. by g_output_stream_splice()
or one
of the other stream methods). In such a case, NULL
will be returned but error
will remain unset. error
is only set in the case that the server
indicates that the operation was successful, but an error is encountered in parsing the result sent by the server.
Since: 0.8.0
gdata_youtube_service_get_categories ()
GDataAPPCategories * gdata_youtube_service_get_categories (GDataYouTubeService *self
,GCancellable *cancellable
,GError **error
);
Gets a list of the categories currently in use on YouTube. The returned GDataAPPCategories contains a list of GDataYouTubeCategorys which enumerate the current YouTube categories.
The category labels (“label”) are localised based on the value of “locale”.
Since: 0.7.0
gdata_youtube_service_get_categories_async ()
void gdata_youtube_service_get_categories_async (GDataYouTubeService *self
,GCancellable *cancellable
,GAsyncReadyCallback callback
,gpointer user_data
);
Gets a list of the categories currently in use on YouTube. self
is reffed when this function is called, so can safely be unreffed after this
function returns.
For more details, see gdata_youtube_service_get_categories()
, which is the synchronous version of this function.
When the operation is finished, callback
will be called. You can then call gdata_youtube_service_get_categories_finish()
to get the results of the
operation.
Parameters
self |
||
cancellable |
optional GCancellable object, or |
[allow-none] |
callback |
a GAsyncReadyCallback to call when the request is finished |
|
user_data |
data to pass to the |
[closure] |
Since: 0.7.0
gdata_youtube_service_get_categories_finish ()
GDataAPPCategories * gdata_youtube_service_get_categories_finish (GDataYouTubeService *self
,GAsyncResult *async_result
,GError **error
);
Finishes an asynchronous request for a list of categories on YouTube, as started with gdata_youtube_service_get_categories_async()
.
Since: 0.7.0
gdata_youtube_service_get_developer_key ()
const gchar *
gdata_youtube_service_get_developer_key
(GDataYouTubeService *self
);
Gets the “developer-key” property from the GDataYouTubeService.
Types and Values
GDataYouTubeService
typedef struct _GDataYouTubeService GDataYouTubeService;
All the fields in the GDataYouTubeService structure are private and should never be accessed directly.
GDataYouTubeServiceClass
typedef struct { } GDataYouTubeServiceClass;
All the fields in the GDataYouTubeServiceClass structure are private and should never be accessed directly.
enum GDataYouTubeServiceError
Error codes for GDataYouTubeService operations.
Members
the API request quota for this developer account has been exceeded |
||
the entry (e.g. video) quota for this user account has been exceeded |
||
the currently authenticated user doesn't have a YouTube channel, but the current action requires one; if this error is received, inform the user that they need a YouTube channel, and provide a link to https://www.youtube.com/create_channel |
enum GDataYouTubeStandardFeedType
Standard feed types for standard feed queries with
gdata_youtube_service_query_standard_feed()
. For more information, see the
Members
This feed contains the most highly rated
YouTube videos. Deprecated: 0.17.0: Google no longer supports this feed
type, and it will return results equivalent to
|
||
This feed contains videos most frequently
flagged as favorite videos. Deprecated: 0.17.0: Google no longer
supports this feed type, and it will return results equivalent to
|
||
This feed contains the most frequently
watched YouTube videos. Deprecated: 0.17.0: Google no longer supports
this feed type, and it will return results equivalent to
|
||
This feed contains the most popular YouTube videos, selected using an algorithm that combines many different signals to determine overall popularity. As of version 0.17.0, this is the only supported feed type. |
||
This feed contains the videos most recently
submitted to YouTube. Deprecated: 0.17.0: Google no longer supports
this feed type, and it will return results equivalent to
|
||
This feed contains the YouTube videos
that have received the most comments. Deprecated: 0.17.0: Google no
longer supports this feed type, and it will return results equivalent to
|
||
This feed contains the YouTube videos that
receive the most links from other websites. Deprecated: 0.17.0: Google
no longer supports this feed type, and it will return results equivalent to
|
||
This feed contains YouTube videos that
receive the most video responses. Deprecated: 0.17.0: Google no longer
supports this feed type, and it will return results equivalent to
|
||
This feed contains videos recently
featured on the YouTube home page or featured videos tab. Deprecated:
0.17.0: Google no longer supports this feed type, and it will return
results equivalent to |
||
This feed contains videos suitable for
playback on mobile devices. Deprecated: 0.17.0: Google no longer
supports this feed type, and it will return results equivalent to
|
Property Details
The “developer-key”
property
“developer-key” gchar *
The developer key your application has registered with the YouTube API. For more information, see the online documentation.
With the port from v2 to v3 of the YouTube API in libgdata 0.17.0, it might be necessary to update your application’s developer key.
Flags: Read / Write / Construct Only
Default value: NULL