歌曲B方案编码
This commit is contained in:
parent
c2f19ee805
commit
3d675cfa1b
@ -0,0 +1,35 @@
|
||||
package com.qiuguo.iot.third.resp;
|
||||
|
||||
import java.util.List;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* XXX
|
||||
*
|
||||
* @author weiyachao
|
||||
* @since 2023/9/28 14:03
|
||||
*/
|
||||
@Data
|
||||
public class SearchResponse {
|
||||
|
||||
private Result result;
|
||||
|
||||
private Integer code;
|
||||
|
||||
|
||||
|
||||
@Data
|
||||
public static class Result{
|
||||
|
||||
private List<Song> songs;
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Song{
|
||||
|
||||
private String id;
|
||||
|
||||
private String name;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,32 @@
|
||||
package com.qiuguo.iot.third.resp;
|
||||
|
||||
import java.util.List;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* XXX
|
||||
*
|
||||
* @author weiyachao
|
||||
* @since 2023/9/28 14:26
|
||||
*/
|
||||
@Data
|
||||
public class SingerResponse {
|
||||
|
||||
private Result result;
|
||||
|
||||
private Integer code;
|
||||
|
||||
@Data
|
||||
public static class Result{
|
||||
|
||||
private List<Artist> artists;
|
||||
|
||||
@Data
|
||||
public static class Artist{
|
||||
|
||||
private String id;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,27 @@
|
||||
package com.qiuguo.iot.third.resp;
|
||||
|
||||
import java.util.List;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* XXX
|
||||
*
|
||||
* @author weiyachao
|
||||
* @since 2023/9/28 14:33
|
||||
*/
|
||||
@Data
|
||||
public class SingerSongsResponse {
|
||||
|
||||
private Integer code;
|
||||
|
||||
private List<Song> songs;
|
||||
|
||||
@Data
|
||||
public static class Song{
|
||||
|
||||
private String id;
|
||||
|
||||
private String name;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
package com.qiuguo.iot.third.resp;
|
||||
|
||||
import java.util.List;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* XXX
|
||||
*
|
||||
* @author weiyachao
|
||||
* @since 2023/9/28 14:12
|
||||
*/
|
||||
@Data
|
||||
public class SongInfoResponse {
|
||||
|
||||
private List<ResultSong> data;
|
||||
|
||||
private Integer code;
|
||||
|
||||
@Data
|
||||
public static class ResultSong{
|
||||
|
||||
private String id;
|
||||
|
||||
private String url;
|
||||
|
||||
private String name;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,106 @@
|
||||
package com.qiuguo.iot.third.service;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.qiuguo.iot.third.resp.SingerResponse;
|
||||
import com.qiuguo.iot.third.resp.SingerSongsResponse;
|
||||
import com.qiuguo.iot.third.resp.SongInfoResponse;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import reactor.core.publisher.Mono;
|
||||
import com.qiuguo.iot.third.resp.SearchResponse;
|
||||
|
||||
import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE;
|
||||
import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON_VALUE;
|
||||
|
||||
/**
|
||||
* XXX
|
||||
*
|
||||
* @author weiyachao
|
||||
* @since 2023/9/28 10:40
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class MusicService {
|
||||
|
||||
// 网易云音乐
|
||||
public Mono<List<SongInfoResponse.ResultSong>> searchMusic(String keyword, Integer type) {
|
||||
if (ObjectUtils.isEmpty(type)) {
|
||||
type = 1;
|
||||
}
|
||||
if (ObjectUtils.isEmpty(keyword)) {
|
||||
keyword = "追梦赤子心";
|
||||
}
|
||||
WebClient build = WebClient.builder().defaultHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON_VALUE).build();
|
||||
String baseUrl = "http://bilibiliz.cn:8031";
|
||||
if (type == 1) {
|
||||
return build.get().uri(baseUrl.concat("/search?keywords=").concat(keyword).concat("&limit=10")).retrieve()
|
||||
.bodyToMono(SearchResponse.class).flatMap(res -> {
|
||||
if (!Objects.equals(res.getCode(), 200)) {
|
||||
return Mono.empty();
|
||||
}
|
||||
String ids = res.getResult().getSongs().stream().map(SearchResponse.Song::getId)
|
||||
.collect(Collectors.joining(","));
|
||||
Map<String, String> songMap = res.getResult().getSongs().stream()
|
||||
.collect(Collectors.toMap(SearchResponse.Song::getId, SearchResponse.Song::getName));
|
||||
|
||||
return build.get().uri(baseUrl.concat("/song/url?id=").concat(ids)).retrieve()
|
||||
.bodyToMono(SongInfoResponse.class).flatMap(song -> {
|
||||
if (Objects.equals(200, song.getCode()) && song.getData().size() > 0) {
|
||||
|
||||
List<SongInfoResponse.ResultSong> data = song.getData();
|
||||
for (SongInfoResponse.ResultSong datum : data) {
|
||||
datum.setName(songMap.get(datum.getId()));
|
||||
}
|
||||
return Mono.just(data);
|
||||
}
|
||||
return Mono.empty();
|
||||
});
|
||||
});
|
||||
} else {
|
||||
return build.get().uri(baseUrl.concat("/search?keywords=").concat(keyword).concat("&type=100")).retrieve()
|
||||
.bodyToMono(SingerResponse.class).flatMap(res -> {
|
||||
if (!Objects.equals(res.getCode(), 200) && res.getResult().getArtists().size() == 0) {
|
||||
return Mono.empty();
|
||||
}
|
||||
String id = res.getResult().getArtists().get(0).getId();
|
||||
return build.get().uri(baseUrl.concat("/artist/top/song?id=").concat(id)).retrieve()
|
||||
.bodyToMono(SingerSongsResponse.class).flatMap(song -> {
|
||||
if (Objects.equals(200, song.getCode()) && song.getSongs().size() > 0) {
|
||||
String ids = song.getSongs().stream().limit(10)
|
||||
.map(SingerSongsResponse.Song::getId).collect(Collectors.joining(","));
|
||||
Map<String, SingerSongsResponse.Song> songMap = song.getSongs().stream()
|
||||
.collect(Collectors.toMap(SingerSongsResponse.Song::getId,
|
||||
Function.identity()));
|
||||
return build.get().uri(baseUrl.concat("/song/url?id=").concat(ids)).retrieve()
|
||||
.bodyToMono(SongInfoResponse.class).flatMap(so -> {
|
||||
if (Objects.equals(200, song.getCode())
|
||||
&& so.getData().size() > 0) {
|
||||
List<SongInfoResponse.ResultSong> data = so.getData();
|
||||
for (SongInfoResponse.ResultSong datum : data) {
|
||||
datum.setName(songMap.get(datum.getId()).getName());
|
||||
}
|
||||
return Mono.just(data);
|
||||
}
|
||||
return Mono.empty();
|
||||
});
|
||||
}
|
||||
return Mono.empty();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,35 @@
|
||||
package com.qiuguo.iot.user.api.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.qiuguo.iot.third.resp.SongInfoResponse;
|
||||
import com.qiuguo.iot.third.service.MusicService;
|
||||
import java.util.List;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* XXX
|
||||
*
|
||||
* @author weiyachao
|
||||
* @since 2023/9/28 12:28
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/music")
|
||||
public class MusicController {
|
||||
|
||||
@Autowired
|
||||
private MusicService musicService;
|
||||
|
||||
@GetMapping("/search")
|
||||
public Mono<List<SongInfoResponse.ResultSong>> search(String name,
|
||||
@RequestParam(required = false, defaultValue = "1") Integer type) {
|
||||
|
||||
return musicService.searchMusic(name,type);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user