package cn.edu.nju.software.netlyric;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class NetLyric {
private URL url;
private String artist;
private String title;
private String mlyric = "";
public NetLyric(String art, String tit) {
artist = art;
title = tit;
url = generateURL();
}
public NetLyric(String s) {
String url1 = "http://mp3.baidu.com/m?f=ms&tn=baidump3lyric&ct=150994944&lf=2&rn=10&word=";
String url3 = "&lm=-1";
// http://mp3.sogou.com/lyric.so?query=andy&class=3&st=&ac=&pf=&_asf=&_ast=&p=&w=0
// String url1 = "http://mp3.sogou.com/lyric.so?query=";
// String url3 = "&class=3&st=&ac=&pf=&_asf=&_ast=&p=&w=0";
try {
String s1 = URLEncoder.encode(s, "GB2312");
url = new URL(url1 + s1 + url3);
} catch (Exception e1) {
e1.printStackTrace();
}
}
public void setKey(String s) {
String url1 = "http://mp3.baidu.com/m?f=ms&tn=baidump3lyric&ct=150994944&lf=2&rn=10&word=";
String url3 = "&lm=-1";
try {
String s1 = URLEncoder.encode(s, "GB2312");
url = new URL(url1 + s1 + url3);
} catch (Exception e1) {
e1.printStackTrace();
}
}
protected URL generateURL() {
String url1 = "http://mp3.baidu.com/m?f=ms&tn=baidump3lyric&ct=150994944&lf=2&rn=10&word=";
String url3 = "&lm=-1";
try {
String s = URLEncoder.encode(artist + " " + title, "GB2312");
url = new URL(url1 + s + url3);
} catch (Exception e) {
e.printStackTrace();
}
return url;
}
private void fetchLyric() {
StringBuffer sb = new StringBuffer();
try {
Scanner in = new Scanner(url.openConnection().getInputStream(),
"GB2312");
for (; in.hasNextLine();) {
sb.append(in.nextLine() + "\n");
}
mlyric = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
private String explainLyric() {
try {
Pattern regex = Pattern
.compile(
"<div style=\"padding-left:10px;line-height:20px;padding-top:1px\">.*?</div>",
Pattern.DOTALL);
Matcher matcher = regex.matcher(mlyric);
while (matcher.find()) {
mlyric = mlyric.substring(matcher.start(), matcher.end());
// mlyric = matcher.group();
mlyric = mlyric
.replaceAll(
"<div style=\"padding-left:10px;line-height:20px;padding-top:1px\">|</div>",
" ");
if (mlyric == null)
return "";
mlyric = mlyric.replaceAll("<br>", "\n");
if (mlyric == null)
return "";
mlyric = mlyric.replaceAll(
"<font style=color:#e10900>|</font>", " ");
if (mlyric == null)
return "";
mlyric = removeTagFromText(mlyric);
mlyric = removeTag(mlyric);
mlyric.replaceAll("\u005Bti:", "");
mlyric.replaceAll("\u005Bar:", "");
mlyric.replaceAll("\u005Bal:", "");
mlyric.replaceAll("]", "");
mlyric.replaceAll("'", "'");
}
} catch (Exception e) {
e.printStackTrace();
}
if (mlyric == null)
mlyric = "MO";
return mlyric;
}
public String getLyric() {
fetchLyric();
String s = explainLyric();
return s;
}
public String removeTagFromText(String content) {
Pattern p = null;
Matcher m = null;
String value = null;
// <> "<[^<>]*>"
p = Pattern.compile("(<[^<>].*?>)",Pattern.DOTALL);
m = p.matcher(content);
String temp = content;
while (m.find()) {
value = m.group(0);
temp = temp.replace(value, "");
}
return temp;
}
public String removeTag(String content) {
int left = 0;
for (int i = 0; i < content.length(); i++) {
if (content.charAt(i) == '<')
left = i;
else if (content.charAt(i) == '>'){
String leftstr = content.substring(0, left);
String rightstr = content.substring(i+1,content.length());
content = leftstr + rightstr;
}
else if (content.charAt(i) == ']'){
String leftstr = content.substring(0, i);
String rightstr = content.substring(i+1,content.length());
content = leftstr + rightstr;
}
else if(content.charAt(i) == '['){
if(content.charAt(i+1) == 't'){
String leftstr = content.substring(0, i);
String rightstr = content.substring(i+4,content.length());
content = leftstr + "" + rightstr;
}
if(content.charAt(i+1) == 'a' && content.charAt(i+2) == 'r'){
String leftstr = content.substring(0, i);
String rightstr = content.substring(i+4,content.length());
content = leftstr + "" + rightstr;
}
if(content.charAt(i+1) == 'a' && content.charAt(i+2) == 'l'){
String leftstr = content.substring(0, i);
String rightstr = content.substring(i+4,content.length());
content = leftstr + "" + rightstr;
}
}
else if(content.charAt(i) == '&' && content.charAt(i+1) == '#'
&& content.charAt(i+2) == '3' && content.charAt(i+3) == '9'
&& content.charAt(i+4) == ';'){
String leftstr = content.substring(0, i);
String rightstr = content.substring(i+5,content.length());
content = leftstr + "'" + rightstr;
}
}
return content;
}
}
|