BOBOBK

Python implementation for Qianqian Music MP3 download

MISCELLANEOUS

Entering the Qianqian Music homepage and selecting Jay Chou’s “Confession Balloon” reveals that it’s a 2016 song with no preview available, which is sad. Is there a way to get the MP3 file? The answer is yes. A runnable program for music download is available at the end of the article.

Without further ado, I opened the charts and chose a new song that could be previewed; “Sheng Pi Zi” was the first one I could listen to.

1. Analyze API information

Opening the developer tools, I found that music files are definitely submitted through an API. Among many requests, I found a request that could retrieve music files. See the image below:

Check the request details:

The songid parameter can be found in the current URL: http://music.taihe.com/song/611238837. It’s simple. from should be “web” or “app”, etc. The format defining the return data type doesn’t need to be changed. method doesn’t need to be changed. The _ parameter is a 13-digit timestamp. callback is the prefix of the returned JSON data. The 1546915161467 after the underscore is a 13-digit timestamp, while the preceding 17200943498528136486 is unknown. We’ll try to use the known parameters to see if we can retrieve information without changing the unknown content.

Python 3 code:

    import requests
    import time
    apiurl= "http://musicapi.taihe.com/v1/restserver/ting"
    callback = "jQuery17200943498528136486_"+str(round(time.time()*1000))
    hua = str(round(time.time()*1000))
    params = {"method"="baidu.ting.song.playAAC","format":"jsonp","songid":"611238837","from":"web","callback":callback,"_":hua}
    text = requests.get(apiurl,params=params).text
    text

It was successful in retrieving the result. Now, the next step is to download them in bulk.


2. Batch download music to local

Since the above example returns JSON formatted text, all that’s needed is to parse the JSON text using a JSON parser to get the MP3 file and then use requests to download it. Here’s the code:

    import requests
    import time
    import re,json

    def get_song_list():
        text = requests.get("http://music.taihe.com").text
        songid = re.findall(r'href="/song/(d+)"',text)
        return songid
    def get_mp3_address_and_download(songid): 
        apiurl= "http://musicapi.taihe.com/v1/restserver/ting" 
        callback = "jQuery17200943498528136486_"+str(round(time.time()*1000)) 
        hua = str(round(time.time()*1000)) 
        params = {"method":"baidu.ting.song.playAAC","format":"jsonp","songid":songid,"from":"web","callback":callback,"_":hua} 
        text =  json.loads(requests.get(apiurl,params=params).text.split(callback)[1][1:-2])
        song_address = text["bitrate"]["file_link"]
        mp3w = open(songid+".mp3",'wb')
        mp3w.write(requests.get(song_address).content)
        mp3w.close()
    def main():
        try:
            for songid in get_song_list():
                get_mp3_address_and_download(songid)
        except:
            print("network error")

Now, all MP3s from the Qianqian Music homepage have been downloaded.

3. Search and download music to local

    #!env python
    import requests
    import re,json,time,sys
    def helpmessage():
        msg = r'''
           /          /          /          /
        ////    ////    ////    ////
     /////////////////////////
    //////////////////////////
    ///  This program is developed by Chunjiang Muke            ///
     /      Published on www.bobobk.com            /
     /     The program will open the website when it starts           /
    //    Please don't close it prematurely                //
    //    Author: Chunjiang Muke                  //
     /     Purpose: Music Download                    /
     /     Usage: See the program's runtime interface           /
    //    If you have any questions, please leave a message on the                //
    //    blog page or send an email to    //
     /     2180614215@qq.com                        /
     /                                              /
    ///                                        ///
    //////////////////////////
     /////////////////////////
        ////    ////    ////    ////
           /          /          /          /
    '''
        print(msg)

    def get_mp3_address_and_download(songid,songname):
        apiurl= "http://musicapi.taihe.com/v1/restserver/ting"
        callback = "jQuery17200943498528136486_"+str(round(time.time()*1000))
        hua = str(round(time.time()*1000))
        params = {"method":"baidu.ting.song.playAAC","format":"jsonp","songid":songid,"from":"web","callback":callback,"_":hua}
        text =  json.loads(requests.get(apiurl,params=params).text.split(callback)[1][1:-2])
        song_address = text["bitrate"]["file_link"]
        mp3w = open(songname+".mp3",'wb')
        mp3w.write(requests.get(song_address).content)
        mp3w.close()
    def search_music(keyword):
        if keyword in ["exit",u"退出"]:
            print(u"You chose to exit the current program")

            sys.exit(0)

        text = requests.get("http://music.taihe.com/search?key=%s" % keyword).content.decode("utf8")
        songlist = re.findall(r'href="/song/(d+)".*?data-songdata.*?title="(.+?)"',text)
        return songlist

    def main():
        song = input(u"Please enter the name of the music you want to download: ").strip()
        songlist = search_music(song)

        for i in range(len(songlist)):
            print("%d%s" % (i+1,songlist[i][1]))
        songid = input(u'Please select the number of the song you want to download:')

        get_mp3_address_and_download(songlist[int(songid)-1][0],song)
        print(u"-------Current song download complete---------")
        print(u"To exit, please type 'exit' or '退出'")
    if __name__=='__main__':
        helpmessage()
        while True:
    #        main()
            try:
                main()
            except:
                print(u"Closing program in 5 seconds")
                time.sleep(5)
                sys.exit(0)

Then, use pyinstaller to package the script into an EXE file with the command:

    pyinstaller --onefile download_music.py 

Summary

Here, we used Python’s requests module to retrieve the music list from Qianqian Music’s homepage and download it locally. This method can bypass web page restrictions to download VIP music or copyright-restricted MP3 files. For your convenience, the EXE file has been compressed using ZIP.

Music search and download runnable file download address:

[down_music.zip][1]

 [1]: https://www.bobobk.com/wp-content/uploads/2019/01/down_music.zip

Related

Recursively download files python

TECHNOLOGY
Recursively download files python

I want to back up my website recently, but the size of the file downloaded by PHP is limited, and I am too lazy to install FTP to download it. So I thought of temporarily setting up a secondary domain name site, and then using python (python3)'s requests library to directly download all the files and folders in the root directory of the website to achieve the purpose of backup.