Skip to content

Instantly share code, notes, and snippets.

@mister-ben
Last active October 8, 2015 05:38
Show Gist options
  • Save mister-ben/094d1ecda1e96f77dd02 to your computer and use it in GitHub Desktop.
Save mister-ben/094d1ecda1e96f77dd02 to your computer and use it in GitHub Desktop.
Basic sitemap from Brightcove media API
<?php
/*
* Basic video sitemap generator
*
* Pulls video infromation from Brightcove Media API
*
* If you have a custom field in which you store the URL on your site where the video has been
* added, the script can use that URL in the sitemap.
*
* Otherwise it assumes all videos are to be played back in a single player at same URL. The "bctid"
* URL parameter will be added, which will cause a player on that page to play that video.
*
* For a "real world" implementation of this you may need to cross-reference with your CMS/database
* to determine the page on your site where each video is embedded.
*
* Form will ask for a thumbnail URL. This is used only for videos without a thumbnail.
*
* REQUIRES read API token with URL access.
*
* REQUIRES open source PHP MAPI wrapper from
* http://opensource.brightcove.com/project/php-media-api-wrapper. Download to the server and
* adjust the path in the "require" line after this comment block if necessary.
*
*/
require('inc/bc-mapi.php');
if ( !isset($_GET["accountToken"])
|| !isset($_GET["playerURL"])
|| !isset($_GET["publisherID"])
|| !isset($_GET["playerID"])
|| !isset($_GET["thumbnail"]) )
{
// show form if required input not received
?><!DOCTYPE html>
<html>
<head>
<title>Sitemap</title>
<style type="text/css">
label {float: left; width: 15em; margin-right:1em; text-align: right; }
input.text {width: 40em; clear:both;}
input#submit {margin-left: 19em; }
</style>
</head>
<body>
<form action="sitemap.php">
<label for="accountToken">Brightcove Read Token</label> <input
type="text" name="accountToken" id="accountToken" class="text"> <br>
<label for="customField">Embedded URL custom field *</label><input type="text" name="customField" id="customField" class="text"><br>
<label for="playerURL">Default player URL on site</label> <input type="text" name="playerURL" id="playerURL" class="text"> <br>
<label for="playerID">Viral Player ID</label> <input type="text" name="playerID" id="playerID" class="text"> <br>
<label for="publisherID">Publisher ID</label> <input type="text" name="publisherID" id="publisherID" class="text"> <br>
<label for="thumbnail">Default Thumbnail URL</label> <input type="text" name="thumbnail" id="thumbnail" class="text"> <br>
<input type="checkbox" name="brightcovekk" id="brightcovekk" /><label for="brightcovekk">Brightcove Japan account</label><br />
<input type="submit" value="Submit" id="submit"><br />
<p>This may take some time if you have a lot of content. Please wait, and do not re-submit.</p>
<p><em>* If you have <a href="http://support.brightcove.com/en/video-cloud/docs/overview-using-custom-metadata">custom fields</a> on your account (pro/enterprise) you could use a field to store the URL where you have embedded each video. If you do this, enter the custom field name here.</em></p>
</form>
</body>
</html>
<?php
} else {
header ("Content-Type:application/xml"); ?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" xmlns:bc="http://www.brightcove.com">
<?php
ob_start(); // buffer output
$readToken = $_GET["accountToken"];
$playerURL = $_GET["playerURL"];
$playerID = $_GET["playerID"];
$publisherID = $_GET["publisherID"];
$playerURLfield = $_GET["customField"];
$bc = new BCMAPI($readToken, 'no write token needed');
if (isset($_GET["brightcovekk"]) && ($_GET["brightcovekk"] == "on" )) {
// use Brightcove Japan api
$bc->__set('url_read', 'api.brightcove.co.jp/services/library?');
$bc->__set('url_write', 'api.brightcove.co.jp/services/post');
$serviceDomain = "brightcove.co.jp";
} else {
$serviceDomain = "brightcove.com";
}
$bc->__set('media_delivery', 'http');
$fields = array( 'video_fields' => 'id,name,shortDescription,videoStillURL,publishedDate,FLVURL,length,customFields' );
$videos = $bc->findAll('video',$fields);
foreach ($videos as $video) {
// thumbnail is mandatory. if no thumbnail (live/remote asset?) use default
if (is_null($video->videoStillURL)) {
$thumbnail = $_GET["thumbnail"];
} else {
$thumbnail = $video->videoStillURL;
}
$pubDate = date(DATE_ATOM, round($video->publishedDate/1000));
$length = round($video->length/1000);
echo " <url>\n";
echo " <loc>\n";
if (array_key_exists($playerURLfield, $video->customFields) && !is_null($video->customFields->{$playerURLfield})) {
echo " <![CDATA[{$video->customFields->{$playerURLfield}}]]>\n";
} else {
echo " <![CDATA[{$playerURL}?bctid={$video->id}]]>\n";
}
echo " </loc>\n";
echo " <video:video>\n";
echo " <video:title>\n";
echo " <![CDATA[{$video->name}]]>\n";
echo " </video:title>\n";
echo " <video:description>\n";
echo " <![CDATA[{$video->shortDescription}]]>\n";
echo " </video:description>\n";
echo " <video:player_loc allow_embed=\"true\" autostart=\"autoStart=true\"><![CDATA[http://c.{$serviceDomain}/services/viewer/federated_f9/?isVid=1&isUI=1&playerID={$playerID}&publisherID={$publisherID}&videoId={$video->id}]]>\n";
echo " </video:player_loc>\n";
// if no http url (e.g. live) leave out video_content:loc
if (!is_null($video->FLVURL)) {
echo " <video:content_loc>\n";
echo " <![CDATA[{$video->FLVURL}]]>\n";
echo " </video:content_loc>\n";
}
echo " <video:thumbnail_loc>\n";
echo " <![CDATA[{$thumbnail}]]>\n";
echo " </video:thumbnail_loc>\n";
echo " <video:publication_date>\n";
echo " <![CDATA[{$pubDate}]]>\n";
echo " </video:publication_date>\n";
echo " <video:duration>{$length}</video:duration>\n";
echo " </video:video>\n";
echo " </url>\n";
}
ob_flush(); // send buffer to browser
?>
</urlset><?php } ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment