YouTube API Continued: Editing, Deleting, Retrieving Meta Data

Published on September 11, 2009

YouTube API Continued: Editing, Deleting, Retrieving Meta Data

    A few days ago I published a post about uploading videos to YouTube through the system API. Today I want to introduce a sequel in which I will talk about editing already downloaded videos, obtaining their status and deleting.

    It is perhaps worth noting once again that all the code is provided in a certain dialect of PHP and can be easily transformed and customized to your needs. The previous post caused a large number of responses to personal mail, ICQ, and even a job offer, so the continuation, as I think, is more than appropriate.

    The first point I will mention the fact that iexx spoke about in the comments . Namely, the Zend_Gdata class was mentioned . The class really solves all the problems described, 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, in the documentation, depending on the necessary action, the methods for sending data change - it can be PUT, POST, DELETE or GET - I recommend paying special attention to this.

    As everyone knows, yes, I mentioned this in a previous post, each video posted on YouTube gets its own unique 11-digit code, for example eTpTy2Kvj4o, right after the video is uploaded. Knowing this code allows you to get the necessary information about the video from the server. Any downloaded video goes through the encoding procedure on video hosting servers, and the time it takes to complete this operation depends on the load on the hosting servers, and the actual size of the video.

    Until the video is encoded and receives the appropriate status, for obvious reasons we will not be able to edit it or receive meta-data about the posted video.
    To get the meta-data and the status value of the video, you need to get GET to the gdata.youtube.com/feeds/api/videos/VIDEO page , for example http://gdata.youtube.com/feeds/api/videos/eTpTy2Kvj4o ( look in the browser). If the server returns a 404 error, this may mean 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. In 15-20 minutes everything will fall into place.

    The server response is presented in the form of a piece of XML in Atom format (see the link above), we can extract meta data from it, for example, name, author name, video size, tags, category, get an array of thumbnails, and so on. That is, retrieving video data comes down to the trivial task of parsing XML.

    The video editing procedure is very similar to the procedure for saving meta data about the video before downloading. First, we generate XML, which lists the name, description, category and tags: "; After that, send this piece to gdata.youtube.com / feeds / api / users / YOUTUBE_USERNAME / uploads / VIDEO CODE using the PUT method , for example, like this:

    $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>





    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 sent! Again, I deliberately bypass the issue of error handling, leaving this to the conscience of the programmer. The value of API_NAME, API_KEY and others - see in the last post.

    And finally, the removal of 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: Well, that’s actually all that 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 the documentation.

    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);
    }