Sure, you can filter the search results with some simple comparisons (see https://developers.google.com/youtube/2.0/developers_guide_protocol_partial), but I was unable to find a wildcard for titles or description/summary searches, similar to SQL's functionality WHERE title LIKE '%bbc%'.
So if you want to search live events, you have at least these two options:
1. Load everything from one live event feed and make a string search over the xml result.
- Select the predefined feed, like "Current live events" (https://gdata.youtube.com/feeds/api/charts/live/events/live_now?v=2&max-results=50&inline=true&prettyprint=true&start-index=1), load 50 results, increment start-index by 50, load again and so on until you have loaded everything. Then make a strpos or regexp find to find the Live Event entries that you are interested in.
- Load the YouTube page with your keywords and Live Event filters enabled (http://www.youtube.com/results?search_query=arirang&filters=live&lclk=live&page=1). Strip the video ids from the resulting HTML. Load detailed info for each found video id, for example (https://gdata.youtube.com/feeds/api/videos/WLqF0kEgMvw?v=2&prettyprint=true). The web page gives you only 20 entries, so you need to increment the page index and load the page again if you want to have more results. This method relies on YouTube webpage's element attributes and therefore is not suitable for any final or commercial productions. Also you have no control over the results: the result set may contain also already ended Live Events, not only currently broadcasted events...
The next code snippet loads the web page url, finds the video ids, and loads the detailed information about the live event with YouTube Data API.
// get your keywords and page index from page request |
// $searchwords = $_REQUEST["searchwords"] |
// $page = $_REQUEST["page"] |
$page = 1; |
$searchwords = 'arirang'; |
// create url |
$search_url = "http://www.youtube.com/results?search_query=" |
."$searchwords&filters=live&lclk=live&page=$page"; |
// load web page contents |
$html = file_get_contents($search_url); |
// now search the content ids |
$ids = array(); // to store the found ids |
$idlength = 11; // length of youtube videoid |
$attr = 'data-context-item-id="'; // video id in the html |
$pos = strpos($html,$attr,0); // pos in html |
while ($pos!==FALSE) { |
$ids[] = substr($html, $pos+strlen($attr), $idlength); |
$pos=strpos($html,$attr,$pos+$idlength); |
} |
// now you can loop the ids and load live events for each id |
foreach ($ids as $id) { |
$xmlurl = "http://gdata.youtube.com/feeds/api/videos/" |
."$id?v=2&prettyprint=true"; |
$xmldata = file_get_contents($xmlurl); |
// parse live event here etc... |
} |
2 comments:
Could this be used to embed currently live YouTube videos?
Hi! Sure, I have not tried it, but I suppose you just need to create embed code using the live event ID.
Post a Comment