Skip to content
This repository was archived by the owner on Nov 26, 2023. It is now read-only.

Commit 5dc346d

Browse files
committed
NEEEDS TESTING: custom yarn impl
1 parent d4d976c commit 5dc346d

File tree

6 files changed

+175
-34
lines changed

6 files changed

+175
-34
lines changed

src/main/java/com/dunctebot/sourcemanagers/DuncteBotSources.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.dunctebot.sourcemanagers.clypit.ClypitAudioSourceManager;
2020
import com.dunctebot.sourcemanagers.extra.YoutubeContextFilterOverride;
21+
import com.dunctebot.sourcemanagers.getyarn.GetyarnAudioSourceManager;
2122
import com.dunctebot.sourcemanagers.pornhub.PornHubAudioSourceManager;
2223
import com.dunctebot.sourcemanagers.speech.SpeechAudioSourceManager;
2324
import com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager;
@@ -34,6 +35,7 @@ public static void registerCustom(AudioPlayerManager playerManager, String speec
3435
YoutubeContextFilterOverride.getOrCreate(updateYoutubeData, youtubeSource.getHttpInterface())
3536
);
3637

38+
playerManager.registerSourceManager(new GetyarnAudioSourceManager());
3739
playerManager.registerSourceManager(new ClypitAudioSourceManager());
3840
playerManager.registerSourceManager(new SpeechAudioSourceManager(speechLanguage));
3941
playerManager.registerSourceManager(new PornHubAudioSourceManager());

src/main/java/com/dunctebot/sourcemanagers/Mp3Track.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
import java.net.URI;
3131

3232
public class Mp3Track extends DelegatedAudioTrack {
33-
34-
private static final Logger log = LoggerFactory.getLogger(Mp3Track.class);
33+
protected static final Logger log = LoggerFactory.getLogger(Mp3Track.class);
3534

3635
private final HttpAudioSourceManager manager;
3736

@@ -47,16 +46,22 @@ public void process(LocalAudioTrackExecutor executor) throws Exception {
4746
}
4847
}
4948

50-
private void loadStream(LocalAudioTrackExecutor localExecutor, HttpInterface httpInterface) throws Exception {
51-
final String trackUrl = trackInfo.identifier;
49+
protected void loadStream(LocalAudioTrackExecutor localExecutor, HttpInterface httpInterface) throws Exception {
50+
final String trackUrl = getPlaybackUrl();
5251
log.debug("Starting {} track from URL: {}", manager.getSourceName(), trackUrl);
52+
// Setting contentLength (last param) to null makes it default to Long.MAX_VALUE
5353
try (PersistentHttpStream stream = new PersistentHttpStream(httpInterface, new URI(trackUrl), null)) {
54+
// TODO make a method to only return the class that we need
5455
processDelegate(new Mp3AudioTrack(trackInfo, stream), localExecutor);
5556
}
5657
}
5758

59+
protected String getPlaybackUrl() {
60+
return this.trackInfo.identifier;
61+
}
62+
5863
@Override
59-
public AudioSourceManager getSourceManager() {
64+
public HttpAudioSourceManager getSourceManager() {
6065
return manager;
6166
}
6267
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2020 Duncan "duncte123" Sterken
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.dunctebot.sourcemanagers;
18+
19+
import com.sedmelluq.discord.lavaplayer.container.mpeg.MpegAudioTrack;
20+
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManager;
21+
import com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface;
22+
import com.sedmelluq.discord.lavaplayer.tools.io.PersistentHttpStream;
23+
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
24+
import com.sedmelluq.discord.lavaplayer.track.playback.LocalAudioTrackExecutor;
25+
26+
import java.net.URI;
27+
28+
public abstract class MpegTrack extends Mp3Track {
29+
public MpegTrack(AudioTrackInfo trackInfo, AudioSourceManager manager) {
30+
super(trackInfo, manager);
31+
}
32+
33+
@Override
34+
protected void loadStream(LocalAudioTrackExecutor localExecutor, HttpInterface httpInterface) throws Exception {
35+
final String trackUrl = getPlaybackUrl();
36+
log.debug("Starting {} track from URL: {}", getSourceManager().getSourceName(), trackUrl);
37+
// Setting contentLength (last param) to null makes it default to Long.MAX_VALUE
38+
try (PersistentHttpStream stream = new PersistentHttpStream(httpInterface, new URI(trackUrl), null)) {
39+
processDelegate(new MpegAudioTrack(trackInfo, stream), localExecutor);
40+
}
41+
}
42+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2020 Duncan "duncte123" Sterken
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.dunctebot.sourcemanagers.getyarn;
18+
19+
import com.dunctebot.sourcemanagers.IdentifiedAudioReference;
20+
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager;
21+
import com.sedmelluq.discord.lavaplayer.source.http.HttpAudioSourceManager;
22+
import com.sedmelluq.discord.lavaplayer.track.AudioItem;
23+
import com.sedmelluq.discord.lavaplayer.track.AudioReference;
24+
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
25+
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
26+
import com.sedmelluq.discord.lavaplayer.track.info.AudioTrackInfoBuilder;
27+
28+
import java.io.DataInput;
29+
import java.io.DataOutput;
30+
import java.util.regex.Matcher;
31+
import java.util.regex.Pattern;
32+
33+
public class GetyarnAudioSourceManager extends HttpAudioSourceManager {
34+
private static final Pattern GETYARN_REGEX = Pattern.compile("(?:http://|https://(?:www\\.)?)?getyarn\\.io/yarn-clip/(.*)");
35+
36+
@Override
37+
public String getSourceName() {
38+
return "getyarn.io";
39+
}
40+
41+
@Override
42+
public AudioItem loadItem(DefaultAudioPlayerManager manager, AudioReference reference) {
43+
final Matcher m = GETYARN_REGEX.matcher(reference.identifier);
44+
45+
if (!m.matches()) {
46+
return null;
47+
}
48+
49+
final String videoId = m.group(m.groupCount());
50+
51+
final IdentifiedAudioReference ref = new IdentifiedAudioReference(
52+
getMP4VideoUrl(videoId),
53+
reference.identifier,
54+
videoId
55+
);
56+
57+
return new GetyarnAudioTrack(
58+
AudioTrackInfoBuilder.create(ref, null).build(),
59+
this
60+
);
61+
}
62+
63+
@Override
64+
public void encodeTrack(AudioTrack track, DataOutput output) {
65+
// Nothing to encode
66+
}
67+
68+
@Override
69+
public AudioTrack decodeTrack(AudioTrackInfo trackInfo, DataInput input) {
70+
return new GetyarnAudioTrack(trackInfo, this);
71+
}
72+
73+
private String getMP4VideoUrl(String videoId) {
74+
return "https://y.yarn.co/" + videoId + ".mp4?v=0";
75+
}
76+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2020 Duncan "duncte123" Sterken
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.dunctebot.sourcemanagers.getyarn;
18+
19+
import com.dunctebot.sourcemanagers.MpegTrack;
20+
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManager;
21+
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
22+
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
23+
24+
public class GetyarnAudioTrack extends MpegTrack {
25+
public GetyarnAudioTrack(AudioTrackInfo trackInfo, AudioSourceManager manager) {
26+
super(trackInfo, manager);
27+
}
28+
29+
@Override
30+
public AudioTrack makeClone() {
31+
return new GetyarnAudioTrack(trackInfo, getSourceManager());
32+
}
33+
}

src/main/java/com/dunctebot/sourcemanagers/pornhub/PornHubAudioTrack.java

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,37 @@
1616

1717
package com.dunctebot.sourcemanagers.pornhub;
1818

19-
import com.sedmelluq.discord.lavaplayer.container.mpeg.MpegAudioTrack;
19+
import com.dunctebot.sourcemanagers.MpegTrack;
2020
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
2121
import com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface;
22-
import com.sedmelluq.discord.lavaplayer.tools.io.PersistentHttpStream;
2322
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
24-
import com.sedmelluq.discord.lavaplayer.track.DelegatedAudioTrack;
25-
import com.sedmelluq.discord.lavaplayer.track.playback.LocalAudioTrackExecutor;
2623
import org.apache.commons.io.IOUtils;
2724
import org.apache.http.client.methods.CloseableHttpResponse;
2825
import org.apache.http.client.methods.HttpGet;
2926

3027
import java.io.IOException;
31-
import java.net.URI;
3228
import java.nio.charset.StandardCharsets;
3329
import java.util.ArrayList;
3430
import java.util.List;
3531
import java.util.regex.Matcher;
3632
import java.util.regex.Pattern;
3733

38-
public class PornHubAudioTrack extends DelegatedAudioTrack {
34+
import static com.sedmelluq.discord.lavaplayer.tools.FriendlyException.Severity.SUSPICIOUS;
35+
36+
public class PornHubAudioTrack extends MpegTrack {
3937
private static final Pattern MEDIA_STRING = Pattern.compile("(var\\s+?mediastring.+?)<\\/script>");
4038
private static final Pattern MEDIA_STRING_FILTER = Pattern.compile("\\/\\* \\+ [a-zA-Z0-9_]+ \\+ \\*\\/");
4139

42-
private final PornHubAudioSourceManager sourceManager;
43-
4440
public PornHubAudioTrack(AudioTrackInfo trackInfo, PornHubAudioSourceManager sourceManager) {
45-
super(trackInfo);
46-
this.sourceManager = sourceManager;
41+
super(trackInfo, sourceManager);
4742
}
4843

4944
@Override
50-
public void process(LocalAudioTrackExecutor executor) throws Exception {
51-
if (!this.trackInfo.identifier.equals(this.trackInfo.uri)) {
52-
return;
53-
}
54-
55-
final String playbackUrl = loadTrackUrl(this.trackInfo, this.sourceManager.getHttpInterface());
56-
57-
try (final PersistentHttpStream stream = new PersistentHttpStream(this.sourceManager.getHttpInterface(), new URI(playbackUrl), Long.MAX_VALUE)) {
58-
processDelegate(
59-
new MpegAudioTrack(this.trackInfo, stream),
60-
executor
61-
);
45+
protected String getPlaybackUrl() {
46+
try {
47+
return loadTrackUrl(this.trackInfo, this.getSourceManager().getHttpInterface());
48+
} catch (IOException e) {
49+
throw new FriendlyException("Could not load PornHub video", SUSPICIOUS, e);
6250
}
6351
}
6452

@@ -72,7 +60,7 @@ private static String loadTrackUrl(AudioTrackInfo trackInfo, HttpInterface httpI
7260
final Matcher matcher = MEDIA_STRING.matcher(html);
7361

7462
if (!matcher.find()) {
75-
throw new FriendlyException("Could not find media info", FriendlyException.Severity.SUSPICIOUS, null);
63+
throw new FriendlyException("Could not find media info", SUSPICIOUS, null);
7664
}
7765

7866
final String js = matcher.group(matcher.groupCount());
@@ -95,7 +83,7 @@ private static String parseJsValueToUrl(String htmlPage, String js) {
9583

9684
if (!matcher.find()) {
9785
System.out.println(htmlPage);
98-
throw new FriendlyException("URL part " + item + " missing", FriendlyException.Severity.SUSPICIOUS, null);
86+
throw new FriendlyException("URL part " + item + " missing", SUSPICIOUS, null);
9987
}
10088

10189
videoParts.add(
@@ -105,9 +93,4 @@ private static String parseJsValueToUrl(String htmlPage, String js) {
10593

10694
return String.join("", videoParts);
10795
}
108-
109-
@Override
110-
public PornHubAudioSourceManager getSourceManager() {
111-
return sourceManager;
112-
}
11396
}

0 commit comments

Comments
 (0)