首先向大家推荐一个github上的一个项目,是关于网易云音乐的一个APIservice,这个小的自动获取的程序就是建立在这个项目的基础之上的
项目地址
程序需要的基础
- 电脑上已将找好了node.js
- 使用npm install,或者也可以使用yarn install安装项目需要的依赖文件
- 下载解析json的jar包,这里我使用的是JSON.jar
出现类似这样的界面就是项目启动成功了,可以在浏览器中进行访问
通过查看文档可以得到,得到音乐下载地址的步骤是
我们得到的文件是一个json文件,于是我们就需要使用java的json库进行解析得到我们想要的文件
package RTTI;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class JSON {
public static Map<String, Integer> getSongId(String fileName){
Map<String, Integer> songMap = new HashMap<>();
try {
BufferedReader reader = new BufferedReader( new FileReader(fileName));
String data = reader.readLine();
JSONObject jo = new JSONObject(data);
JSONObject jo1 = jo.getJSONObject("playlist");
Boolean b = jo1.getBoolean("subscribed");
System.out.println(b);
JSONArray jo2 = jo1.getJSONArray("tracks");
for(int i=0; i<jo2.length(); i++){
JSONObject temp = (JSONObject) jo2.get(i);
int id = temp.getInt("id");
String name = temp.getString("name");
songMap.put(name, id);
}
return songMap;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null ;
}
public static String getSongUrl(String fileName){
String url = "" ;
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String data = reader.readLine();
JSONObject jo = new JSONObject(data);
JSONArray ja = jo.getJSONArray("data");
JSONObject jo1 = ja.getJSONObject(0);
Object object = jo1.get("url");
if(object instanceof String){
System.out.println("ok");
return (String)object;
}else
return "";
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return url ;
}
}
上面的类是用来的解析json文件,得到自己需要的id的
package RTTI;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Set;
public class Download {
public static void downLoadFromUrl(String urlStr,String fileName,String savePath) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置超时间为3秒
conn.setConnectTimeout(3*1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
InputStream inputStream = conn.getInputStream();
//获取自己数组
byte[] getData = readInputStream(inputStream);
//文件保存位置
File saveDir = new File(savePath);
if(!saveDir.exists()){
saveDir.mkdir();
}
File file = new File(saveDir+File.separator+fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
if(fos!=null){
fos.close();
}
if(inputStream!=null){
inputStream.close();
}
System.out.println("info:"+url+" download success");
}
/**
* 从输入流中获取字节数组
* @param inputStream
* @return
* @throws IOException
*/
public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
public static void main(String[] args) {
FileWriter fileWriter = null ;
try {
fileWriter = new FileWriter("d:\\url.txt");
} catch (IOException e) {
System.out.println("文件创建失败");
}
//通过第一次获得的是关于歌曲的name和对应的id
Map<String, Integer> songMap = JSON.getSongId("d:\\id.json");
Set name = songMap.keySet();
Object[] names = name.toArray();
for(int i=0; i<names.length; i++){
//然后我们通过歌曲的id,循环得到歌曲对应的具体的json文件
//其中就包含了我们想要的下载地址的url
try{
downLoadFromUrl("http://127.0.0.1:3000/song/url?id="+songMap.get(names[i]),
"temp.json","d:/");
}catch (Exception e) {
// TODO: handle exception
}
//通过第二次解析,我们的到的是所有歌曲的json文件中的url
String url = JSON.getSongUrl("D:\\temp.json");
if(url=="") continue;
try {
//进行保存歌曲文件
downLoadFromUrl(url, names[i]+".mp3", "e:/song");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
首先的得到了一个歌单的json文件,就是在主函数中的d:\id.json
上面这个类实现的功能是通过得到的url地址将文件保存到本地
最后贴上一张效果图