根据前端参数后台对数据进行分页返回
HOST_SINGERS_URL = 'http://artistlistinfo.kuwo.cn/mb.slist?stype=artistlist&category=0&order=hot&pn=0&rn=100&encoding=utf8'
CNAME_SINGERS_URL = 'http://artistlistinfo.kuwo.cn/mb.slist?stype=artistlist&category=0&order=dict&pn=0&rn=100&encoding=utf8&prefix={}'
SINGERS_IMG_PRE_URL = 'http://img1.sycdn.kuwo.cn/star/starheads/'
def api(func):
@wraps(func)
def _deal_with(*args, **kwargs):
response_data = demjson.encode(func(*args, **kwargs))
response = HttpResponse(response_data, content_type='application/json')
response["Access-Control-Allow-Origin"] = "*"
response["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS"
response["Access-Control-Max-Age"] = "1000"
response["Access-Control-Allow-Headers"] = "*"
return response
return _deal_with
@api
def get_singers(request):
page = request.GET.get('page', '1')
page = int(page) if page.isdigit() else 1
size = 20
cname = request.GET.get('cname', 'host').upper()
if cname in [chr(i).upper() for i in range(97, 123)]:
if not cache.has_key('{}.txt'.format(cname)):
url = CNAME_SINGERS_URL.format(cname)
response = requests.get(url)
json_str = response.text
print(cname)
cache.set('{}.txt'.format(cname), json_str, 60)
else:
json_str = cache.get('{}.txt'.format(cname))
json_obj = demjson.decode(json_str)
artistlist = json_obj.get('artistlist', None)
for artist in artistlist:
artist['pic'] = SINGERS_IMG_PRE_URL + artist['pic']
artist_dict = {
'artistlist': artistlist[(page - 1) * size: page * size]
}
return artist_dict