From ba272e4b1bbc986bead779ba6633b57c819873e2 Mon Sep 17 00:00:00 2001 From: N1KO Date: Tue, 25 Nov 2025 14:23:27 +0800 Subject: [PATCH] music --- .../music/processor/FlacAudioProcessor.java | 25 ++++++++++----- .../service/impl/MusicInfoServiceImpl.java | 32 +++++++++++++++---- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/main/java/top/baogutang/music/processor/FlacAudioProcessor.java b/src/main/java/top/baogutang/music/processor/FlacAudioProcessor.java index d31f0a0..2fe1f06 100644 --- a/src/main/java/top/baogutang/music/processor/FlacAudioProcessor.java +++ b/src/main/java/top/baogutang/music/processor/FlacAudioProcessor.java @@ -1,6 +1,7 @@ package top.baogutang.music.processor; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.jaudiotagger.audio.AudioFile; import org.jaudiotagger.audio.exceptions.CannotReadException; import org.jaudiotagger.audio.exceptions.CannotWriteException; @@ -18,9 +19,7 @@ import top.baogutang.music.domain.res.download.MusicDownloadRes; import java.io.*; /** - * * @description: - * * @author: N1KO * @date: 2024/12/12 : 10:40 */ @@ -33,13 +32,23 @@ public class FlacAudioProcessor extends AbstractAudioProcessor { FlacFileReader flacFileReader = new FlacFileReader(); AudioFile audioFile = flacFileReader.read(file); Tag tag = audioFile.getTag(); - tag.setField(FieldKey.TITLE, res.getName()); - tag.setField(FieldKey.ALBUM, res.getAlbumName()); - tag.setField(FieldKey.ARTIST, res.getArtistName()); - tag.setField(FieldKey.LYRICS, res.getLyric()); + if (StringUtils.isNotBlank(res.getName())) { + tag.setField(FieldKey.TITLE, res.getName()); + } + if (StringUtils.isNotBlank(res.getAlbumName())) { + tag.setField(FieldKey.ALBUM, res.getAlbumName()); + } + if (StringUtils.isNotBlank(res.getArtistName())) { + tag.setField(FieldKey.ARTIST, res.getArtistName()); + } + if (StringUtils.isNotBlank(res.getLyric())) { + tag.setField(FieldKey.LYRICS, res.getLyric()); + } StandardArtwork artwork = new StandardArtwork(); - artwork.setImageUrl(res.getPic()); - artwork.setBinaryData(fetchImageData(res.getPic())); + if (StringUtils.isNotBlank(res.getPic())) { + artwork.setImageUrl(res.getPic()); + artwork.setBinaryData(fetchImageData(res.getPic())); + } tag.setField(artwork); FlacFileWriter flacFileWriter = new FlacFileWriter(); flacFileWriter.write(audioFile); diff --git a/src/main/java/top/baogutang/music/service/impl/MusicInfoServiceImpl.java b/src/main/java/top/baogutang/music/service/impl/MusicInfoServiceImpl.java index 4fd240e..b3fec1a 100644 --- a/src/main/java/top/baogutang/music/service/impl/MusicInfoServiceImpl.java +++ b/src/main/java/top/baogutang/music/service/impl/MusicInfoServiceImpl.java @@ -5,6 +5,8 @@ import com.fasterxml.jackson.core.type.TypeReference; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -37,12 +39,12 @@ import static top.baogutang.music.constants.CacheKey.KEY_MUSIC_TAG_TOKEN_PREFIX; /** * * @description: - * * @author: N1KO * @date: 2025/01/23 : 14:53 */ @Slf4j @Service +@RefreshScope public class MusicInfoServiceImpl implements IMusicInfoService { @Resource @@ -57,6 +59,24 @@ public class MusicInfoServiceImpl implements IMusicInfoService { @Resource(name = "commonExecutor") private Executor commonExecutor; + @Value("${baogutang.music-tag.token}") + private String tokenUrl; + + @Value("${baogutang.music-tag.token.param1:username}") + private String tokenParam1; + + @Value("${baogutang.music-tag.token.value1:admin}") + private String tokenValue1; + + @Value("${baogutang.music-tag.token.param2:password}") + private String tokenParam2; + + @Value("${baogutang.music-tag.token.value2:admin}") + private String tokenValue2; + + @Value(("${baogutang.music-tag.cover}")) + private String coverUrl; + @Override public void getCover(String title, String artist, String album, HttpServletResponse response) { MusicRecordEntity musicRecord = musicRecordService.queryByNameOrAlbumOrArtist(title, album, artist); @@ -99,11 +119,11 @@ public class MusicInfoServiceImpl implements IMusicInfoService { InfoRes> res = null; try { params.put("resource", "qmusic"); - res = OkHttpUtil.post("http://114.96.87.132:8002/apimt/fetch_id3_by_title/", headers, params, new TypeReference<>() { + res = OkHttpUtil.post(coverUrl, headers, params, new TypeReference<>() { }); if (Objects.isNull(res) || !Boolean.TRUE.equals(res.getResult()) || CollectionUtils.isEmpty(res.getData())) { params.put("resource", "netease"); - res = OkHttpUtil.post("http://114.96.87.132:8002/apimt/fetch_id3_by_title/", headers, params, new TypeReference<>() { + res = OkHttpUtil.post(coverUrl, headers, params, new TypeReference<>() { }); } } catch (Exception e) { @@ -115,11 +135,11 @@ public class MusicInfoServiceImpl implements IMusicInfoService { private String geneToken() { Map params = new HashMap<>(); - params.put("username", "admin"); - params.put("password", "admin"); + params.put(tokenParam1, tokenValue1); + params.put(tokenParam2, tokenValue2); TokenRes tokenRes = null; try { - tokenRes = OkHttpUtil.post("http://114.96.87.132:8002/apimt/token/", params, new TypeReference<>() { + tokenRes = OkHttpUtil.post(tokenUrl, params, new TypeReference<>() { }); } catch (Exception e) { log.error("<<<<<<<<<<获取token异常:{}>>>>>>>>>>", e.getMessage(), e);