This article is for Django Advent Calendar 2019. The target audience is for those who are new to Django.
The previous article was about @ yuu-eguci's 500 error. https://qiita.com/yuu-eguci/items/a1e4b0a2f238d5ccc985
This article is about running Spotify's API in Django.
I need to create a Spotify user account, but the following article by Class Method is very helpful. https://dev.classmethod.jp/etc/about-using-of-spotify-api/
If you want to execute the sample code of the article, please download the chart data in CSV format from the following. https://spotifycharts.com/regional/jp/daily/latest
It's very bad manners, but I want you to check the operation easily, so I've summarized everything in views.py. I'm sorry if it makes you feel uncomfortable. .. ..
class SpotifySong:
def __init__(self, song_name, uri):
self.song_name = song_name
self.uri = uri
def spotify(request) :
#Spotify Client ID,Enforce authorization using Secret
client_id = ''
client_secret = ''
client_credentials_manager = spotipy.oauth2.SpotifyClientCredentials(client_id, client_secret)
#Create a Spotify instance
spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
#Read CSV file downloaded from Spotify
songs = pd.read_csv(BASE_DIR + '/regional-jp-daily-latest.csv', index_col=0, header=1)
#Get song information from Spotify
spotify_songs_list = []
for url in songs['URL']:
spotify_songs_list.extend(spotify.audio_features(url))
#Attach song titles
response_list = []
for i,spotify_song in enumerate(spotify_songs_list):
response_list.append(SpotifySong(songs.iat[i,0],spotify_song['uri']))
#Specify template
template = loader.get_template('spotify/spotify_base.html')
context = {
'response_list': response_list,
}
return HttpResponse(template.render(context, request))
Please refer to the following for the response returned by spotify.audio_features. Not only the playing time and keys, but also indicators such as how acoustic the song is and how much you can dance are returned. I will omit it in this article, but it seems that I can make a service to learn the tendency of songs that are preferred depending on the season! https://developer.spotify.com/documentation/web-api/reference/tracks/get-audio-features/
{% if response_list %}
<h1>daily ranking</h1>
<ul>
{% for response in response_list %}
<li><a href="https://embed.spotify.com/?uri={{ response.uri }}"> {{response.song_name}} </a></li>
{% endfor %}
</ul>
{% else %}
<p>Failed to get music from Spotify.</p>
{% endif %}
Although it is simple HTML, you can make a daily ranking like the following. It's a simple screen that you can click to jump to the Spotify song page, but I think it would be great for beginners to make it.
There are a few things to keep in mind when working with Templates in Django. Click here for the manual. https://docs.djangoproject.com/en/2.2/ref/templates/language/#templates
You need to get it by writing {{variable | length}}. You cannot write the variable .length.
You can only get it by directly specifying the index number, such as {{variable .0}}. You cannot write {{variable [[0]]}}.
For the above reasons, this sample code defines its own class to get the song title. It is possible to pass the return value of SpotifyAPI and the result of CSV reading, but it is difficult to get it using the index number, so it is troublesome to handle the data with the objects separated.
If you type the following command, the Port is being used error may occur.
python3 manage.py runserver
Django version 2.2.7, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Error: That port is already in use.
The easiest way to solve the above is to kill the process by following the steps below.
lsof -i -P | grep 8000
Python 71504 user 4u IPv4 0x2cdb3e922e88888 0t0 TCP localhost:8000 (LISTEN)
kill -9 71504
The hurdles for the final release date of Django 3.0 haven't been exceeded, but I hope it helps someone. .. .. Tomorrow's Django Advent Calendar is @ shimayu22. Thank you!!
Recommended Posts