質問

I have to make a simple artist searcher in python with django with last.fm API. I know that django uses a database, but I don't know how can I reach the last.fm database, where can I set this?

役に立ちましたか?

解決

You don't need to connect your Django database directly into last.fm, and - in fact - you cannot even do so. Instead, you need to use last.fm API to get data from their database -- something you already mentioned in your question.

On a high level what you need to do is:

  • Get an API account to last.fm
  • Find the API method you want to call from last.fm API doc (artist.search)
  • Call this method in your Python script (most likely some views.py method)
  • Return and format the results from the API call (maybe JSON or just directly rendering to HTML template)

In practice, you would end up with something like:

import requests

def lastfm_artist_search(request, artist_name):
    api_url = 'http://ws.audioscrobbler.com/2.0/'
    api_key = 'YOUR_LASTFM_API_KEY'
    url = api_url+'?method=artist.search&format=json&artist='+artist_name+'&api_key='+api_key
    data = requests.get(url)
    return HttpResponse(data.text)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top