Skip to content

Instantly share code, notes, and snippets.

@daks
Forked from GeReinhart/albums_per_year.md
Last active February 28, 2017 18:34
Show Gist options
  • Save daks/de3d81be3cf0c2ccef0b7358b5a00f69 to your computer and use it in GitHub Desktop.
Save daks/de3d81be3cf0c2ccef0b7358b5a00f69 to your computer and use it in GitHub Desktop.
Albums

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.

2003

Radiohead - Hail to the Thief Cat Power - You Are Free The White Stripes - Elephant The Kills - Keep on Your Mean Side Syd Matters - A Whisper and a Sigh Kings Of Leon - Youth & Young Manhood Blur - Think Tank Grandaddy - Sumday The Warlocks - Phoenix album

2002

Interpol - Turn on the bright lights The Libertines - Up the bracket Queens of the stone age - Songs for the deaf Dionysos - Western sous la neige Sigur Ros - () The flaming lips - Yoshimi Battles The Pink Robots Dj Shadow - The private press Coldplay - A rush of blood to the head Moby - 18 Eminem - The Eminem Show The coral - The coral

2001

Noir Désir - Des visages des figures Gorillaz - Gorillaz The Strokes - Is this it New Order - Get Ready Tricky - Blow back

The Microphones - The Glow Pt2

https://play.spotify.com/album/6QYoRO2sXThCORAifrP4Bl

Röyksopp - Melody A.M.

https://play.spotify.com/album/4WOZU9evfEO7eI6ICsoGN0

Sparklehorse - It's a wonderful life

https://play.spotify.com/album/1akX7d4n8BJWFumQUMnf4x

Air - 10 000 Hz Legend

https://play.spotify.com/album/2WNI378JH55k63eXBHbjbI

Kings of convenience - Quiet the new loud

https://play.spotify.com/album/7MjceL6iPFM86qxxeWCVEz

Gillian Welch - Tim the revelator

https://play.spotify.com/album/55FP2ypQcghszSqylyBRbp

Mercury Rev - All is dream

https://play.spotify.com/album/4uF6vRvePC5498VtY8WZmA

Daft Punk - Discovery

https://play.spotify.com/album/2noRn2Aes5aoNVsU6iWThc

2000

Radiohead - Kid A

https://play.spotify.com/album/19RUXBFyM4PpmrLRdtqWbp

The White Stripes - De stijl

https://play.spotify.com/album/0vkb15L7oNuvVcznJRE7xc

God Speed You Black Emperor - Lift your...

https://play.spotify.com/album/2rT82YYlV9UoxBYLIezkRq

PJ Harvey - Stories from the city, stories from the sea

https://play.spotify.com/album/1juaKifFulnYa2ygjvlAUU

Coldplay - Parachutes

https://play.spotify.com/artist/4gzpq5DPGxSnKTe4SA8HAU

Modest Mouse - Building nothing out of something

https://play.spotify.com/album/737pByztRdxfeP77wFrxCC

Badly drawn boy - The Hour of Bewilderbeast Placebo - Black market music

Idlewild - 100 broken windows

https://play.spotify.com/album/2USo2GmIfOLo7q25IKLjeZ

Elliott Smith - Figure 8

https://play.spotify.com/album/1zcOyLZRT2TyxReQ55AEbM

Grandaddy - The sophtware slump

https://play.spotify.com/album/0xKo3a0K76BLh2LZD3MnWn

Goldfrapp - Felt mountain

https://play.spotify.com/album/7Ce9F1Eof9r7u9tr702H5C

Primal Scream - Xtrmntr

https://play.spotify.com/album/1QXU6VxsD9aIMuPlw1TSXN

Doves - Lost souls

https://play.spotify.com/album/33nyNThvBKPzS4NGdnWACf

Yo La Tengo - And then nothing turned itself inside-out

https://play.spotify.com/album/5egsoXU4SPbj38cP9sBlXk

Eels - Daisies of the galaxy

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment