A Wired.com user account lets you create, edit and comment on Webmonkey articles. You will also be able to contribute to the Wired How-To Wiki and comment on news stories at Wired.com.
It's fast and free.
processing...Retrieve Sign In
Please enter your e-mail address or username below. Your username and password will be sent to the e-mail address you provided us.
processing...Welcome to Webmonkey
- edit articles
- add to the code library
- design and write a tutorial
- comment on any Webmonkey article
Sign In Information Sent
YouTube Tutorial Lesson 2 - The Data API
/skill level/
/viewed/
m |
|||
| (2 intermediate revisions not shown.) | |||
| Line 1: | Line 1: | ||
In the first lesson of this tutorial, we looked at the YouTube Player API, a handy tool which allows you to customize, skin and otherwise control the playback of YouTube videos you embed on your website. | In the first lesson of this tutorial, we looked at the YouTube Player API, a handy tool which allows you to customize, skin and otherwise control the playback of YouTube videos you embed on your website. | ||
| - | If you're just joining us, or if you're looking for instructions on how to customize the playback of your embedded videos, then we'd suggest you have a look at Lesson 1. | + | If you're just joining us, or if you're looking for instructions on how to customize the playback of your embedded videos, then we'd suggest you have a look at [[Tutorial:YouTube Tutorial Lesson 1 - The Player API | Lesson 1]]. |
For those who want to dig deeper, welcome! It's time to explore the YouTube Data API, which you can use to request and store info about movies you'd like to display on your site. You can pull in not only the videos themselves from YouTube, but also important metadata like the video title, tags, description, duration and so on. | For those who want to dig deeper, welcome! It's time to explore the YouTube Data API, which you can use to request and store info about movies you'd like to display on your site. You can pull in not only the videos themselves from YouTube, but also important metadata like the video title, tags, description, duration and so on. | ||
| Line 35: | Line 35: | ||
== Working with the YouTube Data API == | == Working with the YouTube Data API == | ||
| - | The first thing we need to do is construct an instance of the YouTube Data service. | + | The first thing we need to do is construct an instance of the YouTube Data service. Enter this code at the prompt: |
<pre> | <pre> | ||
| Line 41: | Line 41: | ||
</pre> | </pre> | ||
| - | That's a generic object | + | That's a generic object with no authentication, so we can only retrieve public feeds. But for our purposes, that's all we need. First, let's write a function that can parse the data we'll be returning. |
| - | Create a new text file named youtube_client.py and paste in this code: | + | Create a new text file named <code>youtube_client.py</code> and paste in this code: |
<pre> | <pre> | ||
| Line 68: | Line 68: | ||
</pre> | </pre> | ||
| - | + | Obviously, if you want to store the data you're about to grab, you will need to rewrite the <code>print_items</code> function to do something other than just print out the data. But for the sake of example, and because there are a near infinite number of ways your database could be structured, we'll just stick with a simple print function for now. | |
| - | So make sure | + | So make sure <code>youtube_client.py</code> is on your PythonPath and fire up Python again, then input these lines: |
<pre> | <pre> | ||
| Line 78: | Line 78: | ||
</pre> | </pre> | ||
| - | The last line should produce a barrage of output as the client prints out a list of most linked videos and all the associated data. To get that list we used one of the YouTube service modules built-in methods <code>GetMostLinkedVideoFeed()</code>. | + | The last line should produce a barrage of output as the client prints out a list of most linked videos and all of the associated data for each clip. To get that list, we just used one of the YouTube service modules' built-in methods, <code>GetMostLinkedVideoFeed()</code>. |
| - | + | While that's all well and good if you want the most linked videos on the entire YouTube site, what if you want something more specific? Say, ''your'' uploaded videos? | |
| - | To | + | To get that list, we're going to use another method of YouTube service module: the <code>GetYouTubeVideoFeed()</code> method. |
| - | First, find the video feed | + | |
| + | == Getting a List of Your Uploaded Videos == | ||
| + | |||
| + | First, find the video feed URL for your YouTube account, which should look something like this: | ||
<pre> | <pre> | ||
| Line 90: | Line 93: | ||
</pre> | </pre> | ||
| - | + | Next, plug that into our already running client with these two lines: | |
<pre> | <pre> | ||
| Line 101: | Line 104: | ||
== Conclusion == | == Conclusion == | ||
| - | Hopefully this has given you some insight into how | + | Hopefully, this lesson has given you some insight into how YouTube's Data API operates. We've really just scratched the surface -- dozens of methods are available to retrieve all sorts of data -- see the [Python YouTube Data API guide] for more details. |
| - | While we've used Python, the methods and techniques are essentially the same for all the client libraries, so you should be able to interact with YouTube | + | While we've used Python, the methods and techniques are essentially the same for all the client libraries, so you should be able to interact with YouTube using a language you're comfortable with. |
| - | Obviously you'll | + | Obviously you'll want to adjust the <code>print_items()</code> function we demonstrated to do something more useful than just printing the results. Like what, you ask? |
| - | For full automation, write a shell script to call the methods we used above and attach the script to a cron job. | + | If you're using Django, you could create a model to hold all the data and then use the model's <code>get_or_create()</code> method to plug the data in via <code>print_items()</code>. For full automation, you could also write a shell script to call the methods we used above and attach the script to a cron job. |
| - | + | Anyway, there you have it -- an easy way to add YouTube videos to your own personal site with no manual labor on your end. | |
Current revision
In the first lesson of this tutorial, we looked at the YouTube Player API, a handy tool which allows you to customize, skin and otherwise control the playback of YouTube videos you embed on your website.
If you're just joining us, or if you're looking for instructions on how to customize the playback of your embedded videos, then we'd suggest you have a look at Lesson 1.
For those who want to dig deeper, welcome! It's time to explore the YouTube Data API, which you can use to request and store info about movies you'd like to display on your site. You can pull in not only the videos themselves from YouTube, but also important metadata like the video title, tags, description, duration and so on.
Contents |
Pick a Library
A variety of client libraries are available for the YouTube Data API, including:
We'll be using Python for this tutorial. If you're a PHP whiz or an old-school Java-head, you needn't worry -- the general concepts we're going to cover will be the same no matter which language you use.
Get Started
Let's say you frequently post movies to YouTube and you're tired of cutting and pasting the embed code to get them to show up on your site.
Using the YouTube Data API and some quick Python scripts, we can grab all our movies -- along with the metadata associated with the clips -- and automatically add them our database. For instance, if you followed along with Webmonkey's Django tutorial series, this technique would be a handy way to add YouTube to your list of data providers.
To get started, go ahead and download the Python YouTube Data Client Library. Follow the instructions for installing the Library as well as any required dependencies (in this case, ElementTree, which is only necessary if you aren't running Python 2.5).
Just to make sure you've got everything set up correctly, fire up a terminal window, start Python and try importing the modules we need:
>>> import gdata.youtube >>> import gdata.youtube.service
If those imports worked, you're ready to start grabbing data.
Working with the YouTube Data API
The first thing we need to do is construct an instance of the YouTube Data service. Enter this code at the prompt:
yt_service = gdata.youtube.service.YouTubeService()
That's a generic object with no authentication, so we can only retrieve public feeds. But for our purposes, that's all we need. First, let's write a function that can parse the data we'll be returning.
Create a new text file named youtube_client.py and paste in this code:
import gdata.youtube
import gdata.youtube.service
class YoutubeClient:
def __init__(self):
self.yt_service = gdata.youtube.service.YouTubeService()
def print_items(self, entry):
print 'Video title: %s' % entry.media.title.text
print 'Video published on: %s ' % entry.published.text
print 'Video description: %s' % entry.media.description.text
print 'Video category: %s' % entry.media.category[0].text
print 'Video tags: %s' % entry.media.keywords.text
print 'Video flash player URL: %s' % entry.GetSwfUrl()
print 'Video duration: %s' % entry.media.duration.seconds
print '----------------------------------------'
def get_items(self, feed):
for entry in feed.entry:
self.print_items(entry)
Obviously, if you want to store the data you're about to grab, you will need to rewrite the print_items function to do something other than just print out the data. But for the sake of example, and because there are a near infinite number of ways your database could be structured, we'll just stick with a simple print function for now.
So make sure youtube_client.py is on your PythonPath and fire up Python again, then input these lines:
>>> from youtube_client import YoutubeClient >>> client = YoutubeClient() >>> client.get_items(client.yt_service.GetMostLinkedVideoFeed())
The last line should produce a barrage of output as the client prints out a list of most linked videos and all of the associated data for each clip. To get that list, we just used one of the YouTube service modules' built-in methods, GetMostLinkedVideoFeed().
While that's all well and good if you want the most linked videos on the entire YouTube site, what if you want something more specific? Say, your uploaded videos?
To get that list, we're going to use another method of YouTube service module: the GetYouTubeVideoFeed() method.
Getting a List of Your Uploaded Videos
First, find the video feed URL for your YouTube account, which should look something like this:
http://gdata.youtube.com/feeds/api/users/YOURUSERNAME/uploads
Next, plug that into our already running client with these two lines:
url = 'http://gdata.youtube.com/feeds/api/users/YOURUSERNAME/uploads' client.get_items(client.yt_service.GetYouTubeVideoFeed(url))
You should see a list of all your recently uploaded videos, along with all the metadata we plugged into our print_items() function.
Conclusion
Hopefully, this lesson has given you some insight into how YouTube's Data API operates. We've really just scratched the surface -- dozens of methods are available to retrieve all sorts of data -- see the [Python YouTube Data API guide] for more details.
While we've used Python, the methods and techniques are essentially the same for all the client libraries, so you should be able to interact with YouTube using a language you're comfortable with.
Obviously you'll want to adjust the print_items() function we demonstrated to do something more useful than just printing the results. Like what, you ask?
If you're using Django, you could create a model to hold all the data and then use the model's get_or_create() method to plug the data in via print_items(). For full automation, you could also write a shell script to call the methods we used above and attach the script to a cron job.
Anyway, there you have it -- an easy way to add YouTube videos to your own personal site with no manual labor on your end.
|
Lesson 1: The Player API |
- This page was last modified 22:15, 4 September 2008.
Special Offer For Webmonkey Users
WIRED magazine:
The first word on how technology is changing our world.
