#!/usr/bin/python
import urllib, time, mechanize
from BeautifulSoup import BeautifulSoup, BeautifulStoneSoup
br = mechanize.Browser()
# your radio station of choice goes here:
html = br.open('http://radiotime.com/station/s_32633/KFJC_897.aspx').read()
artists = []
tracks = []
trackLinks = []
soup = BeautifulSoup(html)
anchor = soup.find(text='today')
tds = anchor.findAllNext('td') # get all the
tags
i = 0
for td in tds:
if ((i - 1)%4 == 0):
if td.a == None:
artists.append(td.string)
print i, "Artist: ",td.string
else:
artists.append(td.a.string)
print i, "Artist: ",td.a.string
if ((i - 2)%4 == 0):
tracks.append(td.string)
print i, "Title: ",td.string
i = i + 1
i = 0
for artist,track in zip(artists, tracks):
i = i = 1
escapedTrack = urllib.quote(track)
xml = br.open('http://ws.spotify.com/search/1/track?q='+escapedTrack).read()
stone = BeautifulStoneSoup(xml)
artistNameNode = stone.find(text=artist)
try:
trackLinks.append(artistNameNode.parent.parent.parent['href'])
except:
pass
if i%5==0:
time.sleep(1) #delay after 5 requests because of API access limit
for link in trackLinks:
print link
|