This script helps you construct your albums per year list, using Last.fm top albums list.
You need Python, pylast and musicbrainzng.
Initialize your parameters like in the env.example file and 'source' it.
https://play.spotify.com/album/6QYoRO2sXThCORAifrP4Bl
https://play.spotify.com/album/4WOZU9evfEO7eI6ICsoGN0
https://play.spotify.com/album/1akX7d4n8BJWFumQUMnf4x
https://play.spotify.com/album/2WNI378JH55k63eXBHbjbI
https://play.spotify.com/album/7MjceL6iPFM86qxxeWCVEz
https://play.spotify.com/album/55FP2ypQcghszSqylyBRbp
https://play.spotify.com/album/4uF6vRvePC5498VtY8WZmA
https://play.spotify.com/album/2noRn2Aes5aoNVsU6iWThc
https://play.spotify.com/album/19RUXBFyM4PpmrLRdtqWbp
https://play.spotify.com/album/0vkb15L7oNuvVcznJRE7xc
https://play.spotify.com/album/2rT82YYlV9UoxBYLIezkRq
https://play.spotify.com/album/1juaKifFulnYa2ygjvlAUU
https://play.spotify.com/artist/4gzpq5DPGxSnKTe4SA8HAU
https://play.spotify.com/album/737pByztRdxfeP77wFrxCC
https://play.spotify.com/album/2USo2GmIfOLo7q25IKLjeZ
https://play.spotify.com/album/1zcOyLZRT2TyxReQ55AEbM
https://play.spotify.com/album/0xKo3a0K76BLh2LZD3MnWn
https://play.spotify.com/album/7Ce9F1Eof9r7u9tr702H5C
https://play.spotify.com/album/1QXU6VxsD9aIMuPlw1TSXN
https://play.spotify.com/album/33nyNThvBKPzS4NGdnWACf
https://play.spotify.com/album/5egsoXU4SPbj38cP9sBlXk
https://play.spotify.com/album/3Zd6PJH2M1UnundwZEVwv8
http://pitchfork.com/features/lists-and-guides/5844-top-20-albums-of-2001/ http://www.rocklistmusic.co.uk/lesrock_p2.html#2001 http://www.albumoftheyear.org/genre/1-indie-rock/2001/ http://www.besteveralbums.com/yearstats.php?y=2001
http://www.albumoftheyear.org/genre/1-indie-rock/2000/ http://www.besteveralbums.com/yearstats.php?y=2000
export LASTFM_USERNAME="user" | |
export LASTFM_PASSWORD="password" | |
export API_KEY="your-key" | |
export API_SECRET="your-secret" | |
# default values | |
#export OUTPUT_FILE="albums.txt" | |
export LIMIT=100 | |
export PERIOD="overall" |
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os | |
import pylast | |
import musicbrainzngs | |
# last.fm API | |
API_KEY = os.environ.get('API_KEY') | |
API_SECRET = os.environ.get('API_SECRET') | |
username = os.environ.get('LASTFM_USERNAME') | |
password = os.environ.get('LASTFM_PASSWORD') | |
limit = os.environ.get('LIMIT') or 100 | |
period = os.environ.get('PERIOD') or 'overall' | |
output_file = os.environ.get('OUTPUT_FILE') or ('albums-%s.txt' % period) | |
# logging | |
import logging | |
logging.basicConfig(filename='debug.log',level=logging.DEBUG) | |
# musicbrainz access | |
musicbrainzngs.set_useragent( | |
"python-lastfm", | |
"0.1", | |
) | |
class Album: | |
name = "" | |
artist = "" | |
year = "" | |
def __init__(self, name): | |
self.name = name | |
def __str__(self): | |
output = u"%s - %s - %s" % (self.artist, self.name, self.year) | |
return output.encode('utf-8') | |
def __repr__(self): | |
output = u"%s - %s - %s" % (self.artist, self.name, self.year) | |
return output.encode('utf-8') | |
def init_connection(): | |
password_hash = pylast.md5(password) | |
network = pylast.LastFMNetwork(api_key = API_KEY, api_secret = API_SECRET, | |
username = username, password_hash = password_hash) | |
return network | |
def output_to_text(filename): | |
with open(filename,'w+') as f: | |
ordered_list_per_year = sorted(albums_list_per_year.keys(), reverse=True) | |
for year in ordered_list_per_year: | |
output = '\nYear %s\n' % year | |
f.write(output.encode('utf-8')) | |
albums_list = albums_list_per_year[year] | |
for album in albums_list: | |
output = ' %s by %s\n' % (album.name, album.artist) | |
f.write(output.encode('utf-8')) | |
if __name__ == "__main__": | |
print "" | |
print "Running script with:" | |
print " lastfm username : %s" % username | |
print " period : %s" % period | |
print " number of albums : %s" % limit | |
print " output : %s" % output_file | |
print "" | |
print "available periods are: overall, 7day, 1month, 3month, 6month, 12month" | |
print "" | |
logging.info('initializing last.fm connection') | |
network = init_connection() | |
user = network.get_authenticated_user() | |
logging.info('retrieving %s top albums from period %s' % (limit, period)) | |
top_albums = user.get_top_albums(period=period, limit=limit) | |
albums_list_per_year = {} | |
logging.debug('looping over %s top albums' % limit) | |
for album in top_albums: | |
artist = album.item.get_artist().get_name() | |
album = album.item.get_name() | |
logging.debug(album) | |
# print album.item.get_release_date() # BROKEN | |
release = musicbrainzngs.search_releases(artist=artist,release=album,limit=1) | |
#print release | |
if release.get('release-list', None): | |
#print release['release-list'][0] | |
date = release['release-list'][0].get('date', None) | |
if date: | |
date = date.decode('utf-8') | |
else: | |
date = None | |
album = Album(album) | |
album.artist = artist | |
year = date[:4] if date is not None else u"Unknown" | |
album.year = year | |
if not year in albums_list_per_year: | |
albums_list_per_year[year] = [album] | |
else: | |
albums_list_per_year[year].append(album) | |
output_to_text(output_file) |