Find Out What Was Playing on the Radio





Find Out What Was Playing on the Radio

If you know what time you were listening to the radio, you can find out what song you were listening to using a web service.

"Record Radio Shows" [Hack #20] highlighted a subscription-based web application that provides a program guide for upcoming terrestrial (and Internet) radio. However, with a different web service you can answer the question "What was that song?" for a day after the program ran.

Yes, Inc. (http://www.yes.com) licenses song-by-song playlist information and provides it for the U.S. radio market. Using the drop-downs on the Yes.com main page, you can select a city, a local station, and then any airtime in the last 24 hours. The site then displays the five songs or programs that played nearest that time, giving you the option to purchase the tracks from either Amazon.com or eBay (Figure).

To use this service, you simply need to know what station you were listening to, and at what time.

Hacking the Hack

If you have a car PC running, you can use this approach to bookmark songs you hear on your car radio. Simply add an application to your PC that notes the time, and whenever you hear a song you like, click that application. In Linux, it is trivial to tie a button to a shell script to do this. For example, the following command will append the time and date to a text file:

	# date >>yestimes.txt 

A similar device could be easily whipped up for Windowsfor example, a Visual Basic application with one button that appends the time and date to a logfile.

Yes.com's program listing page


An even better solution is to add such a button to an open source radio controller application. Then you can note not only the time but also the station that is playing, simplifying the process of identifying that song in the future.

If you are really adventurous, through creative screen-scraping, you can get the track names of the songs playing on the radio in real time (assuming you have a mobile Internet connection). Figure contains a Python script that can access Yes.net information (courtesy of Raffi Krikorian).

Figure. Screen scraping script for Yes.net

#!/usr/bin/env python2.3

## \file YesNet.py
## \brief a simple object to access Yes.net information

import re
import urllib2

optionvaluere = re.compile( 'value=\"(.*?)\".*?\>(.*?)\<\/' )
cityre = re.compile( 'name=city.*?\>\s*(.*?)\s*\<\/select', re.S )
cityurlre = re.compile( '\s' )
radiore = re.compile( 'name=radio.*?\>\s*(.*?)\s*<\/select', re.S )
sidre = re.compile( 'name=sid.*?\>\s*(.*?)\s*<\/select', re.S )
playingre = \ 
		  re.compile( '\<td\scolspan=\"2\"\>.*?\n\s*(.*?)' + 
		  '\s*?\n\s*(.*?)\s*?\n', re.S )

class YesNet:
    def __init__( self ):
		"""setup our data structures"""
		self.citymap = self.getcities()
		self.radiomap = {}
		
	def getcities( self ):
		"""pull the list of cities that we can query for.
		
		this is a helper function used in the initialization. it's
		mostly so that we can create a mapping between city names and
		the internal name that yes.net uses.

		\return a mapping of city names
		"""
		
		yesurl = urllib2.urlopen( 'http://www.yes.net/home_bar.jsp' )
		cities = \
			   optionvaluere.\
               findall( cityre.search( yesurl.read() ).group( 1 ) ) 
		citymap = {} 
		for city in cities:
			citymap[city[1]] = cityurlre.sub( '%20', city[0] )
		return citymap

	def listcities( self ):
		"""a function that will list the cities that yes.net knows about

		this returns the human-readable version of the cities that yes.net knows about
		
		\return a list of city names
		"""
		return self.citymap.keys()

	def getstations( self, city ):
		"""given a city name, get the stations that yes.net knows about
		
		this takes a human version of the city name, looks up the 
		appropriate yes.net internal version, queries for that, then 
		returns a list of station names. we also have to cache this 
		so we can do fast lookup on whether a radio station is valid.

		\param city the human-readable version of the city name
		\return a list of the stations in that city
		"""
		if city not in self.citymap:
			return []


		urlcity = self.citymap[ city ] 
		yesurl = \ 
			   urllib2.urlopen( 'http://www.yes.net/home_bar.jsp?city=%s'%						( urlcity ) ) 
		stations = optionvaluere.findall( radiore.\
                                          search( yesurl.read() ).group( 1 ) ) 
		stationlist = [] 
		for station in stations:
			stationlist.append( station[0] )
		self.radiomap[ city ] = stationlist
		return stationlist

	def getlastplayed( self, city, station ):
		"""given a city name and a station name, get the last played
		
		this takes the human-readable city name and the station name, 
		and queries yes.net. from yes.net, it attempts to retrieve 
		the list of the last few songs played and returns them in a 
		list

		\param city the human-readable version of the city name 
		\param station the station name 
		\return the list of what was played 
		""" 
		
		if city not in self.citymap:	
			return []
		if city not in self.radiomap:
			self.getstations( city )
		if station not in self.radiomap[ city ]:
			return []

		yesurl = \ 
			   urllib2. \ 
			   urlopen( 'http://www.yes.net/home_bar.jsp?city=%s&radio=%s'% \
				( self.citymap[ city ], station ) )
		sid = \ 
			optionvaluere. \            
			search( sidre.search( yesurl.read() ).group( 1 ) ).group( 1 )
		yesurl = \
			   urllib2. \
			   urlopen( 'http://www.yes.net/playing.jsp?' +

				'city=%s&radio=%s&sid=%s'%
				( self.citymap[ city ], station, sid ) )        
		playingmatch = playingre.search( yesurl.read() ) 
		return [ playingmatch.group( 1 ), playingmatch.group( 2 ) ]


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows