|
| 1 | +package com.dunctebot.sourcemanagers.pixeldrain; |
| 2 | + |
| 3 | +import com.dunctebot.sourcemanagers.AbstractDuncteBotHttpSource; |
| 4 | +import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager; |
| 5 | +import com.sedmelluq.discord.lavaplayer.tools.FriendlyException; |
| 6 | +import com.sedmelluq.discord.lavaplayer.tools.Units; |
| 7 | +import com.sedmelluq.discord.lavaplayer.track.AudioItem; |
| 8 | +import com.sedmelluq.discord.lavaplayer.track.AudioReference; |
| 9 | +import com.sedmelluq.discord.lavaplayer.track.AudioTrack; |
| 10 | +import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo; |
| 11 | +import org.apache.commons.io.IOUtils; |
| 12 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 13 | +import org.apache.http.client.methods.HttpGet; |
| 14 | +import org.jsoup.Jsoup; |
| 15 | +import org.jsoup.nodes.Document; |
| 16 | + |
| 17 | +import java.io.DataInput; |
| 18 | +import java.io.DataOutput; |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | + |
| 22 | +public class PixeldrainAudioSourceManager extends AbstractDuncteBotHttpSource { |
| 23 | + private static final String PIXELDRAIN_LOOKUP_BASE = "https://pixeldrain.com/u/"; |
| 24 | + |
| 25 | + /* package */ static final String AUDIO_TEMPLATE = "https://pixeldrain.com/api/file/%s"; |
| 26 | + private static final String THUMBNAIL_TEMPLATE = AUDIO_TEMPLATE + "/thumbnail"; |
| 27 | + |
| 28 | + @Override |
| 29 | + public String getSourceName() { |
| 30 | + return "pixeldrain"; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public AudioItem loadItem(AudioPlayerManager manager, AudioReference reference) { |
| 35 | + final var id = reference.getIdentifier(); |
| 36 | + |
| 37 | + if (id.startsWith(PIXELDRAIN_LOOKUP_BASE)) { |
| 38 | + final var parts = id.split(PIXELDRAIN_LOOKUP_BASE); |
| 39 | + |
| 40 | + if (parts.length < 2) { |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + final var identifier = parts[1]; |
| 45 | + |
| 46 | + try { |
| 47 | + final var trackInfo = downloadInfo(identifier); |
| 48 | + |
| 49 | + return decodeTrack(trackInfo, null); |
| 50 | + } catch (IOException e) { |
| 51 | + throw new FriendlyException( |
| 52 | + "Could not download pixeldrain track info", |
| 53 | + FriendlyException.Severity.SUSPICIOUS, |
| 54 | + e |
| 55 | + ); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + private AudioTrackInfo downloadInfo(String identifier) throws IOException { |
| 63 | + final var document = loadHtml(identifier); |
| 64 | + |
| 65 | + if (document == null) { |
| 66 | + notAvailable(); |
| 67 | + } |
| 68 | + |
| 69 | +// System.out.println(document.select("meta[property]")); |
| 70 | + |
| 71 | + final var title = document.selectFirst("meta[property='og:title']").attr("content"); |
| 72 | + final var type = document.selectFirst("meta[property='og:type']").attr("content"); |
| 73 | + |
| 74 | + if (!"music.song".equals(type)) { |
| 75 | + throw new FriendlyException("Type " + type + " is currently not supported", FriendlyException.Severity.COMMON, null); |
| 76 | + } |
| 77 | + |
| 78 | + System.out.println(type); |
| 79 | + System.out.println(title); |
| 80 | + |
| 81 | + return new AudioTrackInfo( |
| 82 | + title, |
| 83 | + "Unknown artist", |
| 84 | + Units.CONTENT_LENGTH_UNKNOWN, |
| 85 | + identifier, |
| 86 | + false, |
| 87 | + PIXELDRAIN_LOOKUP_BASE + identifier, |
| 88 | + String.format(THUMBNAIL_TEMPLATE, identifier), |
| 89 | + null |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + private Document loadHtml(String identifier) throws IOException { |
| 94 | + final var httpGet = new HttpGet(PIXELDRAIN_LOOKUP_BASE + identifier); |
| 95 | + |
| 96 | + try (final CloseableHttpResponse response = getHttpInterface().execute(httpGet)) { |
| 97 | + final int statusCode = response.getStatusLine().getStatusCode(); |
| 98 | + |
| 99 | + if (statusCode != 200) { |
| 100 | + if (statusCode == 404) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + throw new IOException("Unexpected status code for pixeldrain page response: " + statusCode); |
| 105 | + } |
| 106 | + |
| 107 | + final var html = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8); |
| 108 | + |
| 109 | + return Jsoup.parse(html); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public boolean isTrackEncodable(AudioTrack track) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void encodeTrack(AudioTrack track, DataOutput output) throws IOException { |
| 120 | + // Nothing to encode |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public AudioTrack decodeTrack(AudioTrackInfo trackInfo, DataInput input) throws IOException { |
| 125 | + return new PixelDrainAudioTrack(trackInfo, this); |
| 126 | + } |
| 127 | + |
| 128 | + private void notAvailable() { |
| 129 | + throw new FriendlyException("This item is not available", FriendlyException.Severity.COMMON, null); |
| 130 | + } |
| 131 | +} |
0 commit comments