Skip to content

Commit

Permalink
Add pixeldrain 'support'
Browse files Browse the repository at this point in the history
  • Loading branch information
duncte123 committed Jun 30, 2024
1 parent a8762b1 commit 3676296
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static class Sources {
private boolean mixcloud = true;
private boolean soundgasm = true;
private boolean elgato = false;
private boolean pixeldrain = false;

public boolean isGetyarn() {
return getyarn;
Expand Down Expand Up @@ -109,5 +110,13 @@ public boolean isElgato() {
public void setElgato(boolean elgato) {
this.elgato = elgato;
}

public boolean isPixeldrain() {
return pixeldrain;
}

public void setPixeldrain(boolean pixeldrain) {
this.pixeldrain = pixeldrain;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.dunctebot.sourcemanagers.getyarn.GetyarnAudioSourceManager;
import com.dunctebot.sourcemanagers.mixcloud.MixcloudAudioSourceManager;
import com.dunctebot.sourcemanagers.ocremix.OCRemixAudioSourceManager;
import com.dunctebot.sourcemanagers.pixeldrain.PixeldrainAudioSourceManager;
import com.dunctebot.sourcemanagers.pornhub.PornHubAudioSourceManager;
import com.dunctebot.sourcemanagers.reddit.RedditAudioSourceManager;
import com.dunctebot.sourcemanagers.soundgasm.SoundGasmAudioSourceManager;
Expand Down Expand Up @@ -89,6 +90,11 @@ public AudioPlayerManager configure(@NotNull AudioPlayerManager manager) {
// manager.registerSourceManager(new StreamDeckAudioSourceManager());
}

if (this.sourcesConfig.isPixeldrain()) {
logger.info("Registering pixeldrain audio source manager");
manager.registerSourceManager(new PixeldrainAudioSourceManager());
}

return manager;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.dunctebot.sourcemanagers.pixeldrain;

import com.dunctebot.sourcemanagers.AbstractDuncteBotHttpSource;
import com.dunctebot.sourcemanagers.Mp3Track;
import com.sedmelluq.discord.lavaplayer.container.wav.WavAudioTrack;
import com.sedmelluq.discord.lavaplayer.tools.io.SeekableInputStream;
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
import com.sedmelluq.discord.lavaplayer.track.InternalAudioTrack;

public class PixelDrainAudioTrack extends Mp3Track {
public PixelDrainAudioTrack(AudioTrackInfo trackInfo, AbstractDuncteBotHttpSource manager) {
super(trackInfo, manager);
}

@Override
protected InternalAudioTrack createAudioTrack(AudioTrackInfo trackInfo, SeekableInputStream stream) {
return new WavAudioTrack(trackInfo, stream);
}

@Override
public String getPlaybackUrl() {
return String.format(PixeldrainAudioSourceManager.AUDIO_TEMPLATE, this.trackInfo.identifier);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.dunctebot.sourcemanagers.pixeldrain;

import com.dunctebot.sourcemanagers.AbstractDuncteBotHttpSource;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
import com.sedmelluq.discord.lavaplayer.tools.Units;
import com.sedmelluq.discord.lavaplayer.track.AudioItem;
import com.sedmelluq.discord.lavaplayer.track.AudioReference;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class PixeldrainAudioSourceManager extends AbstractDuncteBotHttpSource {
private static final String PIXELDRAIN_LOOKUP_BASE = "https://pixeldrain.com/u/";

/* package */ static final String AUDIO_TEMPLATE = "https://pixeldrain.com/api/file/%s";
private static final String THUMBNAIL_TEMPLATE = AUDIO_TEMPLATE + "/thumbnail";

@Override
public String getSourceName() {
return "pixeldrain";
}

@Override
public AudioItem loadItem(AudioPlayerManager manager, AudioReference reference) {
final var id = reference.getIdentifier();

if (id.startsWith(PIXELDRAIN_LOOKUP_BASE)) {
final var parts = id.split(PIXELDRAIN_LOOKUP_BASE);

if (parts.length < 2) {
return null;
}

final var identifier = parts[1];

try {
final var trackInfo = downloadInfo(identifier);

return decodeTrack(trackInfo, null);
} catch (IOException e) {
throw new FriendlyException(
"Could not download pixeldrain track info",
FriendlyException.Severity.SUSPICIOUS,
e
);
}
}

return null;
}

private AudioTrackInfo downloadInfo(String identifier) throws IOException {
final var document = loadHtml(identifier);

if (document == null) {
notAvailable();
}

// System.out.println(document.select("meta[property]"));

final var title = document.selectFirst("meta[property='og:title']").attr("content");
final var type = document.selectFirst("meta[property='og:type']").attr("content");

if (!"music.song".equals(type)) {
throw new FriendlyException("Type " + type + " is currently not supported", FriendlyException.Severity.COMMON, null);
}

System.out.println(type);
System.out.println(title);

return new AudioTrackInfo(
title,
"Unknown artist",
Units.CONTENT_LENGTH_UNKNOWN,
identifier,
false,
PIXELDRAIN_LOOKUP_BASE + identifier,
String.format(THUMBNAIL_TEMPLATE, identifier),
null
);
}

private Document loadHtml(String identifier) throws IOException {
final var httpGet = new HttpGet(PIXELDRAIN_LOOKUP_BASE + identifier);

try (final CloseableHttpResponse response = getHttpInterface().execute(httpGet)) {
final int statusCode = response.getStatusLine().getStatusCode();

if (statusCode != 200) {
if (statusCode == 404) {
return null;
}

throw new IOException("Unexpected status code for pixeldrain page response: " + statusCode);
}

final var html = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);

return Jsoup.parse(html);
}
}

@Override
public boolean isTrackEncodable(AudioTrack track) {
return false;
}

@Override
public void encodeTrack(AudioTrack track, DataOutput output) throws IOException {
// Nothing to encode
}

@Override
public AudioTrack decodeTrack(AudioTrackInfo trackInfo, DataInput input) throws IOException {
return new PixelDrainAudioTrack(trackInfo, this);
}

private void notAvailable() {
throw new FriendlyException("This item is not available", FriendlyException.Severity.COMMON, null);
}
}
7 changes: 4 additions & 3 deletions source-managers/src/test/java/LocalPlaybackTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import com.dunctebot.sourcemanagers.elgato.streamdeck.StreamDeckAudioSourceManager;
import com.dunctebot.sourcemanagers.pixeldrain.PixeldrainAudioSourceManager;
import com.sedmelluq.discord.lavaplayer.format.AudioDataFormat;
import com.sedmelluq.discord.lavaplayer.format.AudioPlayerInputStream;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
Expand All @@ -15,7 +16,7 @@

public class LocalPlaybackTest {
public static void main(String[] args) throws Exception {
final var mngr = new StreamDeckAudioSourceManager();
final var mngr = new PixeldrainAudioSourceManager();

AudioPlayerManager manager = new DefaultAudioPlayerManager();

Expand All @@ -25,10 +26,10 @@ public static void main(String[] args) throws Exception {

AudioPlayer player = manager.createPlayer();

player.setVolume(35);
player.setVolume(100);

manager.loadItem(
"https://cdn.discordapp.com/attachments/340834322674089986/1242398908815118376/Fanfare_-_Show_Intro.streamDeckAudio?ex=664f0326&is=664db1a6&hm=9a4898f7301601b3bc14cfda4101aab0ed94cdfb5fe89d2a0917dc4e01514da6&",
"https://pixeldrain.com/u/WUJkkH3F",
new FunctionalResultHandler(item -> {
player.playTrack(item);
}, playlist -> {
Expand Down
13 changes: 13 additions & 0 deletions source-managers/src/test/java/PixeldrainTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import com.dunctebot.sourcemanagers.pixeldrain.PixeldrainAudioSourceManager;
import com.sedmelluq.discord.lavaplayer.track.AudioReference;

public class PixeldrainTest {

public static void main(String[] args) {
final var id1 = "https://pixeldrain.com/u/WUJkkH3F";
final var mngr = new PixeldrainAudioSourceManager();
final var res1 = mngr.loadItem(null, new AudioReference(id1, ""));

System.out.println(res1);
}
}

0 comments on commit 3676296

Please sign in to comment.