最近写代码有点腰酸脖疼的,想听听歌,结果发现好多歌曲都要充值会员~~~

花了点时间,撸了一把网站,发现酷我还比较好撸,就靠它了~~~

上代码:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.djk.local.utils.HttpUtil;
import com.djk.local.utils.SSLTrustUtil;
import lombok.extern.slf4j.Slf4j;import javax.sound.sampled.*;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;@Slf4j
public class GetDataFromKuwo {public static List<JSONObject> searchMusicFromKeyword(String keyword, final int size) {String searchUrl;String encodeKey = null;try {encodeKey = URLEncoder.encode(keyword, "utf-8");} catch (UnsupportedEncodingException e) {log.error("URL转码出错", e);}searchUrl = String.format("http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key=%s&pn=1&rn=20&httpsStatus=1", encodeKey);Map<String, String> headers = new HashMap<>();headers.put("Cookie", "kw_token=F83QZ3U6PQB");headers.put("csrf", "F83QZ3U6PQB");headers.put("Referer", String.format("http://www.kuwo.cn/search/list?key=%s", encodeKey));Map<String, Object> stringObjectMap = HttpUtil.get(searchUrl, headers);String jsonResponse = String.valueOf(stringObjectMap.get("responseBody"));try {JSONObject jsonObject = (JSONObject) JSON.parse(jsonResponse);JSONObject data = (JSONObject) jsonObject.get("data");List<JSONObject> list = (List) data.get("list");final AtomicInteger atomicInteger = new AtomicInteger();return list.stream().filter(item -> (atomicInteger.getAndIncrement() <= size - 1)).collect(Collectors.toList());} catch (Exception e) {throw new RuntimeException("解析json出错,\n" + jsonResponse, e);}}public static JSONObject getMusicInfoFromRid(String rid) {String infoUrl = String.format("http://www.kuwo.cn/api/v1/www/music/playUrl?mid=%s&type=convert_url&httpsStatus=1&br=320kmp3", rid);Map<String, Object> stringObjectMap = HttpUtil.get(infoUrl, null);String musicInfoResp = String.valueOf(stringObjectMap.get("responseBody"));try {JSONObject musicJson = (JSONObject) JSON.parse(musicInfoResp);return (JSONObject) musicJson.get("data");} catch (Exception e) {throw new RuntimeException("解析json出错,\n" + musicInfoResp, e);}}public static void playMusic(String url) {try {URL urlObj = new URL(url);SSLTrustUtil.trustSSL(url);URLConnection connection = urlObj.openConnection();connection.setDoInput(true);connection.setDoOutput(true);connection.setUseCaches(false);connection.setRequestProperty("connection", "Keep-Alive");InputStream inputStream = connection.getInputStream();AudioInputStream stream = AudioSystem.getAudioInputStream(inputStream);AudioFormat format = stream.getFormat();format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,format.getSampleRate(),16,format.getChannels(),format.getChannels() * 2,format.getSampleRate(), false);AudioInputStream audio = AudioSystem.getAudioInputStream(format, stream);DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);line.open(format);line.start();int bytesRead = -1;byte[] buffer = new byte[1024];while ((bytesRead = audio.read(buffer)) > 0) {line.write(buffer, 0, bytesRead);}audio.close();line.stop();line.close();} catch (Exception e) {throw new RuntimeException("播放出错", e);}}public static void main(String[] args) {//单曲循环while (true) {try {String searchKeyword = "三年的赌债";int limit = 1;List<JSONObject> ret = searchMusicFromKeyword(searchKeyword, limit);JSONObject item = ret.get(0);String rid = String.valueOf(item.get("rid"));JSONObject musicInfo = getMusicInfoFromRid(rid);String url = String.valueOf(musicInfo.get("url"));log.error("搜索关键词:{},下载地址:{}", searchKeyword, url);playMusic(url);} catch (Exception e) {log.error(e.getMessage(), e);}}}
}

依赖包:

<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.47</version>
</dependency>
<dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>3.14.8</version>
</dependency>
<dependency><groupId>com.googlecode.soundlibs</groupId><artifactId>mp3spi</artifactId><version>1.9.5.4</version>
</dependency>

好了,搞定了哈~~~~ 欢迎大家留言,都喜欢听啥歌?推荐一把~~~

最新文章

  1. 2016河北省职称计算机考试试题及答案,2016年最新河北省职称计算机考试试题及答案概要.doc...
  2. 新的MOVE结构,和在项目中实际的感受
  3. [C++基础]001_iostream和iostream.h的区别
  4. lua c语言混合编程入门
  5. 开源混合云:harvester混合应用架构
  6. Caffe阅读代码并修改
  7. 悼念程序员的那些难忘时刻 漫画组图(一)
  8. 嵌套组合图echarts
  9. 网站性能测试工具--MS Web Application Stress Tool
  10. MyBatis框架的使用及源码分析(三) 配置篇 Configuration
  11. 基于ssh的远程连接服务器
  12. 安徽师大附中%你赛day4T1 金字塔 解题报告
  13. hive 数据类型 java_Hive数据类型
  14. 海外手机号码正则匹配
  15. 洪水填充算法_洪水填充算法分析
  16. Python3下的NLTK及nltk_data安装问题(Ubuntu环境)
  17. 老虎过河编程java_2017年拓展游戏《老虎过河》
  18. 售后单导出(抖音、快手)
  19. UGUI内核大探究(十三)Dropdown
  20. 七夕节基于Qt Android给女朋友做个转盘抽礼物的App

热门文章

  1. 四足机器人——机械结构
  2. 对国内ASP应用不乐观
  3. 【Java】IDEA插件推荐,让你的开发效率翻倍
  4. idea非活动变更列表中的文件被修改_互动活动策划别抓瞎,用这13条套路搞定!...
  5. 计算机作业批改反思,作业批改检查教学反思
  6. db dbm dbw dbi
  7. bim常用出图软件的【标注避让】【洞口标注】功能
  8. Lasagne 实现并测试MNIST
  9. 用Python实现栈---1顺序表:牛客题解 AB1 【模板】栈
  10. 自考之操作系统大题解析