Skip to content

Commit 57f850b

Browse files
committed
[YouTube] Support live URLs and do minor improvements to YoutubeStreamLinkHandlerFactory
- Move license header at the top; - Use an unmodifiable set for the subpaths instead of a modifiable list; - Add missing Nonnull and Nullable annotations; - Improve exception messages.
1 parent c589a2c commit 57f850b

1 file changed

Lines changed: 34 additions & 30 deletions

File tree

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

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
/*
2+
* Created by Christian Schabesberger on 02.02.16.
3+
*
4+
* Copyright (C) Christian Schabesberger 2018 <chris.schabesberger@mailbox.org>
5+
* YoutubeStreamLinkHandlerFactory.java is part of NewPipe Extractor.
6+
*
7+
* NewPipe Extractor is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* NewPipe Extractor is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with NewPipe Extractor. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
121
package org.schabi.newpipe.extractor.services.youtube.linkHandler;
222

323
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isHooktubeURL;
@@ -15,41 +35,21 @@
1535
import java.net.URI;
1636
import java.net.URISyntaxException;
1737
import java.net.URL;
18-
import java.util.Arrays;
1938
import java.util.List;
2039
import java.util.regex.Matcher;
2140
import java.util.regex.Pattern;
2241

42+
import javax.annotation.Nonnull;
2343
import javax.annotation.Nullable;
2444

25-
/*
26-
* Created by Christian Schabesberger on 02.02.16.
27-
*
28-
* Copyright (C) Christian Schabesberger 2018 <chris.schabesberger@mailbox.org>
29-
* YoutubeStreamLinkHandlerFactory.java is part of NewPipe.
30-
*
31-
* NewPipe is free software: you can redistribute it and/or modify
32-
* it under the terms of the GNU General Public License as published by
33-
* the Free Software Foundation, either version 3 of the License, or
34-
* (at your option) any later version.
35-
*
36-
* NewPipe is distributed in the hope that it will be useful,
37-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
38-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39-
* GNU General Public License for more details.
40-
*
41-
* You should have received a copy of the GNU General Public License
42-
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
43-
*/
44-
4545
public final class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory {
4646

4747
private static final Pattern YOUTUBE_VIDEO_ID_REGEX_PATTERN
4848
= Pattern.compile("^([a-zA-Z0-9_-]{11})");
4949
private static final YoutubeStreamLinkHandlerFactory INSTANCE
5050
= new YoutubeStreamLinkHandlerFactory();
5151
private static final List<String> SUBPATHS
52-
= Arrays.asList("embed/", "shorts/", "watch/", "v/", "w/");
52+
= List.of("embed/", "live/", "shorts/", "watch/", "v/", "w/");
5353

5454
private YoutubeStreamLinkHandlerFactory() {
5555
}
@@ -67,21 +67,24 @@ private static String extractId(@Nullable final String id) {
6767
return null;
6868
}
6969

70+
@Nonnull
7071
private static String assertIsId(@Nullable final String id) throws ParsingException {
7172
final String extractedId = extractId(id);
7273
if (extractedId != null) {
7374
return extractedId;
7475
} else {
75-
throw new ParsingException("The given string is not a Youtube-Video-ID");
76+
throw new ParsingException("The given string is not a YouTube video ID");
7677
}
7778
}
7879

80+
@Nonnull
7981
@Override
8082
public String getUrl(final String id) {
8183
return "https://www.youtube.com/watch?v=" + id;
8284
}
8385

8486
@SuppressWarnings("AvoidNestedBlocks")
87+
@Nonnull
8588
@Override
8689
public String getId(final String theUrlString)
8790
throws ParsingException, IllegalArgumentException {
@@ -124,14 +127,14 @@ public String getId(final String theUrlString)
124127
if (!Utils.isHTTP(url) || !(isYoutubeURL(url) || isYoutubeServiceURL(url)
125128
|| isHooktubeURL(url) || isInvidiousURL(url) || isY2ubeURL(url))) {
126129
if (host.equalsIgnoreCase("googleads.g.doubleclick.net")) {
127-
throw new FoundAdException("Error found ad: " + urlString);
130+
throw new FoundAdException("Error: found ad: " + urlString);
128131
}
129132

130-
throw new ParsingException("The url is not a Youtube-URL");
133+
throw new ParsingException("The URL is not a YouTube URL");
131134
}
132135

133136
if (YoutubePlaylistLinkHandlerFactory.getInstance().acceptUrl(urlString)) {
134-
throw new ParsingException("Error no suitable url: " + urlString);
137+
throw new ParsingException("Error: no suitable URL: " + urlString);
135138
}
136139

137140
// Using uppercase instead of lowercase, because toLowercase replaces some unicode
@@ -154,9 +157,9 @@ public String getId(final String theUrlString)
154157

155158
final URL decodedURL;
156159
try {
157-
decodedURL = Utils.stringToURL("http://www.youtube.com" + uQueryValue);
160+
decodedURL = Utils.stringToURL("https://www.youtube.com" + uQueryValue);
158161
} catch (final MalformedURLException e) {
159-
throw new ParsingException("Error no suitable url: " + urlString);
162+
throw new ParsingException("Error: no suitable URL: " + urlString);
160163
}
161164

162165
final String viewQueryValue = Utils.getQueryValue(decodedURL, "v");
@@ -231,7 +234,7 @@ public String getId(final String theUrlString)
231234
}
232235
}
233236

234-
throw new ParsingException("Error no suitable url: " + urlString);
237+
throw new ParsingException("Error: no suitable URL: " + urlString);
235238
}
236239

237240
@Override
@@ -246,7 +249,8 @@ public boolean onAcceptUrl(final String url) throws FoundAdException {
246249
}
247250
}
248251

249-
private String getIdFromSubpathsInPath(final String path) throws ParsingException {
252+
@Nullable
253+
private String getIdFromSubpathsInPath(@Nonnull final String path) throws ParsingException {
250254
for (final String subpath : SUBPATHS) {
251255
if (path.startsWith(subpath)) {
252256
final String id = path.substring(subpath.length());

0 commit comments

Comments
 (0)