# -*- coding: utf-8 -*-
"""
1.搜索薛之谦url
2.找XHR,只能一个个找,无他法;在preview中找json数据。
3.找出播放m3u地址
"""
import requests
def get_id():
    #搜索薛之谦url
    url='https://music.163.com/weapi/cloudsearch/get/web?csrf_token='
    data={#参加加密了
          'params':'GOTkc2PntV8zEm+2i2/1NcU5ypnhQZeBCtpxa+BuI0mhBsMdq0gGaZRNPJkB5DAJ4R8sIfiaj8eUTJxWTbNs1RQXzmi/BAZIfglDEzY87Dzj0PJCfa/E32v4/kv/dVFRHwLibs27CNwT89MyPZz9vsjbCA4cIpyeWoXj5/gh7ezWX/a+2q9641LQ/oSVgEurhf7X/CoWdXFVxN7s69s0w/1S7DfintAFn6VBM75RoOVG10wMxJAyShJYUe+C+2VgBAkc9jDdlA8HrtYV//3JwQ==',
          'encSecKey':'56bd1322ddbb92737fce3101fb9f6b6550b68abe001d8a85a01923fcbf6deebf2db1d4990f3185aee7793f06cd3f45a698568e31c9b88cdca45ccffd2d404f5433e4d0747021262a1c14df48564b5d5761c39f2ba2f934edbaf03121bf391a3f0e1ed6c76769e62293f5dbea166a9e3fe787411b88cdc62d64f0adefc8cc3db4'
          }
    
    response=requests.post(url,data=data).json()
    idName_lists=response["result"]["songs"]
    url_list=[]
    for i in idName_lists:
        item={}
        item["id"]=i["id"]
        item["name"]=i["name"]
        url_list.append(item)
    return url_list
    
def pars_data(id_lists):
    for i in id_lists:
        #之前的接口,
        url='https://music.163.com/song/media/outer/url?id={}.mp3'.format(i["id"])
        print('正在下载:{}'.format(i["name"]))
        #要下载,要打印二进制
        res=requests.get(url).content
        with open('mp3/'+i["name"]+'.mp3','wb') as f:
            f.write(res)
            
id_list=get_id()
pars_data(id_list)