Python scans Baidu local music and downloads lyrics

Source: Internet
Author: User
Tags cdata urlencode

This time this really is dry goods oh, last night made a half night,,,, from 8 points after eating dinner began to write, has been nearly 12 o'clock to finish,,, novice, can't afford to hurt Ah ....
First of all, Baidu provides a music search API, you want Baidu request similar to

http://box.zhangmen.baidu.com/x?op=12&count=1&title= best loss friend $$ Eason Chan $ $

Address, Baidu will return you a section of XML, as shown below

The code is as follows Copy Code

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<result>
<count>1</count>
<url>
<encode>
<! [cdata[

http://zhangmenshiting.baidu.com/data2/music/12762845/ Ymrqamdua21fn6nndk6ap5wxcjlrmg1xljhobwibmgpjk5ztmwizcwrjz5lqbgyelgkwlztubgljz5lka2uanwsxy1qin5t1ywbmzw5ocglhawdnbgtqbze $

]]>
</encode>
<decode>
<! [cdata[
12762845.mp3?xcode=e6b69cf593ea22ac9d2b9314e565fc0caf85125f065ce3e0&mid=0.31929107437537
]]>
</decode>
<type>8</type>
<lrcid>2829</lrcid>
<flag>1</flag>
</url>
<durl>
<encode>
<! [cdata[

http://zhangmenshiting2.baidu.com/data2/music/7345405/ Agvnawlmbgaeomzzrzmmnjzvmgqxbhcbl2dsz5qxawqslwpsmmdrb2mxamxpbxcclgnsmw2ba25mymxtapmzcwqtwagemnrox2vkbwdvaghozmzramluoa $$

]]>
</encode>
<decode>
<! [cdata[
7345405.mp3?xcode=e6b69cf593ea22ac78e1478e78479dc19e8e4650995cb99a&mid=0.31929107437537
]]>
</decode>
<type>8</type>
<lrcid>2829</lrcid>
<flag>1</flag>
</durl>
<p2p>
<url>
<! [Cdata[]]>
</url>
<type>mp3</type>
<size>3778335</size>
<bitrate>128</bitrate>
</p2p>
</result>

The simple explanation, since all we have to do is get to the LRC lyrics address of the song, so the only useful one is 2829 this label.
and encode and decode inside the stitching up is the MP3 download address, as in this case

http://zhangmenshiting.baidu.com/data2/music/12762845/ Ymrqamdua21fn6nndk6ap5wxcjlrmg1xljhobwibmgpjk5ztmwizcwrjz5lqbgyelgkwlztubgljz5lka2uanwsxy1qin5t1ywbmzw5ocglhawdnbgtqbze $12762845.mp3?xcode=e6b69cf593ea22ac9d2b9314e565fc0caf85125f065ce3e0&mid=0.31929107437537

is to download the address, but the sound quality is too poor, there is time to study this.
Keep saying the lyrics, notice the 2829 in the lrcid tag.
http://box.zhangmen.baidu.com/bdlrc/This is Baidu LRC lyrics store address,
And then this example's lyrics address is HTTP://BOX.ZHANGMEN.BAIDU.COM/BDLRC/28/2829.LRC
See, the two digits behind the address of the lyrics are the integers that lrcid divided by 100, the first number, and then the second number is lrcid, followed by the suffix. LRC's done.
It's easy to get a LRC address, just ask for the address, and then write the acquired content to the file.
Well, that's probably it, here's the code.

The code is as follows Copy Code

Import OS
Import Os.path
Import re
Import Eyed3
Import Urllib2
Import Urllib
From Urllib import UrlEncode
Import Sys

Import OS
Reload (SYS)
Sys.setdefaultencoding (' UTF8 ')

Music_path = r "E:\music"
Lrc_path = r "E:\LRC"

Os.remove (' Nolrc.txt ')
Os.remove (' Lrcxml.txt ')

The_file = open (' Lrcxml.txt ', ' a ')
Nolrc_file = open (' Nolrc.txt ', ' a ')

For Root,dirs,files in Os.walk (Music_path):
For filepath in Files:
The_path = Os.path.join (Root,filepath)
if (The_path.find ("MP3")!=-1):
Print The_path
The_music = Eyed3.load (The_path)
The_teg = The_music.tag._getalbum ()
The_artist = The_music.tag._getartist ()
The_title = The_music.tag._gettitle ()
# Print The_teg
# Print The_title
# Print The_artist
b = The_title.replace (', ' + ')
# Print B
A = The_artist.replace (', ' + ')
#print UrlEncode (str (b))
If Isinstance (A,unicode):
A = A.encode (' UTF8 ')
Song_url = "http://box.zhangmen.baidu.com/x?op=12&count=1&title=" +b+ "$$" +a+ "$ $ $"

The_file.write (song_url+ ' \ n ')
page = Urllib2.urlopen (song_url). Read ()
Print page
Theid = 0

Lrcid = Re.compile (' <lrcid> (. *?) </lrcid> ', Re. S). FindAll (page)
HAVE_LRC = True
If Lrcid!= []:
Theid = lrcid[0]

Else
Nolrc_file.write (the_title+ ' \ n ')
HAVE_LRC = False
Print Theid


If HAVE_LRC:
FirstID = Int (theid)/100
Lrcurl = "http://box.zhangmen.baidu.com/bdlrc/" +str (FirstID) + "/" +theid+ ". LRC"
Print Lrcurl
LRC = Urllib2.urlopen (Lrcurl). Read ()
if (lrc.find (' html ') = = 1):
Lrcfile = open (lrc_path+ "\ +the_title+". LRC ", ' W ')
Lrcfile.writelines (LRC)
Lrcfile.close ()
Else
Nolrc_file.write (the_title+ ' \ n ')

The_file.close ()
Nolrc_file.close ()
Print "end!"

Useful the first step of the request is obtained in the XML format, so would like to parse XML to get lrcid, but in the implementation of the process encountered a variety of problems, and other easy, in this together the longest waste of time, the tangle of fruit, can only use regular expressions to obtain ... It only means that the Apprentice is not fine,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.