-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
187 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...-managers/src/main/java/com/dunctebot/sourcemanagers/pixeldrain/PixelDrainAudioTrack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
131 changes: 131 additions & 0 deletions
131
...s/src/main/java/com/dunctebot/sourcemanagers/pixeldrain/PixeldrainAudioSourceManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |