Java program automatically downloads Google music lyrics

Source: Internet
Author: User

Please indicate the source for reprinting. Thank you!

Java program automatically downloads Google music lyrics

I recently wrote an android music player, but I was unable to find a website that could automatically download the lyrics. The one searched on the internet is no longer available.At this time, I thought that I could call Baidu or Google's interface to search for the lyrics. Since Google just launched Google music, I thought about trying it out.

Go to the Google music homepage, and enter a song to search, jump to the song search. I found that there are really lyrics. Unfortunately, the lyrics are not nested in the webpage, and the address of the lyrics webpage contains the ID of the lyrics. What about this?

So I searched for the song ID on the page and found the song ID in the HTML source code. Then I used this ID to splice it into the string of the lyrics URL, finally, the lyrics page was successfully displayed.

However, there is very little information on the lyrics page. Even the lyrics are read from other places, and there is no corresponding address for the lyrics in the HTML source code. Fortunately, I saw the address of the lyrics in the Framework Information on the page. after entering the URL in the browser, I can download the lyrics directly. The downloaded file is the lyrics of the song, excluding other information.

In this way, you have the following ideas:

1. The received parameters are the song name and singer (which can accurately locate the lyrics file). Then, the parameters are spliced behind the query string to query the lyrics page.

2. Obtain the HTML source code of the lyrics page and parse it to obtain the song ID.

3. splice the song ID at the end of the lyrics frame page to obtain the HTML source code of the lyrics frame page.

4. parse the source code of the Framework page and obtain the lyrics.

5. Download and save the lyrics to a file.


Note that:

1. There are not many songs and singers in Google music. Some songs may not find the lyrics, or even Google will not include these songs.

2. The Google server is unstable and the lyrics may fail to be downloaded because everyone knows why.

3. The downloaded lyrics are stored in the project directory by default. You can modify the lyrics by yourself.

4. Google music page encoding are UTF-8, do not need to consider the problem of garbled.


PS: This program is just a reference. You can use it as needed.


The code below:

Tool

Package COM. ZYC. download_lrc;/*** get LRC from Google music * Version 1.00 * @ author ZYC * 2012.2.2 * // ** step: * 1. input music's name and artist, use Google music search engine * the URL like "http://www.google.cn/music/search? Q = Name + artist "* 2.get music ID, the ID is starts with" http://g.top100.cn/16667639/html/lyrics.html? ID "* and ID is after * 3.use ID to get LRC framework HTML, find download lrc url * 4. download LRC * 5. write LRC to file ** notice: Google work may abnormal, you known */import Java. io. bufferedreader; import Java. io. file; import Java. io. filewriter; import Java. io. ioexception; import Java. io. inputstreamreader; import java.net. sockettimeoutexception; import java.net. URL; import java.net. urlconnection; Public C Lass downloadgooglelrctool {/* Music info start, but can't get ID from this, because the music has no LRC */private final static string id_head_flag = "<! -- Freemusic ";/* Google music engine search head */private final static string search_engine_head =" http://www.google.cn/music/search? Q = ";/* Google music LRC by ID head */private final static string search_id_url_head =" http://www.google.cn/music/top100/lyrics? Id = ";/* Google music ID Flag */private final static string search_id_flag =" http://g.top100.cn/16667639/html/lyrics.html? ID ";/* Google music download URL flag */private final static string search_lrc_download_url_flag =" http://lyric.top100.cn /"; /* "% 3d" means "=" */private final static string search_html_equal = "% 3d"; private final static string end_flag = "\\"; private Final Static string lrc_suffix = ". LRC "; private final static string line_end =" \ n ";/* Google music can't connect return code" */private final static strin G google_sb_return_code = "502";/* error message, you know "*/private final static string google_sb_message =" Google shoot... you know \ n, please try again later "; private final static string Space =" ";/*" % 20 "means" "*/private final static string html_space =" % 20 "; private Final Static string html_url_head = "http ://"; /* ID is letter and number "*/private final static string id_match =" ^ [A-Za-z0-9] + {1} quot;/* download message */Private final static string cannot_find_lec_message = "unable to find the lyrics"; private final static string fail_message = "failed to download song % s-% s! \ N cause: "; private final static string success_message =" The Song % s-% s lyrics have been downloaded! \ N lyrics: % s \ n "; private final static string downloading_message =" Download song % s-% s lyrics... \ n ";/* Time Out message */private final static string downloading_connect_time_out =" Connect timed out "; private final static string downloading_connect_time_out_message =" connection timeout. Check network configuration "; private Final Static string downloading_unknown_message = "unknown";/* set timeout = 5 S */private final static long connect_time_out = 5000; /* Set HTTP proxy, if don't use HTTP proxy, set it false */private final static Boolean use_http_proxy = true; private final static string fail_write_file_message = "file writing failed! "; Public static void downloadlrcfromgoogle (string name, string artist) {try {system. out. printf (downloading_message, name, artist);/* Get LRC */string LRC = downloadlrc (name, artist); If (LRC! = NULL) {/* write LRC to file */string filename = Name + lrc_suffix; file = new file (filename); writetofile (file, LRC); system. out. printf (success_message, name, artist, file. getabsolutefile ();} elsesystem. out. println (cannot_find_lec_message);} catch (exception e) {system. out. printf (fail_message, name, artist); If (E. getmessage (). contains (google_sb_return_code) system. out. println (google_sb_message); else I F (E. getmessage (). contains (downloading_connect_time_out) {system. out. println (downloading_connect_time_out_message);} else {system. out. println (downloading_unknown_message);} // E. printstacktrace () ;}} Private Static void Init () {system. setproperty ("sun.net. client. defaulttimetimeout ", String. valueof (connect_time_out); If (use_http_proxy) {// Add HTTP Proxy setting here} Private Static string downloadlrc (St Ring name, string artist) throws ioexception, sockettimeoutexception {Init ();/* Replace "" to "% 2D" */name = Name. replaceall (space, html_space); artist = artist. replaceall (space, html_space); string id = getsongid (name, artist);/* can't find ID return null "*/If (ID = NULL |! Id. matches (id_match) {return NULL;} string Path = getlrcdownloadurl (ID); string LRC = NULL; /* If path isn't start with "HTTP" // "ignore */If (path. startswith (html_url_head) LRC = dodownloadlrc (PATH); Return LRC;} Private Static string getsongid (string name, string artist) throws ioexception {string source = readhtml (search_engine_head + name + "+" + artist); name = Name. replaceall (html_space, space); string m Atch01 = "\"> <B> "+ name +" </B> "; string match02 = NULL; If (name. contains (Space) {/* Set first letter upper */string [] array = Name. split (Space); For (INT I = 0; I <array. length; I ++) {char firstchar = array [I]. charat (0); If (firstchar> = 'A' & firstchar <= 'Z') {array [I] = array [I]. replace (firstchar, (char) (firstchar-32); // set upper} string newname = ""; for (INT I = 0; I <array. length; I ++) {if (I <Rray. length-1) newname + = array [I] + space; elsenewname + = array [I];} match02 = "\"> <B> "+ newname +" </B> ";} If (! Source. Contains (match01) & match02! = NULL &&! Source. contains (match02) {/* may other music return */return NULL;} int first = source. indexof (id_head_flag); If (first =-1) {/* No music ID */return NULL;} int last = source. indexof (id_head_flag, first + id_head_flag.length (); If (last! =-1) {/* ignore only a music ID, if have some choose first one */source = source. substring (first, last);}/* Find ID */INT start = source. indexof (search_id_flag); int mid = source. indexof (search_html_equal, start); int end = source. indexof (end_flag, mid); If (START =-1 | mid =-1 | End =-1) {// can't find ID return NULL;} string id = source. substring (Mid + html_space.length (), end); Return ID;} p Rivate static string getlrcdownloadurl (string ID) throws ioexception {/* set music lrc html url with ID */string source = readhtml (search_id_url_head + id); int start = source. indexof (search_lrc_download_url_flag); int end = source. indexof (lrc_suffix, start); string urlstring = source. substring (START, end + lrc_suffix.length (); Return urlstring;} Private Static string dodownloadlrc (string path) throws Ioexception {/* download LRC */return readhtml (PATH);} Private Static string readhtml (string urlpath) throws ioexception {/* use Google music search engine */URL url = new URL (urlpath);/* connect and get html */urlconnection conn = URL. openconnection (); bufferedreader reader = new bufferedreader (New inputstreamreader (Conn. getinputstream (); string line = NULL; stringbuilder = new stringbuild Er (); While (line = reader. Readline ())! = NULL) {stringbuilder. append (LINE + line_end);} return stringbuilder. tostring ();} Private Static void writetofile (File file, string buffer) throws ioexception {/* write to file, file name is music's name + ". LRC "*/filewriter writer = new filewriter (file, false); try {writer. write (buffer);} catch (exception e) {system. out. println (fail_write_file_message);} finally {writer. close ();}}}

Test class

Package COM. ZYC. download_lrc; public class downloadtest {public static void main (string [] ARGs) {// only for teststring name = "Battle Room"; string artist = "pan baibai"; downloadmusiclrc (name, artist);} static void downloadmusiclrc (string name, string artist) {downloadgooglelrctool. downloadlrcfromgoogle (name, artist );}}

Test results:

Downloading the song Battle Room-Pan yibai lyrics...
Download the song Battle Room-Pan yibai lyrics!
Location:/project/downloadlrc/Battle Room. LRC


The lyrics are as follows:

[Ti: Battle Room]
[AR: Pan baibai]
[Al: Master]
[00: 05. 00]
[. 44] [. 69] unable to restrain heartbeat Acceleration
[. 27] [. 67] I am so excited that I want to fight.
[. 25] [. 47] Focus on the corner
[. 25] [. 57] cold eyes look like a dangerous tiger
[. 23] [. 58] The Fist band cannot be bound.
[. 25] [. 55] the intensity of the beating is fast
[. 22] [. 51] The lights are shouting in the pop-up crowd.
[. 19] [. 48] I'm totally dismissive of raising my head.
[. 21] [. 60] noboby gonna stop me
[. 21] [. 57] Listen to yourself in silence
[. 07] [. 54] noboby gonna front me
[. 23] [. 51] Believe that you will never escape
[. 25] [. 55] noboby gonna touch me
[. 19] [. 48]
[. 12] [. 41] Open the door and walk yourself out.
[. 06] [. 22] This is my life my soul
[. 39] [. 08] [. 15] [. 47] clench the soul's hand
[. 75] [. 93] [. 03] Last minute battle
[. 86] [. 96] [. 10] [. 38] in fear of darkness
[. 61] [. 71] [. 84] [. 19] only you and me are left in the Battle Room.
[. 86] [. 86] [. 09] [. 38] countdown clock
[. 77] [. 90] [. 00] [. 30] Victory ends
[. 37] [. 51] [. 59] [. 84] I closed my eyes and stood in the fighting room.
[. 29] [. 35] [. 50] [. 94]


Then you can put the video on your mobile phone!

Done !!!




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.