package com.thefinestartist.ytpa.utils;

import android.net.Uri;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by TheFinestArtist on 2/15/15.
 */
public class YouTubeUrlParser {

    // ^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/
    final static String reg = "^.*((youtu.be\\/)|(v\\/)|(\\/u\\/w\\/)|(embed\\/)|(watch\\?))\\??v?=?([^#\\&\\?]*).*";

    public static String parseVideoId(String url) {
        String videoDd = null;
        if (url != null && url.trim().length() > 0 && url.startsWith("http")) {
            Pattern pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(url);

            // Regular reg some how doesn't work with id with "v" at prefix
            if (matcher.matches()) {
                String groupIndex = matcher.group(7);
                if (groupIndex != null && groupIndex.length() == 11)
                    videoDd = groupIndex;
                else if (groupIndex != null && groupIndex.length() == 10)
                    videoDd = "v" + groupIndex;
            }
        }
        return videoDd;
    }

    public static String getVideoId(String videoUrl) {
        if (videoUrl == null)
            return null;

        Uri uri = Uri.parse(videoUrl);
        String videoId = uri.getQueryParameter("v");

        if (videoId == null)
            videoId = parseVideoId(videoUrl);

        return videoId;
    }

    public static String getVidoeUrl(String videoId) {
        return "http://www.youtube.com/watch?v=" + videoId;
    }
}

@@ -38,33 +38,8 @@
 s;%0A%0A
-import android.net.Uri;%0A%0A
 impo
@@ -193,84 +193,123 @@
  // 
-%5E.*((
+/(?:
 youtu
-.
 be
-%5C/)%7C(v%5C/)%7C(%5C/u%5C/%5Cw
+(?:-nocookie)?%5C.com%5C/(?:%5B%5E%5C/%5Cn%5Cs%5D+%5C/%5CS+
 %5C/
-)
 %7C(
-embed%5C/)%7C(watch%5C?))%5C??v?=?(%5B%5E#%5C&%5C?%5D*).*/
+?:v%7Ce(?:mbed)?)%5C/%7C%5CS*?%5B?&%5Dv=)%7Cyoutu%5C.be%5C/)(%5Ba-zA-Z0-9_-%5D%7B11%7D)
 %0A   
@@ -340,92 +340,135 @@
  = %22
-%5E.*((
+/(?:
 youtu
-.
 be
-%5C%5C/)%7C(v%5C%5C/)%7C(
+(?:-nocookie)?%5C%5C.com%5C%5C/(?:%5B%5E%5C%5C/%5C%5Cn%5C%5Cs%5D+
 %5C%5C/
-u
 %5C%5C
-/w
+S+
 %5C%5C/
-)
 %7C(
-embed%5C%5C/)%7C(watch%5C%5C?))%5C%5C??v?=?(%5B%5E#%5C%5C&%5C%5C?%5D*).*
+?:v%7Ce(?:mbed)?)%5C%5C/%7C%5C%5CS*?%5B?&%5Dv=)%7Cyoutu%5C%5C.be%5C%5C/)(%5Ba-zA-Z0-9_-%5D%7B11%7D)
 %22;%0A%0A
@@ -492,21 +492,19 @@
  String 
-parse
+get
 VideoId(
@@ -510,17 +510,22 @@
 (String 
-u
+videoU
 rl) %7B%0A  
@@ -534,59 +534,38 @@
     
-String videoDd = null;%0A        if (url !
+if (videoUrl =
 = null 
-&& u
+%7C%7C videoU
 rl.t
@@ -583,45 +583,40 @@
 h() 
-%3E 0 && url.startsWith(%22http%22)) %7B%0A    
+%3C= 0)%0A            return null;%0A%0A
     
@@ -693,20 +693,16 @@
         
-    
 Matcher 
@@ -731,772 +731,104 @@
 her(
-url);%0A%0A            // Regular reg some how doesn't work with id with %22v%22 at prefix%0A            if (matcher.matches()) %7B%0A                String groupIndex = matcher.group(7);%0A                if (groupIndex != null && groupIndex.length() == 11)%0A                    videoDd = groupIndex;%0A                else if (groupIndex != null && groupIndex.length() == 10)%0A                    videoDd = %22v%22 + groupIndex;%0A            %7D%0A        %7D%0A        return videoDd;%0A    %7D%0A%0A    public static String getVideoId(String videoUrl) %7B%0A        if (videoUrl == null)%0A            return null;%0A%0A        Uri uri = Uri.parse(videoUrl);%0A        String videoId = uri.getQueryParameter(%22v%22);%0A%0A        if (videoId == null)%0A            videoId = parseVideoId(videoUrl);%0A%0A        return videoId
+videoUrl);%0A%0A        if (matcher.find())%0A            return matcher.group(1);%0A        return null
 ;%0A  
@@ -914,32 +914,17 @@
 p://
-www.
 youtu
+.
 be
-.com/watch?v=
+/
 %22 + 

Update Url Parser