Skip to content

Instantly share code, notes, and snippets.

@stefansundin
Last active December 17, 2015 00:10
Show Gist options
  • Save stefansundin/5519170 to your computer and use it in GitHub Desktop.
Save stefansundin/5519170 to your computer and use it in GitHub Desktop.
PHP script that converts GData feeds for YouTube Live events to human readable feeds.
<?php
/*
https://gist.github.com/stefansundin/5519170
Converts GData feeds for YouTube Live events to human readable feeds.
http://stefansundin.com/blog/476
https://developers.google.com/youtube/2.0/developers_guide_protocol_retrieving_live_events
Use this url to get data for streams that no longer appear in any of the feeds:
https://gdata.youtube.com/feeds/api/users/CHANNELNAME/live/videos/VIDEOID?v=2
It does not appear to contain all of the info though. Replace CHANNELNAME and VIDEOID.
*/
header('Content-Type: application/atom+xml;charset=utf-8');
$feed_map = array(
'featured' => 'https://gdata.youtube.com/feeds/api/charts/live/events/featured?v=2',
'live_now' => 'https://gdata.youtube.com/feeds/api/charts/live/events/live_now?v=2',
'upcoming' => 'https://gdata.youtube.com/feeds/api/charts/live/events/upcoming?v=2',
'recently_broadcasted' => 'https://gdata.youtube.com/feeds/api/charts/live/events/recently_broadcasted?v=2',
);
if (isset($_GET['id']) && in_array($_GET['id'],array_keys($feed_map))) {
$url = $feed_map[$_GET['id']];
}
else {
$url = $feed_map['featured'];
}
$data = file_get_contents($url);
preg_match("/<updated>.*?<\/updated>/", $data, $updated);
preg_match("/<title>(.*?)<\/title>/", $data, $title);
$title = ucwords($title[1]);
echo <<<END
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>urn:uuid:b72b7d83-271d-4e0d-8102-edb555ee649a</id>
<title>YouTube {$title}</title>
{$updated[0]}
<link href="http://stefansundin.com/blog/476" />
<author>
<name>Stefan Sundin</name>
<uri>http://stefansundin.com/</uri>
</author>
END;
preg_match_all("/<entry [\s\S]*?<\/entry>/", $data, $entries, PREG_SET_ORDER);
foreach ($entries as $m) {
preg_match("/<id>.*?<\/id>/", $m[0], $id);
preg_match("/<updated>.*?<\/updated>/", $m[0], $updated);
preg_match("/<title>(.*?)<\/title>/", $m[0], $title);
preg_match("/<name>.*?<\/name>/", $m[0], $author);
preg_match("/<content .*?\/live\/videos\/(.*?)\?v/", $m[0], $video_id);
preg_match("/<yt:status>(.*?)<\/yt:status>/", $m[0], $status);
$has_start = preg_match("/<yt:when .*?start='(.*?)'/", $m[0], $start);
$has_end = preg_match("/<yt:when .*?end='(.*?)'/", $m[0], $end);
$has_summary = preg_match("/<summary>([\s\S]*?)<\/summary>/", $m[0], $summary);
if (!$has_start) {
$start = array("", "No start time set");
}
if (!$has_end) {
$end = array("", "No end time set");
}
if (!$has_summary) {
$summary = array("", "&lt;i>No description available.&lt;/i>");
}
$summary = preg_replace("/[[:alpha:]]+:\/\/[^<>[:space:]]+[[:alnum:]\/]/", "&lt;a href=\"\\0\">\\0&lt;/a>", $summary[1]);
$summary = str_replace("\n", "&lt;br/>\n", $summary);
echo <<<END
<entry>
{$id[0]}
<title>{$title[1]} ($status[1])</title>
<link href="https://www.youtube.com/watch?v={$video_id[1]}" />
{$updated[0]}
<author>{$author[0]}</author>
<content type="html">
Starts: {$start[1]}&lt;br/>
Ends: {$end[1]}&lt;br/>
&lt;p>&lt;iframe width="853" height="480" src="https://www.youtube.com/embed/{$video_id[1]}" frameborder="0" allowfullscreen>&lt;/iframe>&lt;/p>
{$summary}
</content>
</entry>
END;
}
echo <<<END
</feed>
END;
/*
Example entry:
<entry gd:etag='W/&quot;CE4MR347eCp7I2A9WhBUFkk.&quot;'><id>tag:youtube.com,2008:live:event:tl96Y4vtXhRnMI-4p3KdCDjG2QCfBwzv</id><published>2013-05-01T00:11:01.000Z</published><updated>2013-05-04T04:49:46.000Z</updated><category scheme='http://schemas.google.com/g/2005#kind' term='http://gdata.youtube.com/schemas/2007#liveEvent'/><title>LCS 2013 EU Summer Promotion Tournament D1 (EN)</title><summary/><content type='application/atom+xml' src='https://gdata.youtube.com/feeds/api/users/LoLChampSeries/live/videos/oS6qReoDsxE?v=2'/><link rel='self' type='application/atom+xml' href='https://gdata.youtube.com/feeds/api/users/vqRdlKsE5Q8mf8YXbdIJLw/live/events/tl96Y4vtXhRnMI-4p3KdCDjG2QCfBwzv?v=2'/><author><name>LoL Esports</name><uri>https://gdata.youtube.com/feeds/api/users/LoLChampSeries</uri><yt:userId>vqRdlKsE5Q8mf8YXbdIJLw</yt:userId></author><media:group><media:description type='plain'/><media:title type='plain'>LCS 2013 EU Summer Promotion Tournament D1 (EN)</media:title></media:group><yt:status>completed</yt:status><yt:when end='2013-05-04T04:49:46.000Z' start='2013-05-03T08:32:49.000Z'/></entry>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment