-
-
Save chonamdoo/f8abdacc1d325036e878c543fc7032d4 to your computer and use it in GitHub Desktop.
Extract video id from YouTube url in java
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 characters
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 characters
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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment