Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fetching latest releases from Deezer API
deezer_id = 700513741 # paste your ID here
# hello, User
import requests
url = f"https://api.deezer.com/user/{deezer_id}"
user_response = requests.get(url)
name = user_response.json()['name']
print(f"Hello, {name}!")
# fetching favorite artists
artists_url = f"https://api.deezer.com/user/{deezer_id}/artists"
artists_response = requests.get(artists_url)
artists_response.json()
artists = []
all_artists_loaded = False
url = f"https://api.deezer.com/user/{deezer_id}/artists"
while not all_artists_loaded:
response = requests.get(url).json()
artists += response['data']
if response.get('next'):
url = response['next']
else:
all_artists_loaded = True
# fetching favorite artists' releases
releases = []
for artist in artists:
current_artist_releases = []
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()
current_artist_releases += response['data']
if response.get('next'):
url = response['next']
else:
all_releases_loaded = True
for release in current_artist_releases:
full_title = f"{artist['name']}{release['title']}"
release['full_title'] = full_title
releases += current_artist_releases
# printing latest releases
import datetime
today = datetime.date.today()
year_ago = today - datetime.timedelta(days=365)
for release in releases:
previous_date = release['release_date']
new_date = datetime.datetime.strptime(previous_date, '%Y-%m-%d').date()
release['release_date'] = new_date
latest_releases = [
release for release in releases
if release['release_date'] > year_ago
]
latest_releases = sorted(latest_releases, key=lambda release: release['release_date'])
for release in latest_releases:
date = release['release_date']
title = release['full_title']
print(f"{date}: {title}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment