Skip to content

Commit 69d8249

Browse files
committed
Refactored YT-like stream link handlers and fixed tests
Also uses parameterized tests now
1 parent 441e4b1 commit 69d8249

File tree

4 files changed

+183
-139
lines changed

4 files changed

+183
-139
lines changed

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/invidious/linkHandler/InvidiousStreamLinkHandlerFactory.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,24 @@ public class InvidiousStreamLinkHandlerFactory extends YoutubeLikeStreamLinkHand
88
implements InvidiousLinkHandlerFactory {
99

1010
protected final String baseUrl;
11+
protected final InvidiousPlaylistLinkHandlerFactory playlistLinkHandlerFactory;
1112

1213
public InvidiousStreamLinkHandlerFactory(final InvidiousService service) {
13-
this.baseUrl = service.getInstance().getUrl();
14+
baseUrl = service.getInstance().getUrl();
15+
playlistLinkHandlerFactory =
16+
(InvidiousPlaylistLinkHandlerFactory) service.getPlaylistLHFactory();
1417
}
1518

1619
@Override
1720
public String getInvidiousBaseUrl() {
1821
return baseUrl;
1922
}
2023

24+
@Override
25+
protected boolean isPlaylistUrl(final String url) {
26+
return playlistLinkHandlerFactory.onAcceptUrl(url);
27+
}
28+
2129
@Override
2230
public String getUrl(final String id) throws ParsingException {
2331
return baseUrl + "/watch?v=" + id;

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/shared/linkHandler/YoutubeLikeStreamLinkHandlerFactory.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import org.schabi.newpipe.extractor.exceptions.FoundAdException;
99
import org.schabi.newpipe.extractor.exceptions.ParsingException;
1010
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
11-
import org.schabi.newpipe.extractor.services.youtube.youtube.linkHandler.YoutubePlaylistLinkHandlerFactory;
1211
import org.schabi.newpipe.extractor.utils.Utils;
1312

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

3332
@Nullable
3433
private static String extractId(@Nullable final String id) {
35-
if (id != null) {
36-
final Matcher m = YOUTUBE_VIDEO_ID_REGEX_PATTERN.matcher(id);
37-
return m.find() ? m.group(1) : null;
34+
if (id == null) {
35+
return null;
3836
}
39-
return null;
37+
38+
final Matcher m = YOUTUBE_VIDEO_ID_REGEX_PATTERN.matcher(id);
39+
return m.find() ? m.group(1) : null;
4040
}
4141

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

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

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

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

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

158+
protected abstract boolean isPlaylistUrl(String url);
159+
159160
@Override
160161
public boolean onAcceptUrl(final String url) throws FoundAdException {
161162
try {

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/youtube/linkHandler/YoutubeStreamLinkHandlerFactory.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,17 @@ public final class YoutubeStreamLinkHandlerFactory extends YoutubeLikeStreamLink
3131
private YoutubeStreamLinkHandlerFactory() {
3232
}
3333

34-
public static YoutubeStreamLinkHandlerFactory getInstance() {
35-
return INSTANCE;
34+
@Override
35+
protected boolean isPlaylistUrl(final String url) {
36+
return YoutubePlaylistLinkHandlerFactory.getInstance().onAcceptUrl(url);
3637
}
3738

3839
@Override
3940
public String getUrl(final String id) {
4041
return "https://www.youtube.com/watch?v=" + id;
4142
}
43+
44+
public static YoutubeStreamLinkHandlerFactory getInstance() {
45+
return INSTANCE;
46+
}
4247
}

0 commit comments

Comments
 (0)