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

Commit

Permalink
Use own, less bloated, abstraction class
Browse files Browse the repository at this point in the history
  • Loading branch information
duncte123 committed Mar 23, 2020
1 parent 3bba558 commit 2045f64
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 65 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ plugins {
}

project.group = "com.dunctebot"
project.version = "1.2.1"
project.version = "1.2.2"
val archivesBaseName = "sourcemanagers"

repositories {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2020 Duncan "duncte123" Sterken
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.dunctebot.sourcemanagers;

import com.dunctebot.sourcemanagers.pornhub.PornHubAudioSourceManager;
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManager;
import com.sedmelluq.discord.lavaplayer.tools.ExceptionTools;
import com.sedmelluq.discord.lavaplayer.tools.io.HttpClientTools;
import com.sedmelluq.discord.lavaplayer.tools.io.HttpConfigurable;
import com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface;
import com.sedmelluq.discord.lavaplayer.tools.io.HttpInterfaceManager;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClientBuilder;
import java.util.function.Consumer;
import java.util.function.Function;

public abstract class AbstractDuncteBotHttpSource implements AudioSourceManager, HttpConfigurable {

private final HttpInterfaceManager httpInterfaceManager;

public AbstractDuncteBotHttpSource() {
httpInterfaceManager = HttpClientTools.createDefaultThreadLocalManager();

httpInterfaceManager.setHttpContextFilter(new PornHubAudioSourceManager.FuckCookies());
}

public HttpInterface getHttpInterface() {
return httpInterfaceManager.getInterface();
}

@Override
public void shutdown() {
ExceptionTools.closeWithWarnings(httpInterfaceManager);
}

@Override
public void configureRequests(Function<RequestConfig, RequestConfig> configurator) {
httpInterfaceManager.configureRequests(configurator);
}

@Override
public void configureBuilder(Consumer<HttpClientBuilder> configurator) {
httpInterfaceManager.configureBuilder(configurator);
}
}
10 changes: 4 additions & 6 deletions src/main/java/com/dunctebot/sourcemanagers/Mp3Track.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package com.dunctebot.sourcemanagers;

import com.sedmelluq.discord.lavaplayer.container.mp3.Mp3AudioTrack;
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManager;
import com.sedmelluq.discord.lavaplayer.source.http.HttpAudioSourceManager;
import com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface;
import com.sedmelluq.discord.lavaplayer.tools.io.PersistentHttpStream;
import com.sedmelluq.discord.lavaplayer.tools.io.SeekableInputStream;
Expand All @@ -34,11 +32,11 @@
public class Mp3Track extends DelegatedAudioTrack {
protected static final Logger log = LoggerFactory.getLogger(Mp3Track.class);

private final HttpAudioSourceManager manager;
private final AbstractDuncteBotHttpSource manager;

public Mp3Track(AudioTrackInfo trackInfo, AudioSourceManager manager) {
public Mp3Track(AudioTrackInfo trackInfo, AbstractDuncteBotHttpSource manager) {
super(trackInfo);
this.manager = (HttpAudioSourceManager) manager;
this.manager = (AbstractDuncteBotHttpSource) manager;
}

@Override
Expand Down Expand Up @@ -66,7 +64,7 @@ protected String getPlaybackUrl() {
}

@Override
public HttpAudioSourceManager getSourceManager() {
public AbstractDuncteBotHttpSource getSourceManager() {
return manager;
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/dunctebot/sourcemanagers/MpegTrack.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
package com.dunctebot.sourcemanagers;

import com.sedmelluq.discord.lavaplayer.container.mpeg.MpegAudioTrack;
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManager;
import com.sedmelluq.discord.lavaplayer.tools.io.SeekableInputStream;
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
import com.sedmelluq.discord.lavaplayer.track.InternalAudioTrack;

public abstract class MpegTrack extends Mp3Track {
public MpegTrack(AudioTrackInfo trackInfo, AudioSourceManager manager) {
public MpegTrack(AudioTrackInfo trackInfo, AbstractDuncteBotHttpSource manager) {
super(trackInfo, manager);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package com.dunctebot.sourcemanagers.clypit;

import com.dunctebot.sourcemanagers.AbstractDuncteBotHttpSource;
import com.dunctebot.sourcemanagers.IdentifiedAudioReference;
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.source.http.HttpAudioSourceManager;
import com.sedmelluq.discord.lavaplayer.tools.ExceptionTools;
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
import com.sedmelluq.discord.lavaplayer.tools.JsonBrowser;
Expand All @@ -37,7 +37,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ClypitAudioSourceManager extends HttpAudioSourceManager {
public class ClypitAudioSourceManager extends AbstractDuncteBotHttpSource {

private static final Pattern CLYPIT_REGEX = Pattern.compile("(http://|https://(www\\.)?)?clyp\\.it/(.*)");

Expand Down Expand Up @@ -73,11 +73,6 @@ public AudioItem loadItem(DefaultAudioPlayerManager manager, AudioReference refe
}
}

@Override
public void encodeTrack(AudioTrack track, DataOutput output) {
// empty because we don't need them
}

// Switched from WebUtils to lavaplayer's stuff because that is better I guess
private JsonBrowser fetchJson(String itemId) throws IOException {
final HttpGet httpGet = new HttpGet("https://api.clyp.it/" + itemId);
Expand All @@ -99,8 +94,18 @@ private JsonBrowser fetchJson(String itemId) throws IOException {
}
}

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

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

@Override
public void encodeTrack(AudioTrack track, DataOutput output) {
// empty because we don't need them
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@

package com.dunctebot.sourcemanagers.clypit;

import com.dunctebot.sourcemanagers.AbstractDuncteBotHttpSource;
import com.dunctebot.sourcemanagers.Mp3Track;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;

public class ClypitAudioTrack extends Mp3Track {

ClypitAudioTrack(AudioTrackInfo trackInfo, ClypitAudioSourceManager manager) {
ClypitAudioTrack(AudioTrackInfo trackInfo, AbstractDuncteBotHttpSource manager) {
super(trackInfo, manager);
}

@Override
public AudioTrack makeClone() {
return new ClypitAudioTrack(trackInfo, (ClypitAudioSourceManager) getSourceManager());
protected AudioTrack makeShallowClone() {
return new ClypitAudioTrack(trackInfo, getSourceManager());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.dunctebot.sourcemanagers.getyarn;

import com.dunctebot.sourcemanagers.AbstractDuncteBotHttpSource;
import com.dunctebot.sourcemanagers.IdentifiedAudioReference;
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.source.http.HttpAudioSourceManager;
Expand All @@ -30,7 +31,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GetyarnAudioSourceManager extends HttpAudioSourceManager {
public class GetyarnAudioSourceManager extends AbstractDuncteBotHttpSource {
private static final Pattern GETYARN_REGEX = Pattern.compile("(?:http://|https://(?:www\\.)?)?getyarn\\.io/yarn-clip/(.*)");

@Override
Expand Down Expand Up @@ -60,6 +61,11 @@ public AudioItem loadItem(DefaultAudioPlayerManager manager, AudioReference refe
);
}

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

@Override
public void encodeTrack(AudioTrack track, DataOutput output) {
// Nothing to encode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@

package com.dunctebot.sourcemanagers.getyarn;

import com.dunctebot.sourcemanagers.AbstractDuncteBotHttpSource;
import com.dunctebot.sourcemanagers.MpegTrack;
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManager;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;

public class GetyarnAudioTrack extends MpegTrack {
public GetyarnAudioTrack(AudioTrackInfo trackInfo, AudioSourceManager manager) {
public GetyarnAudioTrack(AudioTrackInfo trackInfo, AbstractDuncteBotHttpSource manager) {
super(trackInfo, manager);
}

@Override
public AudioTrack makeClone() {
public AudioTrack makeShallowClone() {
return new GetyarnAudioTrack(trackInfo, getSourceManager());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,40 @@

package com.dunctebot.sourcemanagers.pornhub;

import com.dunctebot.sourcemanagers.AbstractDuncteBotHttpSource;
import com.dunctebot.sourcemanagers.AudioTrackInfoWithImage;
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.source.AudioSourceManager;
import com.sedmelluq.discord.lavaplayer.tools.ExceptionTools;
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException.Severity;
import com.sedmelluq.discord.lavaplayer.tools.JsonBrowser;
import com.sedmelluq.discord.lavaplayer.tools.http.HttpContextFilter;
import com.sedmelluq.discord.lavaplayer.tools.io.HttpClientTools;
import com.sedmelluq.discord.lavaplayer.tools.io.HttpConfigurable;
import com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface;
import com.sedmelluq.discord.lavaplayer.tools.io.HttpInterfaceManager;
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.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PornHubAudioSourceManager implements AudioSourceManager, HttpConfigurable {
public class PornHubAudioSourceManager extends AbstractDuncteBotHttpSource {
private static final String DOMAIN_PATTERN = "https?://([a-z]+\\.)?pornhub\\.(com|net)";
public static final Pattern DOMAIN_REGEX = Pattern.compile(DOMAIN_PATTERN);
private static final Pattern VIDEO_REGEX = Pattern.compile("^" + DOMAIN_PATTERN + "/view_video\\.php\\?viewkey=([a-zA-Z0-9]+)(?:.*)$");
private static final Pattern VIDEO_INFO_REGEX = Pattern.compile("var flashvars_\\d+ = (\\{.+})");
private static final Pattern MODEL_INFO_REGEX = Pattern.compile("var MODEL_PROFILE = (\\{.+})");
private final HttpInterfaceManager httpInterfaceManager;

public PornHubAudioSourceManager() {
httpInterfaceManager = HttpClientTools.createDefaultThreadLocalManager();

httpInterfaceManager.setHttpContextFilter(new FuckCookies());
}

@Override
public String getSourceName() {
Expand Down Expand Up @@ -99,25 +84,6 @@ public AudioTrack decodeTrack(AudioTrackInfo trackInfo, DataInput input) {
return new PornHubAudioTrack(trackInfo, this);
}

@Override
public void shutdown() {
ExceptionTools.closeWithWarnings(httpInterfaceManager);
}

@Override
public void configureRequests(Function<RequestConfig, RequestConfig> configurator) {
httpInterfaceManager.configureRequests(configurator);
}

@Override
public void configureBuilder(Consumer<HttpClientBuilder> configurator) {
httpInterfaceManager.configureBuilder(configurator);
}

public HttpInterface getHttpInterface() {
return httpInterfaceManager.getInterface();
}

private AudioItem loadItemOnce(AudioReference reference) throws IOException {
final String html = loadHtml(reference.identifier);

Expand Down Expand Up @@ -229,7 +195,7 @@ private void notAvailable() {
throw new FriendlyException("This video is not available", Severity.COMMON, null);
}

private static class FuckCookies implements HttpContextFilter {
public static class FuckCookies implements HttpContextFilter {
@Override
public void onContextOpen(HttpClientContext context) {
CookieStore cookieStore = context.getCookieStore();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

package com.dunctebot.sourcemanagers.pornhub;

import com.dunctebot.sourcemanagers.AbstractDuncteBotHttpSource;
import com.dunctebot.sourcemanagers.MpegTrack;
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
import com.sedmelluq.discord.lavaplayer.tools.io.HttpInterface;
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;
Expand All @@ -37,7 +39,7 @@ public class PornHubAudioTrack extends MpegTrack {
private static final Pattern MEDIA_STRING = Pattern.compile("(var\\s+?mediastring.+?)<\\/script>");
private static final Pattern MEDIA_STRING_FILTER = Pattern.compile("\\/\\* \\+ [a-zA-Z0-9_]+ \\+ \\*\\/");

public PornHubAudioTrack(AudioTrackInfo trackInfo, PornHubAudioSourceManager sourceManager) {
public PornHubAudioTrack(AudioTrackInfo trackInfo, AbstractDuncteBotHttpSource sourceManager) {
super(trackInfo, sourceManager);
}

Expand Down Expand Up @@ -93,4 +95,9 @@ private static String parseJsValueToUrl(String htmlPage, String js) {

return String.join("", videoParts);
}

@Override
protected AudioTrack makeShallowClone() {
return new PornHubAudioTrack(trackInfo, getSourceManager());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package com.dunctebot.sourcemanagers.speech;

import com.dunctebot.sourcemanagers.AbstractDuncteBotHttpSource;
import com.dunctebot.sourcemanagers.IdentifiedAudioReference;
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager;
import com.sedmelluq.discord.lavaplayer.source.http.HttpAudioSourceManager;
import com.sedmelluq.discord.lavaplayer.track.AudioItem;
import com.sedmelluq.discord.lavaplayer.track.AudioReference;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
Expand All @@ -29,7 +29,7 @@
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

public class SpeechAudioSourceManager extends HttpAudioSourceManager {
public class SpeechAudioSourceManager extends AbstractDuncteBotHttpSource {

private static final String PREFIX = "speak:";
private static final String GOOGLE_TRANSLATE_URL = "https://translate.google.com/translate_tts" +
Expand Down
Loading

0 comments on commit 2045f64

Please sign in to comment.