Lastfm艺术家搜索器在蟒蛇与django [关闭]

问题描述 投票:-1回答:1

我必须使用带有last.fm API的django在python中创建一个简单的艺术家搜索器。我知道django使用数据库,但我不知道如何到达last.fm数据库,我可以在哪里设置它?

python django last.fm
1个回答
4
投票

您不需要将Django数据库直接连接到last.fm,而且 - 实际上 - 您甚至无法这样做。相反,您需要使用last.fm API从其数据库中获取数据 - 您已经在问题中提到过这一点。

在高层次上,您需要做的是:

  • 得到一个API account to last.fm
  • 从last.fm API doc(artist.search)中找到要调用的API方法
  • 在Python脚本中调用此方法(很可能是一些views.py方法)
  • 返回并格式化API调用的结果(可能是JSON或只是直接渲染到HTML模板)

在实践中,你最终会得到类似的东西:

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)
© www.soinside.com 2019 - 2024. All rights reserved.