Getting your latest releases from Deezer with Python

And when all albums are fetched we would need to add the full title to them.releases = [] # we'll store all releases herefor artist in artists: # this time we'll store releases in a temporary array current_artist_releases = [] # nothing changed here all_releases_loaded = False artist_id = artist['id'] url = f"https://api.deezer.com/artist/{artist_id}/albums" while not all_releases_loaded: response = requests.get(url).json() # now we're saving each page's response # to current_artists_releases # instead of straight to releases current_artist_releases += response['data'] # this stays the same too if response.get('next'): url = response['next'] else: all_releases_loaded = True # and after we've fetched all releases # we can add full title to all of them for release in current_artist_releases: full_title = f"{artist['name']} — {release['title']}" release['full_title'] = full_title # and only after we every release from current artist # has a full title we can add them to the main releases releases += current_artist_releasesSo now all releases should have a full titlereleases[-1]['full_title']# => 'Aries — DEADMAN WUNDERLAND'So what have you missed?Let’s check what was released during the last year..If you haven’t checked it, you might be surprised by some interesting albums.To begin, we need to filter releases so that only that were created during the last year are left..To do this we will use a standard datetime library to get a date of the last yearimport datetimetoday = datetime.date.today()print(today)# => 2018-12-13year_ago = today – datetime.timedelta(days=365)print(year_ago)# => 2017-12-13Then, to filter we need to compare the release date with our year_ago variable..The only problem here is that release date is a stringreleases[-1]['release_date']# => '2017-04-18'And we can’t compare string with a date in Python..So a way out of this is to convert a string into a date..The datetime library has a function just for our case called datetime.strptime..It needs a string (the release date) and it’s format.As we can see, release_date format is 'year-month-day'..With a little help of documentation, this converts into '%Y-%m-%d'datetime.datetime.strptime(releases[-1]['release_date'], '%Y-%m-%d').date()# => datetime.date(2017, 4, 18)Let’s store the release_date as a date instead of a string for all releases.for release in releases: previous_date = release['release_date'] new_date = datetime.datetime.strptime(previous_date, '%Y-%m-%d').date() release['release_date'] = new_dateNow we can finally filter our releases to get only the ones from last yearlatest_releases = [ release for release in releases if release['release_date'] > year_ago]After that, all of the last year’s releases are stored in the latest_releases variable..Now we need to sort those releases by date to have them listed chronologicallylatest_releases = sorted(latest_releases, key=lambda release: release['release_date'])And finally, we can print those releasesfor release in latest_releases: date = release['release_date'] title = release['full_title'] print(f"{date}: {title}")# …# 2018-10-25: Aries — BLOSSOM# 2018-11-02: Panic!.At the Disco — The Greatest Show# 2018-11-02: The Neighbourhood — Hard To Imagine The Neighbourhood Ever Changing# 2018-11-09: Muse — Simulation Theory (Super Deluxe)# 2018-11-09: Imagine Dragons — Origins# 2018-11-09: Imagine Dragons — Origins (Deluxe)# 2018-11-09: Goody Grace — Nostalgia Is A Lie# 2018-11-30: Arctic Monkeys — Tranquility Base Hotel & CasinoIf you’ve followed along this far please share your latest releases for the last month in the comments.ConclusionIn this article I’ve covered:how to work with Deezer APIhow to make requests with Pythonhow to convert strings into datesCongratulations, now you know how to check for new releases from artists that you follow..If you’re like me and hate missing out on new releases from your favorite artists, I’ve built a website just for this purpose.If you would like to run the code by yourself and don’t want to scramble it from the article pieces by pieces, I’ve uploaded it into a gist on GitHub.Also, this is the third part of a series of articles about the MuN..Stay tuned for part 4.. More details

Leave a Reply