mirror of
https://github.com/Coloryr/AllMusic.git
synced 2026-11-18 19:39:15 +00:00
自动下载并启动外置api 面板服也许能用
This commit is contained in:
1 parent
ed7af3428b
commit
6830f616aa
8 files changed
+194
-1
No files matched your search
@@ -27,6 +27,7 @@ dependencies {
|
||||
dependencies { compile fileTree(dir:'lib',include:['*.jar'])}
|
||||
compileOnly 'me.clip:placeholderapi:2.10.6'
|
||||
|
||||
compile 'org.apache.ant:ant:1.9.4'
|
||||
compile 'org.apache.httpcomponents:httpclient:4.5.11'
|
||||
}
|
||||
tasks.withType(JavaCompile) {
|
||||
|
||||
@@ -13,6 +13,9 @@ import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
@@ -37,6 +40,7 @@ public class AllMusic {
|
||||
private static MessageOBJ Message;
|
||||
private static File ConfigFile;
|
||||
private static File MessageFile;
|
||||
private static File DataFolder;
|
||||
|
||||
public static VaultHook Vault;
|
||||
|
||||
@@ -86,6 +90,10 @@ public class AllMusic {
|
||||
NowPlayPlayer.add(player);
|
||||
}
|
||||
|
||||
public static String getDataFolder() {
|
||||
return DataFolder.getPath();
|
||||
}
|
||||
|
||||
public static void removeNowPlayPlayer(String player) {
|
||||
NowPlayPlayer.remove(player);
|
||||
}
|
||||
@@ -120,6 +128,13 @@ public class AllMusic {
|
||||
}
|
||||
|
||||
private void initAPI() {
|
||||
if (AllMusic.Config.isAutoApi()) {
|
||||
try {
|
||||
runAPI();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
switch (AllMusic.Config.getMusic_Api()) {
|
||||
case 2: {
|
||||
AllMusic.Music = new API1();
|
||||
@@ -133,6 +148,32 @@ public class AllMusic {
|
||||
}
|
||||
}
|
||||
|
||||
private void runAPI() throws IOException {
|
||||
if (!isPortUsing()) {
|
||||
if (System.getProperty("os.name").toLowerCase().contains("windows")) {
|
||||
Runtime.getRuntime().exec("cmd /c start " + System.getProperty("user.dir") + "\\" + DataFolder + "\\NeteaseCloudMusicApi\\start.cmd");
|
||||
} else {
|
||||
Runtime.getRuntime().exec("bash " + System.getProperty("user.dir") + "\\" + DataFolder + "\\NeteaseCloudMusicApi\\start.sh");
|
||||
}
|
||||
} else {
|
||||
AllMusic.log.info("Api开始失败,端口已被占用,可能是已经启动");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isPortUsing() throws UnknownHostException {
|
||||
boolean flag = false;
|
||||
InetAddress theAddress = InetAddress.getByName("127.0.0.1");
|
||||
try {
|
||||
Socket socket = new Socket(theAddress, 4000);
|
||||
flag = true;
|
||||
} catch (IOException e) {
|
||||
//如果所测试端口号没有被占用,那么会抛出异常,这里利用这个机制来判断
|
||||
//所以,这里在捕获异常后,什么也不用做
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
|
||||
private void LoadConfig() {
|
||||
try {
|
||||
InputStreamReader reader = new InputStreamReader(
|
||||
@@ -197,6 +238,7 @@ public class AllMusic {
|
||||
if (!logs.file.exists()) {
|
||||
logs.file.createNewFile();
|
||||
}
|
||||
DataFolder = file;
|
||||
LoadConfig();
|
||||
isRun = true;
|
||||
} catch (IOException e) {
|
||||
|
||||
@@ -1,14 +1,22 @@
|
||||
package Color_yr.AllMusic.Command;
|
||||
|
||||
import Color_yr.AllMusic.AllMusic;
|
||||
import Color_yr.AllMusic.Http.HttpRequest;
|
||||
import Color_yr.AllMusic.MusicAPI.SongSearch.SearchOBJ;
|
||||
import Color_yr.AllMusic.MusicAPI.SongSearch.SearchPage;
|
||||
import Color_yr.AllMusic.MusicPlay.PlayMusic;
|
||||
import Color_yr.AllMusic.MusicPlay.SendHud.Hud;
|
||||
import Color_yr.AllMusic.MusicPlay.SendHud.PosOBJ;
|
||||
import Color_yr.AllMusic.Utils.Function;
|
||||
import Color_yr.AllMusic.Utils.UnZip;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class CommandEX {
|
||||
private static boolean init = false;
|
||||
|
||||
private static void SearchMusic(Object sender, String Name, String[] args, boolean isDefault) {
|
||||
AllMusic.Side.RunTask(() -> {
|
||||
SearchPage search = AllMusic.Music.Search(args, isDefault);
|
||||
@@ -93,6 +101,22 @@ public class CommandEX {
|
||||
AllMusic.Side.SendMessage(sender, "");
|
||||
}
|
||||
|
||||
private static void initApi() {
|
||||
try {
|
||||
AllMusic.log.info("开始下载");
|
||||
File file = HttpRequest.downloadNet("http://save.s-yh-china.com/", "MusicApi.zip", AllMusic.getDataFolder());
|
||||
AllMusic.log.info("下载完成,开始解压");
|
||||
UnZip.unZip(file, AllMusic.getDataFolder() + "\\");
|
||||
AllMusic.getConfig().setMusic_Api(2);
|
||||
AllMusic.getConfig().setAutoApi(true);
|
||||
AllMusic.save();
|
||||
AllMusic.log.info("外置Api下载完成");
|
||||
AllMusic.Side.reload();
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void EX(Object sender, String Name, String[] args) {
|
||||
if (args.length == 0) {
|
||||
AllMusic.Side.SendMessage(sender, AllMusic.getMessage().getCommand().getError());
|
||||
@@ -326,6 +350,23 @@ public class CommandEX {
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (args[0].equalsIgnoreCase("initApi")) {
|
||||
if (AllMusic.getConfig().isAutoApi() || AllMusic.getConfig().getMusic_Api() == 2) {
|
||||
AllMusic.log.info("你已经在使用外置api了,不需要安装哦");
|
||||
} else {
|
||||
if (!init) {
|
||||
AllMusic.log.info("你确定要安装Api吗,确定请再次输入/music initApi");
|
||||
init = true;
|
||||
} else {
|
||||
init = false;
|
||||
try {
|
||||
new Thread(CommandEX::initApi).start();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else if (AllMusic.getConfig().isNeedPermission() && AllMusic.Side.checkPermission(Name, "AllMusic.addmusic"))
|
||||
AllMusic.Side.SendMessage(sender, AllMusic.getMessage().getCommand().getNoPer());
|
||||
else {
|
||||
|
||||
@@ -11,6 +11,7 @@ public class ConfigOBJ {
|
||||
private String Music_Url;
|
||||
|
||||
private int Music_Api;
|
||||
private boolean AutoApi;
|
||||
|
||||
private int MaxList;
|
||||
private int MinVote;
|
||||
@@ -44,6 +45,7 @@ public class ConfigOBJ {
|
||||
public ConfigOBJ() {
|
||||
Music_Url = "http://localhost:4000";
|
||||
Music_Api = 1;
|
||||
AutoApi = false;
|
||||
MaxList = 10;
|
||||
MinVote = 3;
|
||||
Delay = 0;
|
||||
@@ -184,4 +186,16 @@ public class ConfigOBJ {
|
||||
NoMusicPlayer.remove(ID);
|
||||
AllMusic.save();
|
||||
}
|
||||
|
||||
public boolean isAutoApi() {
|
||||
return AutoApi;
|
||||
}
|
||||
|
||||
public void setAutoApi(boolean autoApi) {
|
||||
AutoApi = autoApi;
|
||||
}
|
||||
|
||||
public void setMusic_Api(int music_Api) {
|
||||
Music_Api = music_Api;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package Color_yr.AllMusic.Http;
|
||||
|
||||
import Color_yr.AllMusic.AllMusic;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
|
||||
public class HttpRequest {
|
||||
|
||||
public static File downloadNet(String urlStr, String fileName, String savePath) throws MalformedURLException {
|
||||
// 下载网络文件
|
||||
int bytesum = 0;
|
||||
int byteread = 0;
|
||||
|
||||
URL url = new URL(urlStr + fileName);
|
||||
|
||||
try {
|
||||
URLConnection conn = url.openConnection();
|
||||
InputStream inStream = conn.getInputStream();
|
||||
FileOutputStream fs = new FileOutputStream(savePath + "\\" + fileName);
|
||||
|
||||
int index = 0;
|
||||
byte[] buffer = new byte[1204];
|
||||
while ((byteread = inStream.read(buffer)) != -1) {
|
||||
bytesum += byteread;
|
||||
if (index >= 5000) {
|
||||
AllMusic.log.info("已经下载了 " + bytesum + " byte");
|
||||
index = 0;
|
||||
}
|
||||
fs.write(buffer, 0, byteread);
|
||||
index++;
|
||||
}
|
||||
|
||||
inStream.close();
|
||||
fs.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return new File(savePath + "\\" + fileName);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,7 +20,7 @@ public class API2 implements IMusicAPI {
|
||||
public boolean isUpdata;
|
||||
|
||||
public API2() {
|
||||
AllMusic.log.info("§d[AllMusic]§e使用内置爬虫");
|
||||
AllMusic.log.info("§d[AllMusic]§e使用内置爬虫,你可以输入/music initApi来尝试自动搭建外置爬虫");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package Color_yr.AllMusic.Utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Enumeration;
|
||||
import org.apache.tools.zip.ZipEntry;
|
||||
import org.apache.tools.zip.ZipFile;
|
||||
|
||||
public class UnZip {
|
||||
|
||||
public static void unZip(File zipFile, String outDir) throws IOException {
|
||||
|
||||
File outFileDir = new File(outDir);
|
||||
if (!outFileDir.exists()) {
|
||||
outFileDir.mkdirs();
|
||||
}
|
||||
|
||||
ZipFile zip = new ZipFile(zipFile);
|
||||
for (Enumeration enumeration = zip.getEntries(); enumeration.hasMoreElements(); ) {
|
||||
ZipEntry entry = (ZipEntry) enumeration.nextElement();
|
||||
String zipEntryName = entry.getName();
|
||||
InputStream in = zip.getInputStream(entry);
|
||||
|
||||
if (entry.isDirectory()) {
|
||||
File fileDir = new File(outDir + zipEntryName);
|
||||
fileDir.mkdir();
|
||||
continue;
|
||||
}
|
||||
|
||||
File file = new File(outDir, zipEntryName);
|
||||
OutputStream out = new FileOutputStream(file);
|
||||
byte[] buff = new byte[1024];
|
||||
int len;
|
||||
while ((len = in.read(buff)) > 0) {
|
||||
out.write(buff, 0, len);
|
||||
}
|
||||
|
||||
in.close();
|
||||
out.close();
|
||||
//zip.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"Music_Url": "http://localhost:4000",
|
||||
"Music_Api": 1,
|
||||
"AutoApi": false,
|
||||
"MaxList": 10,
|
||||
"MinVote": 3,
|
||||
"Delay": 0,
|
||||
|
||||
Reference in new issue
Block a user