Skip to content

Commit

Permalink
Merge 87f48c7 into 4bfbba5
Browse files Browse the repository at this point in the history
  • Loading branch information
BingChunMoLi authored Oct 14, 2024
2 parents 4bfbba5 + 87f48c7 commit 1c5dd09
Show file tree
Hide file tree
Showing 29 changed files with 293 additions and 168 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ docker-compose up -d
16. 网易云音乐歌单收藏
17. b站收藏获取,通知失效视频

## 后台管理功能
1. 签到
2. 签到日志查询
3. 随机music
4. 歌单歌曲

## 启用所有功能
1. 添加配置文件(~/.api/目录下) application.yml
2. 下载application-template.yml 并修改文件名为application-default.yml
Expand Down
72 changes: 5 additions & 67 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.3</version>
<version>3.3.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.bingchunmoli</groupId>
<artifactId>moliapi</artifactId>
<version>3.15.8</version>
<version>3.15.9</version>
<name>MoLiApi</name>
<description>MoLiApi</description>

<properties>
<java.version>17</java.version>
<mybatis-plus.version>3.5.7</mybatis-plus.version>
<mybatis-plus.version>3.5.8</mybatis-plus.version>
<docker-plugin.version>1.4.13</docker-plugin.version>
<hutool.version>5.8.32</hutool.version>
<zxing.version>3.5.3</zxing.version>
<emoji.version>5.1.1</emoji.version>
<h2.version>2.3.232</h2.version>
<ip-info.version>2.2.0</ip-info.version>
<knife4j.version>4.5.0</knife4j.version>
<tencent-cloud-api.version>3.1.1069</tencent-cloud-api.version>
<tencent-cloud-api.version>3.1.1114</tencent-cloud-api.version>
<kotlin.version>2.0.20</kotlin.version>
<firebase-admin.version>9.3.0</firebase-admin.version>
<loki-logback.version>1.5.1</loki-logback.version>
<loki-logback.version>1.5.2</loki-logback.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -120,24 +120,6 @@
<artifactId>tencentcloud-sdk-java-cdn</artifactId>
<version>${tencent-cloud-api.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-indexer</artifactId>
Expand Down Expand Up @@ -217,50 +199,6 @@
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
<args>-Xjsr305=strict</args>
<compilerPlugins>
<plugin>lombok</plugin>
<plugin>spring</plugin>
</compilerPlugins>
<pluginOptions>
<option>lombok:config=${project.basedir}/lombok.config</option>
</pluginOptions>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-lombok</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.util.Locale;
Expand All @@ -20,6 +21,7 @@
*/
@Slf4j
@Component
@Profile("prod")
@RequiredArgsConstructor
public class DownloadCommandLineRunner implements CommandLineRunner {
//备用
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Collection;
Expand All @@ -25,6 +26,7 @@ public MessageEvenListener(Map<String, Push> map, PushLogService pushLogService)
this.pushLogService = pushLogService;
}

@Async
@Override
public void onApplicationEvent(@NotNull MessageEven event) {
Message message = event.getMessage();
Expand Down
29 changes: 27 additions & 2 deletions src/main/java/com/bingchunmoli/api/netease/MusicController.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package com.bingchunmoli.api.netease;

import com.bingchunmoli.api.bean.ResultVO;
import com.bingchunmoli.api.netease.bean.NeteaseMusicSongVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URI;
import java.util.List;

@RestController
@Tag(name = "音乐")
@RequestMapping("music")
Expand All @@ -22,8 +29,26 @@ public PlayListBO getPlayListInfo(String id, String cookie){
return musicService.getPlayListInfo(id, cookie);
}

/**
* 获取歌单中歌曲列表
* @param id 歌单id
* @return 该格党的music
*/
@GetMapping("{id}")
public ResultVO<String> getMusicLink(@PathVariable String id){
return ResultVO.ok("https://music.163.com/api/playlist/detail?id=" + id + "&offset=0&total=true&limit=1001");
public ResultVO<List<NeteaseMusicSongVO>> getMusicLink(@PathVariable String id){
return ResultVO.ok(musicService.getMusicSongList(id));
}

/**
* 获取随机歌曲
* @return
*/
@GetMapping("random")
public ResponseEntity<ResultVO<String>> getRandomMusicLink(){
String url = "https://music.163.com/song/media/outer/url?id=" + musicService.getRandomMusicId() + ".mp3";
return ResponseEntity.status(HttpStatus.FOUND)
.cacheControl(CacheControl.noStore())
.location(URI.create(url))
.body(ResultVO.ok(url));
}
}
43 changes: 31 additions & 12 deletions src/main/java/com/bingchunmoli/api/netease/MusicService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bingchunmoli.api.config.ApiConfig;
import com.bingchunmoli.api.netease.bean.NeteaseMusicAlbum;
import com.bingchunmoli.api.netease.bean.NeteaseMusicPlaylist;
import com.bingchunmoli.api.netease.bean.NeteaseMusicSong;
import com.bingchunmoli.api.netease.bean.NeteaseMusicUser;
import com.bingchunmoli.api.netease.bean.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
Expand Down Expand Up @@ -48,18 +45,27 @@ public class MusicService {
* @param cookie cookie
* @return 歌单实体
*/
public PlayListBO getPlayListInfo(String id, String cookie){
Collection<BasicHeader> defaultHeader = List.of(new BasicHeader("cookie", cookie));
public PlayListBO getPlayListInfo(@Valid @NotBlank String id, String cookie){
Collection<BasicHeader> defaultHeader = List.of(new BasicHeader("cookie", cookie),
new BasicHeader("referer", "https://music.163.com/"),
new BasicHeader("accept", "*/*"),
new BasicHeader("accept-language", "zh,zh-US;q=0.9,zh-SG;q=0.8,en-SG;q=0.7,en;q=0.6,zh-CN;q=0.5"),
new BasicHeader("pragma", "no-cache"),
new BasicHeader("sec-fetch-dest", "script"),
new BasicHeader("sec-fetch-mode", "no-cors"),
new BasicHeader("sec-fetch-site", "same-site"),
new BasicHeader("sec-ch-ua", "\n" +
"\"Google Chrome\";v=\"129\", \"Not=A?Brand\";v=\"8\", \"Chromium\";v=\"129\""),
new BasicHeader("sec-ch-ua-platform", "\"windows\""),
new BasicHeader("origin", "https://music.163.com/"),
new BasicHeader("sec-ch-ua-mobile", "?0"));
try (CloseableHttpClient httpClient = HttpClientBuilder.create()
.setDefaultHeaders(defaultHeader)
.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
.setDefaultRequestConfig(RequestConfig.custom()
.setCookieSpec(CookieSpecs.STANDARD).build())
.setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36")
.build()){
HttpRequest request = new HttpGet( "/api/playlist/detail?id=" + id);
return httpClient.execute(baseHost, request, res -> {
String string = EntityUtils.toString(res.getEntity());
EntityUtils.consume(res.getEntity());
return om.readValue(string, PlayListBO.class);
});
} catch (Exception e) {
Expand Down Expand Up @@ -113,11 +119,15 @@ public void savePlayList() {
.name(album.getName())
.picUrl(album.getPicUrl())
.type(album.getType())
.userId(album.getArtist().getId())
.publishTime(LocalDateTime.ofInstant(Instant.ofEpochMilli(album.getPublishTime()), ZoneId.systemDefault())).build();
NeteaseMusicAlbum dbAlbum = albumService.getOne(new LambdaQueryWrapper<NeteaseMusicAlbum>()
.eq(NeteaseMusicAlbum::getThirdId, musicAlbum.getThirdId()));
if (dbAlbum == null) {
albumService.save(musicAlbum);
}else {
musicAlbum.setId(dbAlbum.getId());
albumService.updateById(musicAlbum);
}
List<PlayListBO.ResultDTO.TracksDTO.ArtistsDTO> artists = track.getArtists();
List<NeteaseMusicUser> musicUserList = new ArrayList<>(artists.size());
Expand All @@ -134,6 +144,7 @@ public void savePlayList() {
.thirdId(Long.valueOf(track.getId()))
.albumId(musicAlbum.getId())
.artists(musicUserList)
.playlistId(musicPlaylist.getId())
.build();
songs.add(song);
}
Expand All @@ -144,4 +155,12 @@ public void savePlayList() {
throw new RuntimeException(e);
}
}

public String getRandomMusicId() {
return String.valueOf(songService.getRandomSong().getThirdId());
}

public List<NeteaseMusicSongVO> getMusicSongList(String id) {
return songService.getMusicSongList(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.baomidou.mybatisplus.extension.service.IService;
import com.bingchunmoli.api.netease.bean.NeteaseMusicSong;
import com.bingchunmoli.api.netease.bean.NeteaseMusicSongVO;

import java.util.List;

Expand All @@ -11,4 +12,17 @@
public interface NeteaseMusicSongService extends IService<NeteaseMusicSong> {

void saveBatchAndChild(List<NeteaseMusicSong> songs);

/**
* 获取随机歌曲
* @return 一首歌曲
*/
NeteaseMusicSong getRandomSong();

/**
* 获取歌曲和专辑图片
* @param id 歌单id
* @return
*/
List<NeteaseMusicSongVO> getMusicSongList(String id);
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.bingchunmoli.api.netease;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bingchunmoli.api.netease.bean.NeteaseMusicSong;
import com.bingchunmoli.api.netease.bean.NeteaseMusicSongVO;
import com.bingchunmoli.api.netease.mapper.NeteaseMusicSongMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author MoLi
Expand All @@ -21,7 +22,6 @@ public class NeteaseMusicSongServiceImpl extends ServiceImpl<NeteaseMusicSongMap
private final NeteaseMusicUserService neteaseMusicUserService;

@Override
@Transactional
public void saveBatchAndChild(List<NeteaseMusicSong> songs) {
if (songs == null || songs.isEmpty()) {
return;
Expand All @@ -35,23 +35,26 @@ public void saveBatchAndChild(List<NeteaseMusicSong> songs) {
getBaseMapper().saveSongUser(song.getId(), userIds);
}
}else {
List<NeteaseMusicSong> list = songs.stream().filter(v -> {
for (NeteaseMusicSong song : dbSongs) {
if (Objects.equals(song.getId(), v.getId())) {
return false;
}
}
return true;
}).toList();
for (NeteaseMusicSong song : list) {
((NeteaseMusicSongService) AopContext.currentProxy()).updateBatchById(songs.stream().filter(v -> v.getId() != null).collect(Collectors.toList()));
((NeteaseMusicSongService) AopContext.currentProxy()).saveBatch(songs.stream().filter(v -> v.getId() == null).collect(Collectors.toList()));
for (NeteaseMusicSong song : songs) {
List<Integer> userIds = neteaseMusicUserService.getIdBatch(song.getArtists());
getBaseMapper().saveSongUser(song.getId(), userIds);
List<Integer> alreadyExistsUserIds = getBaseMapper().getSongUser(song.getId());
List<Integer> saveUserIds = alreadyExistsUserIds.stream().filter(v -> !userIds.contains(v)).toList();
if (CollUtil.isNotEmpty(saveUserIds)) {
getBaseMapper().saveSongUser(song.getId(), saveUserIds);
}
}
}

}
}



@Override
public NeteaseMusicSong getRandomSong() {
return getBaseMapper().selectRandomSong();
}

@Override
public List<NeteaseMusicSongVO> getMusicSongList(String id) {
return getBaseMapper().getMusicSongList(id);
}
}
Loading

0 comments on commit 1c5dd09

Please sign in to comment.