From c2ff546e3d19ccf75a164ac61a203ca682047011 Mon Sep 17 00:00:00 2001 From: khj xiaogu <54445543+khjxiaogu@users.noreply.github.com> Date: Tue, 25 Aug 2020 02:57:19 +0800 Subject: [PATCH] added while to ensure success --- .../musicsource/BaiduMusicSource.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/khjxiaogu/MiraiSongPlugin/musicsource/BaiduMusicSource.java b/src/main/java/com/khjxiaogu/MiraiSongPlugin/musicsource/BaiduMusicSource.java index 8f613ee..e973ee6 100644 --- a/src/main/java/com/khjxiaogu/MiraiSongPlugin/musicsource/BaiduMusicSource.java +++ b/src/main/java/com/khjxiaogu/MiraiSongPlugin/musicsource/BaiduMusicSource.java @@ -16,26 +16,36 @@ public BaiduMusicSource() { @Override public MusicInfo get(String keyword) throws Exception { - - HttpURLConnection huc=(HttpURLConnection) new URL("http://tingapi.ting.baidu.com/v1/restserver/ting?format=json&calback=&from=webapp_music&method=baidu.ting.search.catalogSug&query="+keyword).openConnection(); + JsonObject jo; + HttpURLConnection huc; + do { + huc=(HttpURLConnection) new URL("http://tingapi.ting.baidu.com/v1/restserver/ting?format=json&calback=&from=webapp_music&method=baidu.ting.search.catalogSug&query="+keyword).openConnection(); huc.setRequestProperty("Host","tingapi.ting.baidu.com"); + huc.setRequestProperty("Referrer","http://http://music.taihe.com/"); + huc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36"); huc.setRequestMethod("GET"); huc.connect(); - JsonObject jo=JsonParser.parseString(new String(Utils.readAll(huc.getInputStream()), "UTF-8")).getAsJsonObject(); + jo=JsonParser.parseString(new String(Utils.readAll(huc.getInputStream()), "UTF-8")).getAsJsonObject(); + }while(jo.get("error_code").getAsInt()!=22000); String sid=jo.getAsJsonObject() .getAsJsonObject().get("song").getAsJsonArray().get(0).getAsJsonObject().get("songid") .getAsString(); - huc=(HttpURLConnection) new URL("http://tingapi.ting.baidu.com/v1/restserver/ting?format=json&calback=&from=webapp_music&method=baidu.ting.song.playAAC&songid="+sid).openConnection(); + huc.disconnect(); + huc=(HttpURLConnection) new URL("http://tingapi.ting.baidu.com/v1/restserver/ting?format=json&calback=&from=webapp_music&method=baidu.ting.song.play&songid="+sid).openConnection(); huc.setRequestProperty("Host","tingapi.ting.baidu.com"); + huc.setRequestProperty("Referrer","http://http://music.taihe.com/"); + huc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36"); huc.setRequestMethod("GET"); huc.connect(); JsonObject allinfo=JsonParser.parseString(new String(Utils.readAll(huc.getInputStream()), "UTF-8")).getAsJsonObject(); JsonObject sif=allinfo.getAsJsonObject().get("songinfo").getAsJsonObject(); + huc.disconnect(); return new MusicInfo(sif.get("title").getAsString(), sif.get("author").getAsString(), sif.get("pic_big").getAsString(), allinfo.get("bitrate").getAsJsonObject().get("file_link").getAsString(), sif.get("share_url").getAsString(), + "千千静听"); }