Skip to content

Commit

Permalink
Refactored YT-like stream link handlers and fixed tests
Browse files Browse the repository at this point in the history
Also uses parameterized tests now
  • Loading branch information
litetex committed Apr 24, 2022
1 parent 441e4b1 commit 69d8249
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@ public class InvidiousStreamLinkHandlerFactory extends YoutubeLikeStreamLinkHand
implements InvidiousLinkHandlerFactory {

protected final String baseUrl;
protected final InvidiousPlaylistLinkHandlerFactory playlistLinkHandlerFactory;

public InvidiousStreamLinkHandlerFactory(final InvidiousService service) {
this.baseUrl = service.getInstance().getUrl();
baseUrl = service.getInstance().getUrl();
playlistLinkHandlerFactory =
(InvidiousPlaylistLinkHandlerFactory) service.getPlaylistLHFactory();
}

@Override
public String getInvidiousBaseUrl() {
return baseUrl;
}

@Override
protected boolean isPlaylistUrl(final String url) {
return playlistLinkHandlerFactory.onAcceptUrl(url);
}

@Override
public String getUrl(final String id) throws ParsingException {
return baseUrl + "/watch?v=" + id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.schabi.newpipe.extractor.exceptions.FoundAdException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
import org.schabi.newpipe.extractor.services.youtube.youtube.linkHandler.YoutubePlaylistLinkHandlerFactory;
import org.schabi.newpipe.extractor.utils.Utils;

import java.net.MalformedURLException;
Expand All @@ -32,11 +31,12 @@ public abstract class YoutubeLikeStreamLinkHandlerFactory extends LinkHandlerFac

@Nullable
private static String extractId(@Nullable final String id) {
if (id != null) {
final Matcher m = YOUTUBE_VIDEO_ID_REGEX_PATTERN.matcher(id);
return m.find() ? m.group(1) : null;
if (id == null) {
return null;
}
return null;

final Matcher m = YOUTUBE_VIDEO_ID_REGEX_PATTERN.matcher(id);
return m.find() ? m.group(1) : null;
}

private static String assertIsId(@Nullable final String id) throws ParsingException {
Expand Down Expand Up @@ -85,17 +85,16 @@ public String getId(final String theUrlString)
path = path.substring(1);
}

if (!Utils.isHTTP(url) || !(isSupportedYouTubeLikeHost(url))
) {
if (!Utils.isHTTP(url) || !isSupportedYouTubeLikeHost(url)) {
if ("googleads.g.doubleclick.net".equalsIgnoreCase(host)) {
throw new FoundAdException("Error found ad: " + urlString);
}

throw new ParsingException("The url is not a Youtube-URL");
}

if (YoutubePlaylistLinkHandlerFactory.getInstance().acceptUrl(urlString)) {
throw new ParsingException("Can't find handler for url: " + urlString);
if (isPlaylistUrl(urlString)) {
throw new ParsingException("This url is a playlist url: " + urlString);
}

if (isYoutubeURL(url) || isInvidiousUrl(url) || isHooktubeURL(url)) {
Expand Down Expand Up @@ -156,6 +155,8 @@ public String getId(final String theUrlString)
throw new ParsingException("Error no suitable url: " + urlString);
}

protected abstract boolean isPlaylistUrl(String url);

@Override
public boolean onAcceptUrl(final String url) throws FoundAdException {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@ public final class YoutubeStreamLinkHandlerFactory extends YoutubeLikeStreamLink
private YoutubeStreamLinkHandlerFactory() {
}

public static YoutubeStreamLinkHandlerFactory getInstance() {
return INSTANCE;
@Override
protected boolean isPlaylistUrl(final String url) {
return YoutubePlaylistLinkHandlerFactory.getInstance().onAcceptUrl(url);
}

@Override
public String getUrl(final String id) {
return "https://www.youtube.com/watch?v=" + id;
}

public static YoutubeStreamLinkHandlerFactory getInstance() {
return INSTANCE;
}
}
Loading

0 comments on commit 69d8249

Please sign in to comment.