下載MP3格式音樂

週末在家打算下點歌在U盤,開車的時候可以聽聽,打開企鵝家的音樂準備下載,結果需要開通會員,看了一下8塊錢,忍痛開了1個月。
開完會員以爲就可以下載了😭,誰知道下載下來的格式不是MP3的,是隻有用企鵝的播放器才能播放。
在網上找了一圈,發現一個免費下載的網站。
在這裏插入圖片描述

可惜裏只能一首一首的搜索,一首一首的下載,效率太慢了。
想到平時工作中用到的技能,看看能不能抓到他的接口,直接調用接口下載應該可以。
準備開幹。

打開企鵝音樂的網站,然後選好對應的歌手,頁面上會有一串亂七八糟的字符串,把這個字符串應該是歌手的代號。把它替換到下面的接口中singermid就可以換歌手,從返回的json中拿到songmid,下面的接口需要用到
https://c.y.qq.com/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.com/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.cn/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();
    }

}

寫的有點亂,也順便練習一下寫作能力😄

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章