Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.net.URISyntaxException;
import java.net.URL;

import javax.annotation.Nullable;

/*
* Created by Christian Schabesberger on 02.02.16.
*
Expand Down Expand Up @@ -44,12 +46,16 @@ public static YoutubeStreamLinkHandlerFactory getInstance() {
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
Expand All @@ -75,9 +81,14 @@ public String getId(String urlString) throws ParsingException, IllegalArgumentEx
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) {
Expand Down Expand Up @@ -118,7 +129,7 @@ public String getId(String urlString) throws ParsingException, IllegalArgumentEx
if (path.startsWith("embed/")) {
String id = path.split("/")[1];

return assertIsID(id);
return assertIsId(id);
}

break;
Expand All @@ -139,38 +150,38 @@ public String getId(String urlString) throws ParsingException, IllegalArgumentEx
}

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
}
Expand All @@ -193,21 +204,21 @@ public String getId(String urlString) throws ParsingException, IllegalArgumentEx
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public void getIdfromYt() throws Exception {
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());
}

Expand Down