Skip to content

Commit

Permalink
ボット起動時にPythonのバージョンを確認する処理を実装
Browse files Browse the repository at this point in the history
  • Loading branch information
kosugikun committed Aug 10, 2024
1 parent 6724084 commit 94dfe05
Showing 1 changed file with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.sedmelluq.discord.lavaplayer.source.nico;

import com.sedmelluq.discord.lavaplayer.container.MediaContainerDescriptor;
import com.sedmelluq.discord.lavaplayer.container.*;
import com.sedmelluq.discord.lavaplayer.container.wav.WavContainerProbe;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManager;
import com.sedmelluq.discord.lavaplayer.source.local.LocalSeekableInputStream;
import com.sedmelluq.discord.lavaplayer.tools.DataFormatTools;
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
import com.sedmelluq.discord.lavaplayer.tools.io.HttpClientTools;
Expand All @@ -29,10 +30,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.File;
import java.io.IOException;
import java.io.*;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
Expand All @@ -56,6 +54,10 @@ public class NicoAudioSourceManager implements AudioSourceManager, HttpConfigura
private final HttpInterfaceManager httpInterfaceManager;
private final AtomicBoolean loggedIn;

public NicoAudioSourceManager() {
this(null, null);
}

/**
* @param email Site account email
* @param password Site account password
Expand Down Expand Up @@ -83,17 +85,38 @@ private static String getWatchUrl(String videoId) {
public void updateYtDlp() {
Runtime runtime = Runtime.getRuntime();
try {
log.info("Updating yt-dlp.");
Process process = runtime.exec("python3 -m pip install -U --pre \"yt-dlp\"");
log.info("Checking if python3 is available.");
Process checkPython3 = runtime.exec("python3 --version");
int python3ExitCode = checkPython3.waitFor();

String pythonCommand = "python3";
if (python3ExitCode != 0) {
log.info("python3 not found. Checking if python is Python 3.");
Process checkPython = runtime.exec("python --version");
BufferedReader reader = new BufferedReader(new InputStreamReader(checkPython.getInputStream()));
String pythonVersion = reader.readLine();
checkPython.waitFor();

if (pythonVersion != null && pythonVersion.startsWith("Python 3")) {
log.info("Python is version 3.x.");
pythonCommand = "python";
} else {
log.error("Neither python3 nor python version 3.x found. Aborting yt-dlp update.");
return;
}
}

log.info("Updating yt-dlp using {}.", pythonCommand);
Process process = runtime.exec(pythonCommand + " -m pip install -U --pre \"yt-dlp\"");
process.waitFor();
process.destroy();
log.info("yt-dlp update completed.");
} catch (Exception e) {
log.error("Failed to update yt-dlp. Please run \"python3 -m pip install -U --pre yt-dlp\" to update.");
log.error("Failed to update yt-dlp. Please run \"python3 -m pip install -U --pre yt-dlp\" or \"python -m pip install -U --pre yt-dlp\" manually to update.");
}

}


@Override
public String getSourceName() {
return "niconico";
Expand Down Expand Up @@ -126,6 +149,7 @@ private AudioTrack loadTrack(String videoId) {
}
}


private AudioTrack extractTrackFromXml(String videoId, Document document) {
for (Element element : document.select(":root > thumb")) {

Expand Down

0 comments on commit 94dfe05

Please sign in to comment.