2018-07-13 18:02:40 +02:00
|
|
|
package org.schabi.newpipe.extractor.services.youtube.linkHandler;
|
2017-03-01 18:47:52 +01:00
|
|
|
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.FoundAdException;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
2019-01-13 12:52:07 +01:00
|
|
|
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
|
2020-04-10 10:51:05 +02:00
|
|
|
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
|
2019-01-13 12:52:07 +01:00
|
|
|
import org.schabi.newpipe.extractor.utils.Utils;
|
2017-03-01 18:47:52 +01:00
|
|
|
|
2020-07-02 21:31:05 +02:00
|
|
|
import javax.annotation.Nullable;
|
2019-01-13 12:52:07 +01:00
|
|
|
import java.net.MalformedURLException;
|
2017-03-01 18:47:52 +01:00
|
|
|
import java.net.URI;
|
|
|
|
import java.net.URISyntaxException;
|
2019-01-13 12:52:07 +01:00
|
|
|
import java.net.URL;
|
2021-02-10 17:06:51 +01:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
2020-09-03 02:29:18 +02:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
2017-03-01 18:47:52 +01:00
|
|
|
|
2017-06-29 20:12:55 +02:00
|
|
|
/*
|
2017-03-01 18:47:52 +01:00
|
|
|
* Created by Christian Schabesberger on 02.02.16.
|
|
|
|
*
|
2018-07-01 16:21:40 +02:00
|
|
|
* Copyright (C) Christian Schabesberger 2018 <chris.schabesberger@mailbox.org>
|
2018-07-13 18:02:40 +02:00
|
|
|
* YoutubeStreamLinkHandlerFactory.java is part of NewPipe.
|
2017-03-01 18:47:52 +01:00
|
|
|
*
|
|
|
|
* NewPipe is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* NewPipe is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-07-13 18:02:40 +02:00
|
|
|
public class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory {
|
2017-03-01 18:47:52 +01:00
|
|
|
|
2021-02-05 18:57:58 +01:00
|
|
|
private static final Pattern YOUTUBE_VIDEO_ID_REGEX_PATTERN = Pattern.compile("^([a-zA-Z0-9_-]{11})");
|
2018-07-13 18:02:40 +02:00
|
|
|
private static final YoutubeStreamLinkHandlerFactory instance = new YoutubeStreamLinkHandlerFactory();
|
2021-02-16 19:07:24 +01:00
|
|
|
private static final List<String> SUBPATHS = Arrays.asList("embed/", "shorts/", "watch/", "v/", "w/");
|
2017-03-01 18:47:52 +01:00
|
|
|
|
2018-07-13 18:02:40 +02:00
|
|
|
private YoutubeStreamLinkHandlerFactory() {
|
2017-06-29 20:12:55 +02:00
|
|
|
}
|
2017-03-01 18:47:52 +01:00
|
|
|
|
2018-07-13 18:02:40 +02:00
|
|
|
public static YoutubeStreamLinkHandlerFactory getInstance() {
|
2017-03-01 18:47:52 +01:00
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2020-09-03 02:29:18 +02:00
|
|
|
@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;
|
|
|
|
}
|
|
|
|
return null;
|
2020-05-06 15:41:01 +02:00
|
|
|
}
|
|
|
|
|
2020-09-03 02:29:18 +02:00
|
|
|
private static String assertIsId(@Nullable final String id) throws ParsingException {
|
|
|
|
final String extractedId = extractId(id);
|
|
|
|
if (extractedId != null) {
|
|
|
|
return extractedId;
|
2020-05-06 15:41:01 +02:00
|
|
|
} else {
|
2019-01-13 12:52:07 +01:00
|
|
|
throw new ParsingException("The given string is not a Youtube-Video-ID");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 18:47:52 +01:00
|
|
|
@Override
|
2018-07-01 16:21:40 +02:00
|
|
|
public String getUrl(String id) {
|
2017-08-07 18:12:51 +02:00
|
|
|
return "https://www.youtube.com/watch?v=" + id;
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-01-13 12:52:07 +01:00
|
|
|
public String getId(String urlString) throws ParsingException, IllegalArgumentException {
|
|
|
|
try {
|
|
|
|
URI uri = new URI(urlString);
|
2019-01-20 01:31:30 +01:00
|
|
|
String scheme = uri.getScheme();
|
2017-03-01 18:47:52 +01:00
|
|
|
|
2019-01-24 10:53:03 +01:00
|
|
|
if (scheme != null && (scheme.equals("vnd.youtube") || scheme.equals("vnd.youtube.launch"))) {
|
2019-01-20 01:31:30 +01:00
|
|
|
String schemeSpecificPart = uri.getSchemeSpecificPart();
|
|
|
|
if (schemeSpecificPart.startsWith("//")) {
|
2020-09-03 02:29:18 +02:00
|
|
|
final String extractedId = extractId(schemeSpecificPart.substring(2));
|
|
|
|
if (extractedId != null) {
|
|
|
|
return extractedId;
|
2020-05-06 15:41:01 +02:00
|
|
|
}
|
|
|
|
|
2019-01-20 01:31:30 +01:00
|
|
|
urlString = "https:" + schemeSpecificPart;
|
2019-01-13 12:52:07 +01:00
|
|
|
} else {
|
2020-05-06 15:41:01 +02:00
|
|
|
return assertIsId(schemeSpecificPart);
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
2018-07-25 17:21:42 +02:00
|
|
|
}
|
2019-01-13 12:52:07 +01:00
|
|
|
} catch (URISyntaxException ignored) {
|
2018-07-25 17:21:42 +02:00
|
|
|
}
|
2019-01-13 12:52:07 +01:00
|
|
|
|
|
|
|
URL url;
|
|
|
|
try {
|
2019-01-20 01:31:30 +01:00
|
|
|
url = Utils.stringToURL(urlString);
|
2019-01-13 12:52:07 +01:00
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
throw new IllegalArgumentException("The given URL is not valid");
|
|
|
|
}
|
|
|
|
|
|
|
|
String host = url.getHost();
|
|
|
|
String path = url.getPath();
|
|
|
|
// remove leading "/" of URL-path if URL-path is given
|
|
|
|
if (!path.isEmpty()) {
|
|
|
|
path = path.substring(1);
|
|
|
|
}
|
|
|
|
|
2019-01-27 01:28:51 +01:00
|
|
|
if (!Utils.isHTTP(url) || !(YoutubeParsingHelper.isYoutubeURL(url) ||
|
|
|
|
YoutubeParsingHelper.isYoutubeServiceURL(url) || YoutubeParsingHelper.isHooktubeURL(url) ||
|
|
|
|
YoutubeParsingHelper.isInvidioURL(url))) {
|
2019-01-13 12:52:07 +01:00
|
|
|
if (host.equalsIgnoreCase("googleads.g.doubleclick.net")) {
|
|
|
|
throw new FoundAdException("Error found ad: " + urlString);
|
2018-02-12 17:29:36 +01:00
|
|
|
}
|
2019-01-13 12:52:07 +01:00
|
|
|
|
|
|
|
throw new ParsingException("The url is not a Youtube-URL");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (YoutubePlaylistLinkHandlerFactory.getInstance().acceptUrl(urlString)) {
|
|
|
|
throw new ParsingException("Error no suitable url: " + urlString);
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
2019-01-13 12:52:07 +01:00
|
|
|
|
|
|
|
// using uppercase instead of lowercase, because toLowercase replaces some unicode characters
|
|
|
|
// with their lowercase ASCII equivalent. Using toLowercase could result in faultily matching unicode urls.
|
|
|
|
switch (host.toUpperCase()) {
|
|
|
|
case "WWW.YOUTUBE-NOCOOKIE.COM": {
|
|
|
|
if (path.startsWith("embed/")) {
|
2021-02-13 22:19:49 +01:00
|
|
|
String id = path.substring(6); // embed/
|
2019-01-13 12:52:07 +01:00
|
|
|
|
2020-05-06 15:41:01 +02:00
|
|
|
return assertIsId(id);
|
2019-01-13 12:52:07 +01:00
|
|
|
}
|
2019-01-20 14:39:06 +01:00
|
|
|
|
|
|
|
break;
|
2018-07-25 17:21:42 +02:00
|
|
|
}
|
2019-01-13 12:52:07 +01:00
|
|
|
|
|
|
|
case "YOUTUBE.COM":
|
|
|
|
case "WWW.YOUTUBE.COM":
|
2019-09-12 04:43:49 +02:00
|
|
|
case "M.YOUTUBE.COM":
|
|
|
|
case "MUSIC.YOUTUBE.COM": {
|
2019-01-13 12:52:07 +01:00
|
|
|
if (path.equals("attribution_link")) {
|
|
|
|
String uQueryValue = Utils.getQueryValue(url, "u");
|
|
|
|
|
|
|
|
URL decodedURL;
|
|
|
|
try {
|
2019-01-20 01:31:30 +01:00
|
|
|
decodedURL = Utils.stringToURL("http://www.youtube.com" + uQueryValue);
|
2019-01-13 12:52:07 +01:00
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
throw new ParsingException("Error no suitable url: " + urlString);
|
|
|
|
}
|
|
|
|
|
|
|
|
String viewQueryValue = Utils.getQueryValue(decodedURL, "v");
|
2020-05-06 15:41:01 +02:00
|
|
|
return assertIsId(viewQueryValue);
|
2019-01-13 12:52:07 +01:00
|
|
|
}
|
|
|
|
|
2021-02-13 22:22:50 +01:00
|
|
|
String maybeId = getIdFromSubpathsInPath(path);
|
2021-02-10 17:06:51 +01:00
|
|
|
if (maybeId != null) return maybeId;
|
2019-01-13 12:52:07 +01:00
|
|
|
|
|
|
|
String viewQueryValue = Utils.getQueryValue(url, "v");
|
2020-05-06 15:41:01 +02:00
|
|
|
return assertIsId(viewQueryValue);
|
2018-07-25 17:21:42 +02:00
|
|
|
}
|
2019-01-13 12:52:07 +01:00
|
|
|
|
|
|
|
case "YOUTU.BE": {
|
|
|
|
String viewQueryValue = Utils.getQueryValue(url, "v");
|
|
|
|
if (viewQueryValue != null) {
|
2020-05-06 15:41:01 +02:00
|
|
|
return assertIsId(viewQueryValue);
|
2019-01-13 12:52:07 +01:00
|
|
|
}
|
|
|
|
|
2020-05-06 15:41:01 +02:00
|
|
|
return assertIsId(path);
|
2018-07-25 17:21:42 +02:00
|
|
|
}
|
2019-01-13 12:52:07 +01:00
|
|
|
|
2021-02-10 17:06:51 +01:00
|
|
|
case "HOOKTUBE.COM":
|
2019-08-08 02:19:02 +02:00
|
|
|
case "INVIDIO.US":
|
2021-01-22 20:58:14 +01:00
|
|
|
case "DEV.INVIDIO.US":
|
|
|
|
case "WWW.INVIDIO.US":
|
|
|
|
case "REDIRECT.INVIDIOUS.IO":
|
2019-08-08 02:19:02 +02:00
|
|
|
case "INVIDIOUS.SNOPYTA.ORG":
|
2020-07-02 21:31:05 +02:00
|
|
|
case "YEWTU.BE":
|
2020-11-11 15:54:16 +01:00
|
|
|
case "TUBE.CONNECT.CAFE":
|
|
|
|
case "INVIDIOUS.ZAPASHCANON.FR":
|
|
|
|
case "INVIDIOUS.KAVIN.ROCKS":
|
|
|
|
case "INVIDIOUS.TUBE":
|
|
|
|
case "INVIDIOUS.SITE":
|
|
|
|
case "INVIDIOUS.XYZ":
|
|
|
|
case "VID.MINT.LGBT":
|
|
|
|
case "INVIDIOU.SITE":
|
2021-01-22 19:20:22 +01:00
|
|
|
case "INVIDIOUS.FDN.FR":
|
|
|
|
case "INVIDIOUS.048596.XYZ":
|
|
|
|
case "INVIDIOUS.ZEE.LI":
|
|
|
|
case "VID.PUFFYAN.US":
|
2021-01-22 20:58:14 +01:00
|
|
|
case "YTPRIVATE.COM": { // code-block for hooktube.com and Invidious instances
|
2019-01-24 11:13:01 +01:00
|
|
|
if (path.equals("watch")) {
|
|
|
|
String viewQueryValue = Utils.getQueryValue(url, "v");
|
|
|
|
if (viewQueryValue != null) {
|
2020-05-06 15:41:01 +02:00
|
|
|
return assertIsId(viewQueryValue);
|
2019-01-24 11:13:01 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-13 22:22:50 +01:00
|
|
|
String maybeId = getIdFromSubpathsInPath(path);
|
2021-02-10 17:06:51 +01:00
|
|
|
if (maybeId != null) return maybeId;
|
2019-01-27 01:28:51 +01:00
|
|
|
|
2020-02-12 18:59:46 +01:00
|
|
|
String viewQueryValue = Utils.getQueryValue(url, "v");
|
|
|
|
if (viewQueryValue != null) {
|
2020-05-06 15:41:01 +02:00
|
|
|
return assertIsId(viewQueryValue);
|
2020-02-12 18:59:46 +01:00
|
|
|
}
|
|
|
|
|
2020-05-06 15:41:01 +02:00
|
|
|
return assertIsId(path);
|
2018-07-25 17:21:42 +02:00
|
|
|
}
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
2019-01-13 12:52:07 +01:00
|
|
|
|
|
|
|
throw new ParsingException("Error no suitable url: " + urlString);
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|
|
|
|
|
2017-07-11 05:08:03 +02:00
|
|
|
@Override
|
2018-07-01 16:21:40 +02:00
|
|
|
public boolean onAcceptUrl(final String url) throws FoundAdException {
|
2018-07-25 17:21:42 +02:00
|
|
|
try {
|
|
|
|
getId(url);
|
|
|
|
return true;
|
|
|
|
} catch (FoundAdException fe) {
|
|
|
|
throw fe;
|
|
|
|
} catch (ParsingException e) {
|
2017-03-01 18:47:52 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-02-10 17:06:51 +01:00
|
|
|
|
2021-02-13 22:22:50 +01:00
|
|
|
private String getIdFromSubpathsInPath(String path) throws ParsingException {
|
2021-02-16 19:07:24 +01:00
|
|
|
for (final String subpath : SUBPATHS) {
|
2021-02-13 22:22:50 +01:00
|
|
|
if (path.startsWith(subpath)) {
|
|
|
|
String id = path.substring(subpath.length());
|
2021-02-10 17:06:51 +01:00
|
|
|
return assertIsId(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2017-03-01 18:47:52 +01:00
|
|
|
}
|