A few days ago I published a
post about uploading videos to YouTube through the system API. Today I want to present a sequel, in which it will be told about editing already downloaded videos, getting their status and deleting.
It is worth, perhaps, once again to note that all the code is in some PHP dialect and can easily be transformed and customized for your needs. The previous post caused a large number of responses to personal mail, ICQ, and even a job offer, so I think it’s more than appropriate to continue.
The first point I will note is a fact that
iexx said in the comments. Namely, there was, the class
Zend_Gdata was mentioned. The class really solves all the described tasks, with my posts I just show what the YouTube API is capable of and demonstrate examples of working code.
Almost all requests are sent to the
gdata.youtube.com handler, the documentation changes the methods of sending data depending on the required action - it can be PUT, POST, DELETE or GET - I recommend paying special attention to this.
')
As everyone knows, yes, I mentioned this in the last post, each video posted on YouTube receives its unique 11-digit code, for example, eTpTy2Kvj4o, immediately after downloading the video. Knowing this code allows you to get the necessary information about the video from the server. Any downloaded video passes the encoding procedure on the video hosting servers, and the time it takes to perform this operation depends on the workload of the hosting servers and the actual video size.
Until the video is encoded and receives the corresponding status, we will not be able to either edit it or receive metadata about the posted video for obvious reasons.
To get the meta-data, and the status of the video you need to refer to Gd`om page
gdata.youtube.com/feeds/api/videos/KOD_VIDEO , for example
http://gdata.youtube.com/feeds/api/videos/eTpTy2Kvj4o ( look in the browser). If the server returns a 404 error, it may indicate that the video does not exist, or it has not yet been encoded. Sometimes, in your YouTube account you can see the video, but through the GData interface it will not be available yet - it happens. After 15-20 minutes everything will fall into place.
The server response is represented as a piece of XML in Atom format (see the link above), we can pull out meta data from it, for example, title, author name, video size, tags, category, get an array of thumbnails and so on. That is, getting data about the video comes down to the trivial task of parsing XML.
The procedure for editing video is very similar to the procedure for saving meta-data about a video before downloading. First, we generate XML, which lists the name, description, category and tags:
$data = "<?xml version='1.0'?>
<entry xmlns='http://www.w3.org/2005/Atom'
xmlns:media='http://search.yahoo.com/mrss/'
xmlns:yt='http://gdata.youtube.com/schemas/2007'>
<media:group>
<media:title type='plain'>$name</media:title>
<media:description type='plain'>".$descr."</media:description>
<media:category scheme='http://gdata.youtube.com/schemas/2007/categories.cat'>".$category."</media:category>
<media:keywords>".$tags."</media:keywords>
</media:group>
";
After this, using the PUT method, we send this piece to
gdata.youtube.com / feeds / api / users / YOUTUBE_USERNAME / uploads / VIDEO CODE, for example in the following way:
if ($fp = fsockopen ("gdata.youtube.com", 80, $errno, $errstr, 20))
{
$request ="PUT /feeds/api/users/YOUTUBE_USERNAME/uploads/_" HTTP/1.1\r\n";
$request.="Host: gdata.youtube.com\r\n";
$request.="Content-Type: application/atom+xml; charset=UTF-8\r\n";
$request.="Content-Length: ".strlen($data)."\r\n";
$request .="Authorization: GoogleLogin auth= AUTH_TOKEN \r\n";
$request.="X-GData-Client: API_NAME \r\n";
$request.="X-GData-Key: key=API_KEY\r\n";
$request.="\r\n";
$request.=$data."\r\n";
socket_set_timeout($fp, 10);
fputs($fp,$request,strlen($request));
}
All data is sent! Again, I deliberately bypass the issue of error handling, leaving it to the programmer’s conscience. The value of API_NAME, API_KEY and others - see in the last post.
And finally, delete the video. Here, again, everything is very simple, only the “data delivery” method is changing, this time it is DELETE. The code in this case will be something like this:
if ($fp = fsockopen ("gdata.youtube.com", 80, $errno, $errstr, 20))
{
$request ="DELETE /feeds/api/users/YOUTUBE_USERNAME/uploads/_" HTTP/1.1\r\n";
$request.="Host: gdata.youtube.com\r\n";
$request.="Content-Type: application/atom+xml; charset=UTF-8\r\n";
$request .="Authorization: GoogleLogin auth=AUTH_TOKEN\r\n";
$request.="X-GData-Client: API_NAME \r\n";
$request.="X-GData-Key: key=API_KEY\r\n";
$request.="\r\n";
socket_set_timeout($fp, 10);
fputs($fp,$request,strlen($request));
fclose($fp);
}
Well, that's all I wanted to talk about today in particular, and about the YouTube API in general. The remaining tasks are completely solvable and seem simple when reading documentation.