Search code examples
apiyoutube-apigdatagdata-api

Youtube API Batch Request BUG?


I'm retrieving large amounts of data from Youtube (users and videos) via their API, but it is getting slower and slower because my needs are incresing trough the time and the requests apparently must be made individually:

http://gdata.youtube.com/feeds/api/users/ID https://gdata.youtube.com/feeds/api/videos/ID

So I decided to give a try to the batch processing, were you can "theoretically" do the same thing in packs of 50 in order to save a lot of execution time.

https://developers.google.com/youtube/2.0/developers_guide_protocol_batch_processing

I certainly succeeded in this also, but I am having a problem, the data is coming back, but not completely, some info is missing (yt:statistics node), so I surfed the internet looking for a fix and I found this thread on Google:

https://groups.google.com/forum/?fromgroups=#!topic/youtube-api-gdata/YHopv4yJQzk

There is an answer of a developer of the Youtube API team, and they do not look very concerned on this.

Anyone tried this too? Is there any possible solution?

Thanks in advance and excuse my English.


Solution

  • I discovered that my problem was on the XML parsing and not on the API output. I was trying to convert the XML Output to JSON/Array to provide an easier data manipulation but I've got a hard time because of colons in node names and nesting, and any functions I've found over the internet weren't capable of parsing XML data like this.

    I provide some code in case that it might be useful to someone:

        public static function generarXMLBatch($listaIdsYoutube)
        {
           $version = "2";
           $batchUrl = "https://gdata.youtube.com/feeds/api/users/batch?v=".$version;
           $entryUrl = "https://gdata.youtube.com/feeds/api/users/";
    
    
           $xmlDoc = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?>
                        <feed xmlns="http://www.w3.org/2005/Atom"
                                xmlns:media="http://search.yahoo.com/mrss/"
                                      xmlns:batch="http://schemas.google.com/gdata/batch"
                                      xmlns:yt="http://gdata.youtube.com/schemas/2007">
                                       <batch:operation type="query"/></feed>');
    
           foreach($listaIdsYoutube as $idYoutube) {
               $xmlDoc->addChild("entry")->addChild("id", $entryUrl.$idYoutube["perfilYoutube"]."?v=".$version);
           }
    
           //SimpleXML to String
           $xmlDoc = $xmlDoc->saveXML();
           //XML string to CURL
           $xmlDoc = My_Funciones::cURL_XML($batchUrl, $xmlDoc);
           //XML string CURL output to SimpleXML
           $xmlDoc = new SimpleXMLElement($xmlDoc);
    
           return $xmlDoc;
       }
    

    This function receive a Youtube ID list, generates the XML Batch query (you can query up to 50 videos/users... at the same time), and returns the output after the query in SimpleXML format so you can access via:

    $xmlDoc->entry->....

    Greetings