Merge pull request #324 from Stypox/music-detector-url

[YouTube] Support stream urls in `vnd.youtube://ID` form
This commit is contained in:
Tobias Groza 2020-05-08 17:43:04 +02:00 committed by GitHub
commit f3913e241e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 17 deletions

View File

@ -14,6 +14,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.
*
@ -45,12 +47,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}")) {
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");
}
return id;
}
@Override
@ -76,9 +82,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) {
@ -119,7 +130,7 @@ public class YoutubeStreamLinkHandlerFactory extends LinkHandlerFactory {
if (path.startsWith("embed/")) {
String id = path.split("/")[1];
return assertIsID(id);
return assertIsId(id);
}
break;
@ -140,38 +151,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
}
@ -194,21 +205,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());
}