A key component of VidCaster is to provide simple aggregated video analytics to our users, which means we need to import YouTube Insight data.
The general YouTube 2.0 API supports downloading of this Insight data if the request is authenticated on the behalf of the user that owns this video. Per the documentation, you can obtain a link to a CSV video analytics report for an individual clip by requesting a video entry while authenticated on behalf of the user. In Google’s words:
A video entry contains a link to an Insight data feed if the authenticated user retrieving the entry owns the video.
Unfortunately, this is a GData 2.0 API call and the current Python-GData-Client library only supports 1.0 API calls for YouTube, through the deprecated
service class, instead of the 2.0 API
The question: How to access this YouTube 2.0 API data using the current version of the GData-Python-Client library? We’ve already gone through the trouble of integrating OAuth for our users, and we’d rather not write a full OAuth query implementation, nor rewrite the GData-Python-Client library.
The answer: We can use a rather generic authenticated 2.0 request to fetch raw queries and process the XML response ourselves.
While we can’t use the developer-friendly feed-specific queries like
yt_service.GetYouTubeVideoFeed() from the 1.0 API, we can still make the request and retrieve a response while authenticated which allows us to get the valuable Insight data.
Below is the basic code, note that you’ll have to implement OAuth before using this solution:
from gdata.gauth import OAuthHmacToken
# Our code to fetch site settings and individual OAuth credentials
from django.conf import settings
from myproject.credentials.models import Credential
CONSUMER_KEY = settings.GOOGLE_CONSUMER_KEY
CONSUMER_SECRET = settings.GOOGLE_CONSUMER_SECRET
YOUTUBE_DEVELOPER_KEY = settings.YOUTUBE_DEVELOPER_KEY
SCOPES = ['http://gdata.youtube.com']
# Fetch token and token_secret per your existing OAuth implementation
credential = Credential.objects.get(pk=1)
token = credential.token
token_secret = credential.token_secret
yt_service = gdata.youtube.client.YouTubeClient(source='MyCompany-MyApp-v1')
yt_service.developer_key = YOUTUBE_DEVELOPER_KEY
oauth_token = OAuthHmacToken(consumer_key=CONSUMER_KEY, consumer_secret=CONSUMER_SECRET, token=token, token_secret=token_secret, auth_state=gdata.gauth.ACCESS_TOKEN)
feed = yt_service.get_feed(uri="http://gdata.youtube.com/feeds/api/users/default/uploads/?v=2", auth_token=oauth_token)
# Great, now you have a feed that includes links to the Insight CSV files!
# I'll leave pagination and parsing XML as an exercise for the reader.
- It works.
- No modification of existing gdata-python-client library needed, also saves you from needing to write a raw oauth signed query to obtain this data.
- Returns raw XML response, you’ll have to parse this yourself.
- Not a standard way of implementing a signed OAuth request in V2.0, these functions are normally performed by fancy
clientclass methods on your behalf.
- In theory this code shouldn’t break even with future releases, but there will be a better way of performing this request when the gdata-python-client maintainer(s) update the library.
- Very frustrating: The Insight data returned in these CSV files only has the last 7-days of statistics for a given video. This is an artificial limitation enforced by their proprietary security token passed in the generated link. There’s a bit of discussion about this limitation here. Take a second and “star” the feature request to unlock this additional data.
*Downloading videos from YouTube is not officially supported in the API. Youtube-dl.py is the best solution I’ve found as a workaround. It’s well maintained and Python based.