Gene Lu's Portfolio

Calling Karaoke

My go-to-band in karaoke

My go-to-band in karaoke

Disclaimer
I’m aware that most New Yorkers, especially the ones in Manhattan, can surf the web on their smartphones and can access the list of songs online, but in this case, I’m going to just throw that assumption out the window. The purpose of this project was to help familiarize me with the syntax and the workflow of Twilio. Enjoy!

Overview
In our Public Interfaces class, Ian Curry introduced us to Twilio, an online tool that “provides a cloud API for voice and SMS communications that leverages existing web development skills, resources and infrastructure.”  For my mini-project, I created an app that allows you to call up the karaoke bar (Duets, NYC) to find out the latest songs available so that you wouldn’t have to spend your time fumbling through the music catalog when you got there.

Try It Out
Before diving into the code, try out the app yourself. Call the following number 415-599-2671 and when prompted for the pin code, enter in 62600490. The program will then go through all the songs and if you would like a song to be repeated, press 1. There’s a bit of lag, but I think that’s just how I programmed the darn thing.

Thanks
The following code was inspired by phonemenu from Twilio. The HTML scraping code was copied and modded from somewhere that I can’t remember, BUT you can always google for that sort of stuff. The page that was scraped for data was from Duet Karaoke’s September 2010 playlist.

Code
There’s obviously room for improvement codewise, which is why there’s a comment section below for you to lend a hand! 😉

Here’s the main file (name it whatever you like as long as it ends in .php). The include file, functions.php, is all the way at the bototm. Place this in the same folder as the main file.

<?php
/*
* This Twilio app strips content from the $url variable and then places the
* data into an array. When the caller contacts the system, each entry in the array is
* announced with the option to repeat the entry.
*/

header(“content-type: text/xml”);
echo “<?xml version=\”1.0\” encoding=\”UTF-8\”?>\n”;

include(“functions.php”);

// Generate data from page and store into array
// @start snippet
// What’s the URL of the page that you want to strip?
$url = “http://www.karaokeduet.com/eng/song/1009.html”;

$raw = file_get_contents($url);
$newlines = array(“\t”,”\n”,”\r”,”\x20\x20″,”\0″,”\x0B”,”’”,”&#146;”,”!”,”&”, “&”);
$content = str_replace($newlines, “”, html_entity_decode($raw));

$start = strpos($content,'<table width=”520″ border=”1″ cellpadding=”0″ cellspacing=”0″ bordercolor=”#CCCCCC”>’);
$end = strpos($content,'</table>’,$start) + 8;
$table = substr($content,$start,$end-$start);

preg_match_all(“|<tr(.*)</tr>|U”,$table,$rows);

// Get the value for the number pressed and the songIndex that we are currently on
// and get values (artist, title, and song) from extracted rows ($rows)
$stringData = $_REQUEST[‘Digits’];
$songIndex = $_REQUEST[‘songIndex’];
preg_match_all(“|<TD(.*)</TD>|U”,$rows[0][$songIndex],$cells);

$artist = strip_tags($cells[0][0]);
$title = strip_tags($cells[0][1]);
$song = strip_tags($cells[0][2]);
// @end snippet

// @start snippet
// Switch statement that either repeats the song that you are currently on or goes to the next song.
echo ‘<Response>’;
switch($stringData) {
// If “1” is pressed, we redirect back to this script with the current $songIndex
case 1:
?>
<Redirect>
<?php echo ‘http://’.dirname($_SERVER[“SERVER_NAME”].$_SERVER[‘PHP_SELF’]).’/duet0910.php?songIndex=’.$songIndex; ?>
</Redirect>
<?php
break;

default:
// If we have not passed the script a $songIndex variable, play the menu.
if($songIndex == NULL){ ?>
<Say>The Latest Songs at Duet</Say>
<Say>Press 1 to repeat the song</Say>

<?php
preg_match_all(“|<TD(.*)</TD>|U”,$rows[0][$songIndex],$cells);

$artist = strip_tags($cells[0][0]);
$title = strip_tags($cells[0][1]);
$song = strip_tags($cells[0][2]);
}

// Checking for NULL entries in the $rows array
if(isset($rows[0][$songIndex]) || !is_null($_REQUEST[‘Digits’])){ ?>
<Gather action=”<?php echo ‘http://’ . dirname($_SERVER[“SERVER_NAME”] .  $_SERVER[‘PHP_SELF’]) . ‘/duet0910.php?songIndex=’.$songIndex; ?>” >
<?php announceSong($artist, $title, $song);
$songIndex++; ?>
</Gather>
<Redirect>
<?php echo ‘http://’.dirname($_SERVER[“SERVER_NAME”].$_SERVER[‘PHP_SELF’]).’/duet0910.php?songIndex=’.$songIndex; ?>
</Redirect>
<?php } else {
$songIndex++; ?>
<Redirect>
<?php echo ‘http://’.dirname($_SERVER[“SERVER_NAME”].$_SERVER[‘PHP_SELF’]).’/duet0910.php?songIndex=’.$songIndex; ?>
</Redirect> <?php } // end else
break;
}
?>

</Response>

And here’s the code for functions.php.

<?php

function announceSong($artist, $title, $song){
echo ‘<Say>’;
echo $artist.’,’.$title .’,’.$song.’… Next Song’;
echo ‘</Say>’;
}

?>

Comments are closed.

Information

  • 19 Nov 2010

Filed Under

Tags

Recent Articles

Lead UX Designer on Nike+ Running at R/GA.