[YouTube] Support stream urls in `vnd.youtube://ID` form

This commit is contained in:
Stypox 2020-05-06 15:41:01 +02:00
parent 4086715a68
commit a02bfbeae5
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
2 changed files with 29 additions and 17 deletions

View File

@ -13,6 +13,8 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import javax.annotation.Nullable;
/*
* Created by Christian Schabesberger on 02.02.16.
*
@ -44,12 +46,16 @@ public class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory {
return instance;
}
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");
private static boolean isId(@Nullable String id) {
return id != null && id.matches("[a-zA-Z0-9_-]{11}");
}
private static String assertIsId(@Nullable String id) throws ParsingException {
if (isId(id)) {
return id;
} else {
throw new ParsingException("The given string is not a Youtube-Video-ID");
}
}
@Override
@ -75,9 +81,14 @@ public class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory {
if (scheme != null && (scheme.equals("vnd.youtube") || scheme.equals("vnd.youtube.launch"))) {
String schemeSpecificPart = uri.getSchemeSpecificPart();
if (schemeSpecificPart.startsWith("//")) {
final String possiblyId = schemeSpecificPart.substring(2);
if (isId(possiblyId)) {
return possiblyId;
}
urlString = "https:" + schemeSpecificPart;
} else {
return assertIsID(schemeSpecificPart);
return assertIsId(schemeSpecificPart);
}
}
} catch (URISyntaxException ignored) {
@ -118,7 +129,7 @@ public class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory {
if (path.startsWith("embed/")) {
String id = path.split("/")[1];
return assertIsID(id);
return assertIsId(id);
}
break;
@ -139,38 +150,38 @@ public class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory {
}
String viewQueryValue = Utils.getQueryValue(decodedURL, "v");
return assertIsID(viewQueryValue);
return assertIsId(viewQueryValue);
}
if (path.startsWith("embed/")) {
String id = path.split("/")[1];
return assertIsID(id);
return assertIsId(id);
}
String viewQueryValue = Utils.getQueryValue(url, "v");
return assertIsID(viewQueryValue);
return assertIsId(viewQueryValue);
}
case "YOUTU.BE": {
String viewQueryValue = Utils.getQueryValue(url, "v");
if (viewQueryValue != null) {
return assertIsID(viewQueryValue);
return assertIsId(viewQueryValue);
}
return assertIsID(path);
return assertIsId(path);
}
case "HOOKTUBE.COM": {
if (path.startsWith("v/")) {
String id = path.substring("v/".length());
return assertIsID(id);
return assertIsId(id);
}
if (path.startsWith("watch/")) {
String id = path.substring("watch/".length());
return assertIsID(id);
return assertIsId(id);
}
// there is no break-statement here on purpose so the next code-block gets also run for hooktube
}
@ -193,21 +204,21 @@ public class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory {
if (path.equals("watch")) {
String viewQueryValue = Utils.getQueryValue(url, "v");
if (viewQueryValue != null) {
return assertIsID(viewQueryValue);
return assertIsId(viewQueryValue);
}
}
if (path.startsWith("embed/")) {
String id = path.substring("embed/".length());
return assertIsID(id);
return assertIsId(id);
}
String viewQueryValue = Utils.getQueryValue(url, "v");
if (viewQueryValue != null) {
return assertIsID(viewQueryValue);
return assertIsId(viewQueryValue);
}
return assertIsID(path);
return assertIsId(path);
}
}

View File

@ -79,6 +79,7 @@ public class YoutubeStreamLinkHandlerFactoryTest {
assertEquals("EhxJLojIE_o", linkHandler.fromUrl("http://www.youtube.com/attribution_link?a=JdfC0C9V6ZI&u=%2Fwatch%3Fv%3DEhxJLojIE_o%26feature%3Dshare").getId());
assertEquals("jZViOEv90dI", linkHandler.fromUrl("vnd.youtube://www.youtube.com/watch?v=jZViOEv90dI").getId());
assertEquals("jZViOEv90dI", linkHandler.fromUrl("vnd.youtube:jZViOEv90dI").getId());
assertEquals("n8X9_MgEdCg", linkHandler.fromUrl("vnd.youtube://n8X9_MgEdCg").getId());
assertEquals("O0EDx9WAelc", linkHandler.fromUrl("https://music.youtube.com/watch?v=O0EDx9WAelc").getId());
}