admin管理员组文章数量:1583572
周末在家打算下点歌在U盘,开车的时候可以听听,打开企鹅家的音乐准备下载,结果需要开通会员,看了一下8块钱,忍痛开了1个月。
开完会员以为就可以下载了?,谁知道下载下来的格式不是MP3的,是只有用企鹅的播放器才能播放。
在网上找了一圈,发现一个免费下载的网站。
可惜里只能一首一首的搜索,一首一首的下载,效率太慢了。
想到平时工作中用到的技能,看看能不能抓到他的接口,直接调用接口下载应该可以。
准备开干。
打开企鹅音乐的网站,然后选好对应的歌手,页面上会有一串乱七八糟的字符串,把这个字符串应该是歌手的代号。把它替换到下面的接口中singermid就可以换歌手,从返回的json中拿到songmid,下面的接口需要用到
https://c.y.qq/v8/fcg-bin/fcg_v8_singer_track_cp.fcg?singermid=0025NhlN2yWrP4&order=listen&begin=0&num=50&songstatus=1
抓到下载的接口,发现传入的ID就和企鹅的songmid是一样的。
这个也很简单,把企鹅那个接口返回的songmid传给ID就可以了。然后会返回一个下载地址。
上面准备工作作完了,就开始撸点简单的代码。
public class HttpConnectionUtil {
/**
*
* @param urlPath
* 下载路径
* @param downloadDir
* 下载存放目录
* @return 返回下载文件
*/
public static File downloadFile(String urlPath, String downloadDir,String name) {
System.out.println(urlPath);
System.out.println(downloadDir);
System.out.println(name);
File file = null;
try {
// 统一资源
URL url = new URL(urlPath);
// 连接类的父类,抽象类
URLConnection urlConnection = url.openConnection();
// http的连接类
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
// 设定请求的方法,默认是GET
httpURLConnection.setRequestMethod("GET");
// 设置字符编码
httpURLConnection.setRequestProperty("Charset", "UTF-8");
// 打开到此 URL 引用的资源的通信链接(如果尚未建立这样的连接)。
httpURLConnection.connect();
// 文件大小
int fileLength = httpURLConnection.getContentLength();
// 文件名
String filePathUrl = httpURLConnection.getURL().getFile();
String fileFullName = filePathUrl.substring(filePathUrl.lastIndexOf(File.separatorChar) + 1);
URLConnection con = url.openConnection();
BufferedInputStream bin = new BufferedInputStream(httpURLConnection.getInputStream());
String path = downloadDir+name+".mp3";
file = new File(path);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
OutputStream out = new FileOutputStream(file);
int size = 0;
int len = 0;
byte[] buf = new byte[1024];
double downloadNum;
while ((size = bin.read(buf)) != -1) {
len += size;
out.write(buf, 0, size);
// 打印下载百分比
// downloadNum = (double)len * 100 / fileLength;
// System.out.println("下载了-------> " + String.format("%.2f", downloadNum) +
// "%\n");
}
bin.close();
out.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
return file;
}
}
public static void main(String[] args) {
String url = "https://c.y.qq/v8/fcg-bin/fcg_v8_singer_track_cp.fcg?"
+ "singermid=0025NhlN2yWrP4&order=listen&begin=0&num=50&songstatus=1";
getJson(getSongName(url));
}
public static void getJson(String json) {
JSONObject jsonObject = new JSONObject(json);
JSONObject jsonObject1 = jsonObject.getJSONObject("data");
JSONArray jsonArray1 = jsonObject1.getJSONArray("list");
JSONObject musicData;
JSONObject songname;
String url ="http://music.fooor/api.php?callback=jQuery111309747111397654677_1541733898449";
for(int i=0; i<jsonArray1.length();i++) {
musicData = jsonArray1.getJSONObject(i);
songname = musicData.getJSONObject("musicData");
String response = SentHttp.httpPost(url, "types=url&id="+songname.get("songmid")+"&source=tencent");
String[] a = response.split("\\(");
String[] b = a[1].split("\\)");
downloadFile(getUrl(b[0]), "E:\\mp3\\",(String)songname.get("songname"));
}
}
public static String getUrl(String js) {
JSONObject jsonObject = new JSONObject(js);
return (String) jsonObject.get("url");
}
public static String getSongName(String url) {
return SentHttp.httpGet(url);
}
}
public class SentHttp {
public static String httpPost(String url,String par) {
JSONObject json = null;
//初始化httpclient
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//初始化response
CloseableHttpResponse httpResponse = null;
//设置post请求
HttpPost post = new HttpPost(url);
//设置请求header
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
//post.setHeader("sign", sign);
//设置请求body,使用byte类型
String re=null;
try {
post.setEntity(new StringEntity(par));
//获取返回值
httpResponse = httpClient.execute(post);
//获取状态码
StatusLine sta = httpResponse.getStatusLine();
//获取返回的body
HttpEntity entity = httpResponse.getEntity();
//获取body内容
re = EntityUtils.toString(entity);
if(sta.getStatusCode() != 200) {
System.out.println("请求错误");
}else {
//json = new JSONObject(re);
}
httpClient.close();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return re.toString();
}
public static String httpGet(String url) {
JSONObject json = null;
//初始化httpclient
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//初始化response
CloseableHttpResponse httpResponse = null;
//设置get请求
HttpGet get = new HttpGet(url);
String re=null;
try {
//获取返回值
httpResponse = httpClient.execute(get);
//获取状态码
StatusLine sta = httpResponse.getStatusLine();
//获取返回的body
HttpEntity entity = httpResponse.getEntity();
//获取body内容
re = EntityUtils.toString(entity);
if(sta.getStatusCode() != 200) {
System.out.println("请求错误");
}else {
//json = new JSONObject(re);
}
httpClient.close();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return re.toString();
}
}
写的有点乱,也顺便练习一下写作能力?
版权声明:本文标题:下载MP3格式音乐 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1727919237a1138021.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论