Created
August 5, 2015 16:39
-
-
Save jvanderwee/b30fdb496acff43aef8e to your computer and use it in GitHub Desktop.
Revisions
-
Joseph Van der Wee created this gist
Aug 5, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ import com.google.inject.Singleton; import java.util.regex.Matcher; import java.util.regex.Pattern; @Singleton public class YouTubeHelper { final String youTubeUrlRegEx = "^(https?)?(://)?(www.)?(m.)?((youtube.com)|(youtu.be))/"; final String[] videoIdRegex = { "\\?vi?=([^&]*)","watch\\?.*v=([^&]*)", "(?:embed|vi?)/([^/?]*)", "^([A-Za-z0-9\\-]*)"}; public String extractVideoIdFromUrl(String url) { String youTubeLinkWithoutProtocolAndDomain = youTubeLinkWithoutProtocolAndDomain(url); for(String regex : videoIdRegex) { Pattern compiledPattern = Pattern.compile(regex); Matcher matcher = compiledPattern.matcher(youTubeLinkWithoutProtocolAndDomain); if(matcher.find()){ return matcher.group(1); } } return null; } private String youTubeLinkWithoutProtocolAndDomain(String url) { Pattern compiledPattern = Pattern.compile(youTubeUrlRegEx); Matcher matcher = compiledPattern.matcher(url); if(matcher.find()){ return url.replace(matcher.group(), ""); } return url; } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,46 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import java.util.Arrays; import java.util.Collection; import static org.junit.Assert.assertEquals; @RunWith(Parameterized.class) public class YouTubeHelperTest { @Parameterized.Parameters public static Collection<Object[]> data() { return Arrays.asList(new Object[][] { { "youtube.com/v/vidid" }, { "youtube.com/vi/vidid" }, { "youtube.com/?v=vidid" }, { "youtube.com/?vi=vidid" }, { "youtube.com/watch?v=vidid" }, { "youtube.com/watch?vi=vidid" }, { "youtu.be/vidid" }, { "youtube.com/embed/vidid" }, { "youtube.com/embed/vidid" }, { "www.youtube.com/v/vidid" }, { "http://www.youtube.com/v/vidid" }, { "https://www.youtube.com/v/vidid" }, { "youtube.com/watch?v=vidid&wtv=wtv" }, { "http://www.youtube.com/watch?dev=inprogress&v=vidid&feature=related" }, { "https://m.youtube.com/watch?v=vidid" } }); } private String url; public YouTubeHelperTest(String url) { this.url= url; } private YouTubeHelper youTubeHelper = new YouTubeHelper(); @Test public void extractingVideoIdFromUrlShouldReturnVideoId() { assertEquals("Unable to extract correct video id from url " + url, "vidid", youTubeHelper.extractVideoIdFromUrl(url)); } }