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;
|
|
|
|
import org.schabi.newpipe.extractor.utils.Utils;
|
2017-03-01 18:47:52 +01:00
|
|
|
|
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;
|
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
|
|
|
|
2018-07-13 18:02:40 +02:00
|
|
|
private static final YoutubeStreamLinkHandlerFactory instance = new YoutubeStreamLinkHandlerFactory();
|
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;
|
|
|
|
}
|
|
|
|
|
2019-01-13 12:52:07 +01:00
|
|
|
private static String assertIsID(String id) throws ParsingException {
|
|
|
|
if (id == null || !id.matches("[a-zA-Z0-9_-]{11}")) {
|
|
|
|
throw new ParsingException("The given string is not a Youtube-Video-ID");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 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);
|
2017-03-01 18:47:52 +01:00
|
|
|
|
2019-01-13 12:52:07 +01:00
|
|
|
if (uri.getScheme().equals("vnd.youtube")) {
|
|
|
|
String scheme = uri.getSchemeSpecificPart();
|
|
|
|
if (scheme.startsWith("//")) {
|
|
|
|
urlString = "https:" + scheme;
|
|
|
|
} else {
|
|
|
|
return assertIsID(scheme);
|
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 {
|
|
|
|
url = new URL(urlString);
|
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!YoutubeParsingHelper.isYoutubeALikeURL(url)) {
|
|
|
|
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/")) {
|
|
|
|
String id = path.split("/")[1];
|
|
|
|
|
|
|
|
return assertIsID(id);
|
|
|
|
}
|
2018-07-25 17:21:42 +02:00
|
|
|
}
|
2019-01-13 12:52:07 +01:00
|
|
|
|
|
|
|
case "YOUTUBE.COM":
|
|
|
|
case "WWW.YOUTUBE.COM":
|
|
|
|
case "M.YOUTUBE.COM": {
|
|
|
|
if (path.equals("attribution_link")) {
|
|
|
|
String uQueryValue = Utils.getQueryValue(url, "u");
|
|
|
|
|
|
|
|
URL decodedURL;
|
|
|
|
try {
|
|
|
|
decodedURL = new URL("http://www.youtube.com" + uQueryValue);
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
throw new ParsingException("Error no suitable url: " + urlString);
|
|
|
|
}
|
|
|
|
|
|
|
|
String viewQueryValue = Utils.getQueryValue(decodedURL, "v");
|
|
|
|
return assertIsID(viewQueryValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path.startsWith("embed/")) {
|
|
|
|
String id = path.split("/")[1];
|
|
|
|
|
|
|
|
return assertIsID(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
String viewQueryValue = Utils.getQueryValue(url, "v");
|
|
|
|
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) {
|
|
|
|
return assertIsID(viewQueryValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
return assertIsID(path);
|
2018-07-25 17:21:42 +02:00
|
|
|
}
|
2019-01-13 12:52:07 +01:00
|
|
|
|
|
|
|
case "HOOKTUBE.COM": {
|
|
|
|
if (path.equals("watch")) {
|
|
|
|
String viewQueryValue = Utils.getQueryValue(url, "v");
|
|
|
|
if (viewQueryValue != null) {
|
|
|
|
return assertIsID(viewQueryValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (path.startsWith("embed/")) {
|
|
|
|
String id = path.substring("embed/".length());
|
|
|
|
|
|
|
|
return assertIsID(id);
|
|
|
|
}
|
|
|
|
if (path.startsWith("v/")) {
|
|
|
|
String id = path.substring("v/".length());
|
|
|
|
|
|
|
|
return assertIsID(id);
|
|
|
|
}
|
|
|
|
if (path.startsWith("watch/")) {
|
|
|
|
String id = path.substring("watch/".length());
|
|
|
|
|
|
|
|
return assertIsID(id);
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|