Site : Intrabang (XSPF) - Web App

2

picture-13

API - Twitter API integration

Launched on April 14th 2009

Role - Founder, Web Designer, Web Developer

Built with xHTML, PHP, XSPF, XML and CURL

Visit http://www.Intrabang.com

Intrabang is a online radio station powered by twitter requests. The idea came after reading about xspf feeds (read more here : Music + Playlist + XML) and also wanting to create something with twitter API.

The site was built over the bank holiday weekend (10th - 13th April 2009) for no other reason than for me to have a play with new coding techniques and to brush up on my XML and X-path.

Intrabang parses the atom feed from the twitter API, uses php to cherry pick the relevant info, uses curl to check the mp3 exist and then recompiles it as an xspf feed. The CURL operation can take a few seconds so this script is set to run every 5 mins. There for the tracklist can be up to 5 mins out of date. The generated xspf feed is called by the player, creates the playlists and loads the tracks.

The site is young and still in very much in Alpha. There is plenty than can be done to improve it (bugging, proofing, data storage, simpler tweeting procedure) which will happen over time.

some sites that I referenced heavily include:

Twitter API

Simple XML processing

Simple XSPF Player

XSPF Jukebox Player

XSPF Documentation

Sam’s “is the Mp3 there?” checker

Skreemr music search

Parser PHP which is the engine of the Web App. You can use it as template or tutorial on XML/XSPF:


  //Loads Twitter atom API feed
  $feed =  simplexml_load_file('http://search.twitter.com/search.atom?q=%40intrabang&rpp=30&page=1');
  $children =  $feed->children('http://www.w3.org/2005/Atom');

  //Creates new XML Document
  $doc = new DOMDocument();
  $doc->formatOutput = true;

  //Sets first element as Playlist with name space of http://xspf.org/ns/0/
  $t = $doc->createElement( "playlist");
  $doc->xmlns="http://xspf.org/ns/0/";
  $doc->appendChild( $t );

  //Sets child element as trackList
  $r = $doc->createElement( "trackList" );
  $doc->appendChild( $r );

  //This pulls info out of the twitter atom API feed
  $entries = $children->entry;
  foreach ($entries as $entry)
  {

	    //Sets details to be each child element of Twitter atom API feed
  		$details = $entry->children('http://www.w3.org/2005/Atom');

	    //Sets description to be main content Twitter atom API feed - we need to pull correct info out of this
	  	$description = $details->content->asXML();

        //Cleans description
		$description = str_ireplace("<a href=\"http://twitter.com/intrabang\"><b>@intrabang</b></a>", "", 					$description);
		$description = str_ireplace("", "", $description);

		//Finds Mp3 link and cleans it
		$link = stristr($description, 'http://');
		$link = htmlentities($link);     

        //Finds Artist start of text
		$artist = stristr($description, 'A:');
		//Finds Artist end of text
		$artist = substr($artist, 0, strpos($artist, "T:"));
		$artist = str_ireplace("A:", "", $artist);
		//Removes whitespace
		$artist = trim($artist);
		//Cleans artist
		$artist = htmlentities($artist);  

        //Finds Track name start of text
		$track = stristr($description, 'T:');
		//Finds Track name end of text
		$track = substr($track, 0, strpos($track, "F:"));
		$track = str_ireplace("T:", "", $track);
		//Removes whitespace
		$track = trim($track);
		//Cleans Track name
		$track = htmlentities($track);

        //Pulls Twitter Username out (already clean)
		$user =  $details->author->name;
		//Removes the (Firstname Lastname)
		$user = substr($user, 0, strpos($user, "("));

		//Pulls Twitter account link out (already clean)
		$userlink =  $details->author->uri;

	    //CURL checks header of Mp3 link
        $c3 = curl_init();
		curl_setopt($c3, CURLOPT_URL, $link);
		curl_setopt($c3, CURLOPT_HEADER, true);
		curl_setopt($c3, CURLOPT_NOBODY, true);
		curl_setopt($c3, CURLOPT_RETURNTRANSFER, true);
		$header = curl_exec($c3);
		curl_close($c3);

		//If header contains audio/mpeg anywhere in the content and if exists writes XSPF XML
		if(strpos($header, "Content-Type: audio/mpeg"))
		{

			$b = $doc->createElement( "track" );

  			$location = $doc->createElement( "location" );
  			$location->appendChild(
  			$doc->createTextNode( $link )
  			);
  			$b->appendChild( $location );

  			$creator = $doc->createElement( "creator" );
  			$creator->appendChild(
  			$doc->createTextNode( $artist )
  		    );
  			$b->appendChild( $creator );

  			$album = $doc->createElement( "album" );
  			$album->appendChild(
  			$doc->createTextNode( "The Twitter List" )
  			);
  			$b->appendChild( $album );

  			$title = $doc->createElement( "title" );
  			$title->appendChild(
  			$doc->createTextNode( $track )
  			);
  			$b->appendChild( $title );

  			$annotation = $doc->createElement( "annotation" );
  			$annotation->appendChild(
  			$doc->createTextNode( $user )
  			);
  			$b->appendChild( $annotation );

  			$duration = $doc->createElement( "duration" );
  			$duration->appendChild(
  			$doc->createTextNode( "" )
  		   	);
  			$b->appendChild( $duration );

  			$image = $doc->createElement( "image" );
  			$image->appendChild(
  			$doc->createTextNode("default.png")
  			);
  			$b->appendChild( $image );

  			$info = $doc->createElement( "info" );
  			$info->appendChild(
  			$doc->createTextNode( $userlink )
  			);
  			$b->appendChild( $info );

  			$r->appendChild( $b );
            $t->appendChild( $r );

  		}
  }

  //Writes and saves XSPF XML
  echo $doc->saveXML();
  $doc->save("xspf.xml") 

Screenshots:

to “Site : Intrabang (XSPF) - Web App”

-->
  1. Website Design Kent

    Well done. some excellent coding and a useful Twitter API

  2. callum

    Thanks WDK. The only problem is it that Twitter Search API resets every week or two so the site brakes if songs aren’t continually added. I will tighten it up when I have a few moments by putting some default tunes on it.

Leave a Reply

Name

Website

Comment