New Music Excess

The Method 2.0

Posted in Post-rock by newmusicexcess on December 29, 2016

A good few years ago I posted about something called ‘the method’. It basically involved using the LastFM website to manually create a list of bands that were musically close to another band of ones choice. The list was then used in conjunction with All Music to create a list of top albums for each of those bands. To help with the first step I eventually wrote a program in Java that used the LastFM API and helped save time creating the initial list of bands. Of course, I still had to manually do the album look up on All Music.

As a result of teaching myself Python and looking for a small programming project to undertake, I decided to take a look at the original Java code and convert it to do a similar thing in Python 2.7. The difference this time was that I used LastFM ‘scrobbled’ data for the whole procedure and was therefore able to do everything via the API including top album lookups. Sure, the code could be more ‘Pythonic’, but, it does the job for now, until a refine my coding skills in this language.

Here is what you get if you put in God Is An Astronaut as the ‘seed’ band. Note: The formatting is better tabulated in the actual program

Results for artist similar to God Is An Astronaut whos top album on LastFM is All Is Violent, All Is Bright

Artist    Match    Top Album on LastFM
Collapse Under the Empire 1 Find a Place to Be Safe
If These Trees Could Talk 0.968446 If These Trees Could Talk
Maybeshewill 0.939173 Not for Want of Trying
This Will Destroy You 0.817125 Young Mountain
Exxasens 0.763217 Beyond the Universe
Explosions in the Sky 0.728914 The Earth Is Not a Cold Dead Place
65daysofstatic 0.705380 The Fall of Math
Caspian 0.686965 The Four Trees
I Am Waiting for You Last Summer 0.676615 Edge Party

More importantly, here is the code. But remember, its Python 2.7, to run in 3.x you would need to change  stuff. Secondly, and more importantly, you need to get hold of an API key from LastFM and alter the code accordingly (I have marked where the API key needs to go). Finally, don’t forgot to indent where appropriate – again the formatting is difficult to maintain in this post.

Enjoy

#!/usr/bin/env python

# load funtion modules for url based data and regular expressions
import urllib2
import re

# initalise data for url of lastFM audioscrobbler, api key and limit of artists and albums returned
rooturlFM = “http://ws.audioscrobbler.com/2.0/”
apikeyFM = “your_lastfm_apikey_goes_here”
limitFM = “10”
limitFMa = “1”

# get artist name from user
artistFM = raw_input(“Please enter an artists name: “)

# replace any blanks in artists name
artistFMtmp = artistFM.replace(” “, “%20”)

# construct full url for audioscrobbler query to find 10 similar bands
totalurlFM = rooturlFM + “?method=artist.getSimilar&artist=” + artistFMtmp + “&limit=” + limitFM + “&api_key=” + apikeyFM

# open, read and close data from audioscrobbler url query
file = urllib2.urlopen(totalurlFM)
dataFM = file.read()
file.close()

# find name information in data and clean up
name = re.compile(“<name>.*</name>”)

nameFM = name.findall(dataFM)
nameFM = [n.replace(“&amp;”, “and”) for n in nameFM]
nameFM = [n.replace(“&apos;”, “‘”) for n in nameFM]
nameFM = [n.replace(“<name>”, “”) for n in nameFM]
nameFM = [n.replace(“</name>”, “”) for n in nameFM]

# find match probability in data and clean up
match = re.compile(“<match>.*</match>”)
matchFM = match.findall(dataFM)
matchFM = [n.replace(“<match>”, “”) for n in matchFM]
matchFM = [n.replace(“</match>”, “”) for n in matchFM]

# find most popular album for seed band and cleanup
totalurlFMna = rooturlFM + “?method=artist.gettopalbums&artist=” + artistFMtmp + “&limit=” + limitFMa + “&api_key=” + apikeyFM
file = urllib2.urlopen(totalurlFMna)
dataFM = file.read()
file.close()
namealbumFM = name.findall(dataFM)[0]
namealbumFM = namealbumFM.replace(“&amp;”, “and”)
namealbumFM = namealbumFM.replace(“&apos;”, “‘”)
namealbumFM = namealbumFM.replace(“<name>”, “”)
namealbumFM = namealbumFM.replace(“</name>”, “”)

# initalise list for album population
albumFM = [“”]*10

# looped look up to lastfm for finding most popular albums by similar artists
for i in range(0,9):
    nameFMtmp = “”
    nameFMtmp = nameFM[i].replace(” “, “%20”)
    
    totalurlFMa = rooturlFM + “?method=artist.gettopalbums&artist=” + nameFMtmp + “&limit=” + limitFMa + “&api_key=” + apikeyFM
    
    file = urllib2.urlopen(totalurlFMa)
    dataFM = file.read()
    file.close()
    albumFM[i] = name.findall(dataFM)[0]
    albumFM[i] = albumFM[i].replace(“&amp;”, “and”)
    albumFM[i] = albumFM[i].replace(“&apos;”, “‘”)
    albumFM[i] = albumFM[i].replace(“<name>”, “”)
    albumFM[i] = albumFM[i].replace(“</name>”, “”)

# printout band names, match and top album
print(” “)
print(“Results for artist similar to ” + artistFM + ” whos top album on LastFM is ” + namealbumFM)
print(” “)
print “Artist”.ljust(30), “Match”.ljust(10), “Top Album on LastFM”
for n in range(0,9):
    print nameFM[n].ljust(30), matchFM[n].ljust(10), albumFM[n]