Skip to content

Instantly share code, notes, and snippets.

@unbracketed
Created July 10, 2010 05:05
Show Gist options
  • Save unbracketed/470452 to your computer and use it in GitHub Desktop.
Save unbracketed/470452 to your computer and use it in GitHub Desktop.
LastFM scrobbler for Radio Paradise favorites
BeautifulSoup==3.0.7a
-e svn+http://python-lastfm.googlecode.com/svn/trunk/#egg=lastfm
"""
A quick and dirty script I wrote to scrobble my Radio Paradise favorites to Last.FM
"""
import re
from urllib import urlopen
from BeautifulSoup import BeautifulSoup
import lastfm
debug = True
lastfm_api = '----'
lastfm_secret = '----'
lastfm_session = '----'
def scrape_user_favorites(user_profile_url):
if debug:
soup = BeautifulSoup(open('my_user_page.html'))
else:
soup = BeautifulSoup(urlopen(user_profile_url))
my_songs = []
#naive regex to split the Track info on the hyphen
track_split = re.compile(r'(?P<artist>.*)\s-\s(?P<song>.*)')
for th in soup.findAll('th'):
if th.string == 'Highest Rated Songs':
song_list = th.findNextSibling('tr').td
for part in song_list.findAll('br'):
track_elem = part.previousSibling
rp_song_url = track_elem['href']
track = track_elem.string
m = track_split.match(track)
if m:
rating = int(part.previousSibling.previousSibling.string.strip()[0])
my_songs.append((rating,m.group('artist'),m.group('song'),rp_song_url))
else:
continue
return my_songs
def scrobble_tracks(track_list):
if debug:
api = lastfm.Api(lastfm_api,lastfm_secret,lastfm_session)
else:
api = lastfm.Api(lastfm_api,lastfm_secret,lastfm_session)
me = api.get_user('unbracketed')
misses = []
for track_info in track_list:
results = list(api.search_track(track_info[1],artist=track_info[2]))
if len(results) == 1:
track = results[0]
print 'found track: %s' % track
me.library.add_track(track)
else:
print '%d results for ' % len(results)
print track_info
misses.append(track_info)
return misses
if __name__ == '__main__':
url = "http://www.radioparadise.com/content.php?name=Members&file=userinfo&u=73449"
faves = scrape_user_favorites(url)
misses = scrobble_tracks(faves)
print misses
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment