songtaste是一个非常好的音乐网站,主要是基于SNS的音乐分享主题,让小众的好音乐得到更好的分享,遗憾的是在国内版权保护不力,公司于2015年关闭,现在的首页:
但是点击音乐还是可以找到音乐列表:
任意点击进入一个音乐播放的页面:
无法播放,查看页面的代码:
原来是被注释掉了,分析音乐下载的逻辑:
音乐url请求资源逻辑:
function flashplay(strURL,st_songid,t) {
$.ajax({
type:'POST',
url:'/time.php',
cache:false,
data:'str='+strURL+'&sid='+st_songid+'&t='+t,
dataType:'html',
success:function(data){
if(data){
var obj = {
name:"seeing ghosts〈 小小迷幻系列⒈ 〉",
url:data,
isShowLogo:"0" ,
isAutoReplay:1,
isAutoPlay:0
};
setSongInfo(obj);
if(data.indexOf('duomi.com') > 0){
$("#show_logo").show();
}
} },
error:function(data){
}
});
}
关键在于获取strURL,st_songid,t三个参数。这三个参数随着音乐的ID生成。然后解析页面标签。
最后可以拿到音乐源文件的url,用流的方式下载到本地,完整java代码:
public class Songtaste {
private String strUrl, st_songid, t;
private String musicName;
private static final String outFilePath = "H:" + File.separator + "songtaste download" + File.separator;
// 文件后缀
public static final String FILE_SUFFIX = ".mp3";
// 信息分割
public static final String INFO_SEPARETE = "-------";
public static void main(String[] args) {
Songtaste songtaste = new Songtaste();
boolean isDownload = songtaste
.downloadFromSongtastePageUrl("http://www.songtaste.com/song/650417/");
if (isDownload)
System.out.println("下载成功");
else
System.out.println("下载失败");
}
public boolean downloadFromSongtastePageUrl(String url) {
try {
String htmlContext = this.sendRequest(url);
this.setDuoMiUrlBySongtasteHtml(htmlContext);
String duomiUrl = getDuomiUrl();
download(duomiUrl);
} catch (Exception ex) {
System.out.println("错误信息" + ex.getMessage());
return false;
}
return true;
}
private String getDuomiUrl() throws Exception {
String requestDuomiUrl = "http://www.songtaste.com" + "/time.php?str="
+ this.strUrl + "&sid=" + this.st_songid + "&t=" + this.t;
return this.sendRequest(requestDuomiUrl);
}
// 处理下载任务
public void download(String downloadUrl) throws Exception {
if (downloadUrl.contains("404.html")) {
throw new Exception("歌曲资源不存在");
}
URL url = new URL(downloadUrl);
System.out.println(Songtaste.INFO_SEPARETE);
System.out.println("下载链接为:" + downloadUrl);
System.out.println(Songtaste.INFO_SEPARETE);
URLConnection connection = url.openConnection();
connection.connect();
// 读取字节流写入文件
InputStream in = connection.getInputStream();
System.out.println(Songtaste.INFO_SEPARETE);
System.out.println("文件尺寸为:" + (double) connection.getContentLength()
/ (1024 * 1024) + "MB");
System.out.println(Songtaste.INFO_SEPARETE);
byte[] b = new byte[1024 * 10];
File file = new File(Songtaste.outFilePath + this.musicName + Songtaste.FILE_SUFFIX);
if (file.exists()) {
System.out.println("文件已存在");
return;
}
FileOutputStream out = new FileOutputStream(file);
int len;
int sum = 0;
int size = connection.getContentLength();
while ((len = in.read(b)) > 0) {
out.write(b, 0, len);
sum += len;
System.out.println("已下载" + (sum * 100 / size) + "%");
}
in.close();
out.close();
}
// 获取向多米发送请求的url
public void setDuoMiUrlBySongtasteHtml(String htmlContext) {
// 获取3个参数strURL,st_songid,t
String begainString = "<a href=\"javascript:playmedia1('playicon','player', '";
int begain = htmlContext.indexOf(begainString);
begain += begainString.length();
int end = htmlContext.indexOf(");ListenLog(");
String[] params = htmlContext.substring(begain, end).replace(" ", "")
.split(",");
this.strUrl = params[0].substring(0, params[0].length() - 1);
this.st_songid = params[5].substring(1, params[5].length() - 1);
this.t = params[6];
// 获取歌名
String name = htmlContext.substring(htmlContext.indexOf("mid_tit")
+ "mid_tit".length() + 2,
htmlContext.indexOf("</p>", htmlContext.indexOf("mid_tit")));
this.musicName = name;
System.out.println(Songtaste.INFO_SEPARETE);
System.out.println("歌名为:" + name);
System.out.println(Songtaste.INFO_SEPARETE);
}
public String sendRequest(String urlString) throws Exception {
System.out.println(Songtaste.INFO_SEPARETE);
System.out.println("发送url请求" + urlString);
System.out.println(Songtaste.INFO_SEPARETE);
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.setRequestProperty("connection", "keep-alive");
connection.setRequestProperty("accept", "*/*");
connection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String oneLine = null;
StringBuffer htmlContext = new StringBuffer();
while ((oneLine = reader.readLine()) != null) {
if (oneLine.length() > 2 && (oneLine.substring(0, 2).equals("//"))) {
oneLine = oneLine.substring(2);
}
htmlContext.append(oneLine).append("\n");
}
reader.close();
return htmlContext.toString();
}
}
在main函数中替换想要下载的详情连接
-------
发送url请求http://www.songtaste.com/song/1620684/
-------
-------
歌名为:声をきかせて
-------
-------
发送url请求http://www.songtaste.com/time.php?str=f29562a30e0c8dca8ee79f77e930562eebbc55988262bfeecc25d11cb385297633163f17aa9d4dd82f1ced3fe51ffc51&sid=1620684&t=1
-------
-------
下载链接为:http://mb.songtaste.com/201606191247/24300369f204365ce0f56d54ab528369/b/b8/b80c4cfb07ddb0c705b4ad48eaca1cb7.mp3
-------
-------
文件尺寸为:3.94814395904541MB
-------
已下载0%
....
已下载99%
已下载100%
下载成功
如果你也是songtaste爱好者,可以加入群:33305126。欢迎联系。