Instantly share code, notes, and snippets.

Embed
What would you like to do?
Timed Text from Tweets
<?php
$headerTT = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<tt xmlns=\"http://www.w3.org/2006/10/ttaf1\" xmlns:ttp=\"http://www.w3.org/2006/10/ttaf1#parameter\" ttp:timeBase=\"media\" xmlns:tts=\"http://www.w3.org/2006/10/ttaf1#style\" xml:lang=\"en\" xmlns:ttm=\"http://www.w3.org/2006/10/ttaf1#metadata\">\n<head>\n<metadata>\n<ttm:title>\nTwitter Subtitles\n</ttm:title>\n</metadata>\n<styling>\n<style id=\"s0\" tts:backgroundColor=\"black\" tts:fontStyle=\"normal\" tts:fontSize=\"16\" tts:fontFamily=\"sansSerif\" tts:color=\"white\" />\n</styling>\n</head>\n<body tts:textAlign=\"center\" style=\"s0\">\n<div>";
$footerTT = "</div>\n</body>\n</tt>";
// if form submitted created xml
if (isset($_POST['url'])) {
// load Simplepie - rss reader
require_once('scripts/simplepie.php');
$url = $_POST['url'];
$sPg = $_POST['startPage'];
$ePg = $_POST['endPage'];
if ($ePg < $sPg) $ePg = $sPg;
$rss = new SimplePie();
// Read pages of twitter search results (Dear Twitter, I'm also using Simplepie to cache results)
$j=0;
for ($x=$sPg; $x<=$ePg; $x++){
$pgA[$j] = $url."&page=".$x."&rpp=100";
$j++;
}
$rss->set_feed_url($pgA);
$rss->enable_cache(true);
$rss->init();
$rss->handle_content_type();
if (!$rss->error()) {
// for each rss item collect details (simplepie makes this very easy)
$idx=0;
foreach ($rss->get_items() as $item) {
$date = strtotime($item->get_date('d M Y H:i:s'));
$i[$idx]['date'] = $date;
$i[$idx]['content'] = $item->get_content();
$i[$idx]['author'] = preg_replace('` \([^\]]*\)`', '', $item->get_author()->get_name());
$i[$idx]['link'] = $item->get_link(0);
// if a start tweet has been specified collect date/time
if ($item->get_link(0) == $_POST['startTweet']) {
$startTime = $date;
}
// likewise for end
if ($item->get_link(0) == $_POST['stopTweet']) {
$endTime = $date;
}
$idx++;
}
// if no end time use first tweet (rss in reverse date order)
if (!isset($endTime)){
$endTime = $i[0]['date'];
}
if (isset($i)){
// flip the array around oldest first
sort($i);
// first value is now oldest
if (!isset($startTime)) {
$startTime = $i[0]['date'];
}
// loop through array or results
foreach ($i as $k => $v){
// collect time of current tweet
$timestamp = $i[$k]['date'];
// see if it is in time range if so add it to XML file
if (($timestamp - $startTime>=0) && ($timestamp - $endTime)<=0){
$tt = $tt. "<p style=\"s1\" begin=\"".gmdate('H:i:s', $timestamp-$startTime)."\" id=\"p".$k."\" end=\"".gmdate('H:i:s', $i[$k+1]['date']-$startTime)."\">";
$tt = $tt. $i[$k]['author'].": ".strip_tags($i[$k]['content'])." (@".gmdate('H:i', $timestamp).")</p>\n";
}
}
// All done, push results out to browser, force download
header("Content-type:text/xml");
header("Content-Disposition:attachment;filename=_prepared.xml");
echo $headerTT.$tt.$footerTT;
} else {
echo "Twitter returned no results. <a href='index.php'>Click here to go back and try a new search</a>";
}
} else {
echo $rss->error();
}
}
if (!isset($_POST['url'])){ // Yes I'm a hack programmer ;)
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><meta name="description" content="Tool for creating new subtitle files for BBC iPlayer from twitter search results. The user creats a custom twitter search and the application creates a W3C Timed Text Authoring Format XML file used in the BBC Desktop iPlayer. Created by mhawksey at JISC RSC Scotland North & East">
<meta name="keywords" content="twitter, BBC, iPlayer, subtitle, xml, tool, create, JISC, RSC, Timed Text">
<title>Twitter Powered Subtitles for BBC iPlayer</title>
</head>
<body onLoad="updateText();">
<script language="javascript">
// function to build live link
function updateText(){
hTag = document.aForm.aTag.value;
aDate = document.aForm.aDate.value
aUrlEnd = escape(hTag+'+since:'+aDate+'+until:'+aDate)+'&rpp=100';
aUrl = 'http://search.twitter.com/search?q='+aUrlEnd;
aUrlRSS = 'http://search.twitter.com/search.atom?q='+aUrlEnd;
document.aForm.url.value = aUrlRSS;
document.getElementById('twURL').innerHTML='<a href="'+aUrl+'" target="_blank" >'+aUrl+'</a>';
}</script>
<h3>Twitter Powered Subtitles for BBC iPlayer</h3>
<div style="border:1px solid #CCCCCC; padding:5px;margin:5px; ">
<div style="float:right; text-align:center; border:1px solid #CCCCCC; padding:5px;margin:5px;"><img src="http://www.rsc-ne-scotland.ac.uk/images/RSCScotNE.png" alt="JISC RSC Scotland North &amp; East"><br />Created by m.hawksey at<br/>JISC RSC Scotland North &amp; East
</div>
<p> For more information on this tool (and source code) see my related post on <a href="http://www.rsc-ne-scotland.org.uk/mashe/2010/02/twitter-powered-subtitles-for-bbc-iplayer/">Twitter powered subtitles for BBC iPlayer</a>. This work is based on Tony Hirst's Twitter Subtitle tool concept (See <a href="http://ouseful.wordpress.com/2009/03/17/easier-twitter-powered-subtitles-for-youtube-movies/" rel="bookmark" title="Permanent Link to &quot;Easier Twitter Powered Subtitles for Youtube&nbsp;Movies&quot;">Easier Twitter Powered Subtitles for Youtube&nbsp;Movies</a>).</p>
<p>The basic concept is to enter the date of the live transmission and any search terms to narrow down related tweets. Do some tinkering to find the twitter search pages that cove the broadcast, then generate a new subtitle file '_prepared.xml'. </p>
<p>The broadcast you download from iPlayer will be stored in a folder (something like My Documents] &gt; [My Videos] &gt; [BBC iPlayer] &gt; [repository] &gt; [<em>obscure-broadcast-folder-name- like-b00qx4t0</em>]), locate this folder and replace the file which ends '_prepared.xml' with the one created by this tool keeping the obscure file name ie <em>b00qx4t0_prepared.xml. </em>When you replay the broadcast turn subtitles on to see the tweets. (It might be possible that not all iPlayer programmes have a subtitle file. In this case I imagine you can just drop the created file in appending it with the folder name.) </p>
<p>[In step 4 you can optionally enter the URL of the tweet you want to be the <em>first</em> subtitle (that is, copy the <em>View Tweet</em> link/URL for the tweet you want to use as the first subtitle) and/or the the URL of the tweet you want to use as the <em>last</em> subtitle.]<br style="clear:both; "/>
</p>
</div>
<form method="post" name="aForm" style=" line-height:1.2em; border:1px solid #CCCCCC; padding:5px;margin:5px; ">
<p>1. Enter Date:
<input name="aDate" type="text" id="aDate" value="<?php echo date("Y-m-d", strtotime("last Saturday"));?>" onChange="updateText();">
(in YYYY-MM-DD format) and search/hashtag:
<input name="aTag" type="text" id="aTag" onChange="updateText();" value="#bbcrevolution -watching -RT" size="55">
<a href="javascript:alert('I just needed you to click here to update the url used in step 2 ;-)');">
<input name="url" type="hidden" id="url">
click here</a><br>
2. Open this url: <span id="twURL">(Javscript needs to be enabled)</span> and find the page of the first and last tweet <br>
3. Set the page number of last tweet:
<select name="startPage" id="startPage">
<option value="1" selected>1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
</select>
and first tweet
<select name="endPage" id="endPage">
<option value="1" selected>1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
</select>
(The Twitter Search is displayed newest first)<br/>
4. URL of <em>first</em> Tweet subtitle (optional):
<input type='text' name='startTweet' size='55' value='http://twitter.com/mhawksey/statuses/9397339056'>
<br/>
&nbsp;&nbsp;&nbsp;&nbsp;URL of <em>last</em> Tweet subtitle (optional):
<input type='text' name='stopTweet' size='55' value='http://twitter.com/mhawksey/statuses/9399267077'>
<br>
<br>
<input name="Reset" type='reset' onClick='clearForm()' value='Reset'/>
<input value='Generate subtitles' type='submit'/>
</p>
</form>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-2382132-2");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>
</html>
<?php } ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment