@@ -60,61 +60,59 @@ public String getId(String url) throws ParsingException, IllegalArgumentExceptio
6060 throw new IllegalArgumentException ("The url parameter should not be empty" );
6161 }
6262
63- String id ;
6463 String lowercaseUrl = url .toLowerCase ();
6564 if (lowercaseUrl .contains ("youtube" )) {
6665 if (lowercaseUrl .contains ("list=" )) {
6766 throw new ParsingException ("Error no suitable url: " + url );
68- } else if (url .contains ("attribution_link" )) {
67+ }
68+ if (url .contains ("attribution_link" )) {
6969 try {
7070 String escapedQuery = Parser .matchGroup1 ("u=(.[^&|$]*)" , url );
7171 String query = URLDecoder .decode (escapedQuery , "UTF-8" );
72- id = Parser .matchGroup1 ("v=" + ID_PATTERN , query );
72+ return Parser .matchGroup1 ("v=" + ID_PATTERN , query );
7373 } catch (UnsupportedEncodingException uee ) {
7474 throw new ParsingException ("Could not parse attribution_link" , uee );
7575 }
76- } else if (lowercaseUrl .contains ("youtube.com/shared?ci=" )) {
76+ }
77+ if (lowercaseUrl .contains ("youtube.com/shared?ci=" )) {
7778 return getRealIdFromSharedLink (url );
78- } else if (url .contains ("vnd.youtube" )) {
79- id = Parser .matchGroup1 (ID_PATTERN , url );
80- } else if (url .contains ("embed" )) {
81- id = Parser .matchGroup1 ("embed/" + ID_PATTERN , url );
82- } else if (url .contains ("googleads" )) {
79+ }
80+ if (url .contains ("vnd.youtube" )) {
81+ return Parser .matchGroup1 (ID_PATTERN , url );
82+ }
83+ if (url .contains ("embed" )) {
84+ return Parser .matchGroup1 ("embed/" + ID_PATTERN , url );
85+ }
86+ if (url .contains ("googleads" )) {
8387 throw new FoundAdException ("Error found add: " + url );
84- } else {
85- id = Parser .matchGroup1 ("[?&]v=" + ID_PATTERN , url );
8688 }
87- } else if (lowercaseUrl .contains ("youtu.be" )) {
89+ return Parser .matchGroup1 ("[?&]v=" + ID_PATTERN , url );
90+ }
91+ if (lowercaseUrl .contains ("youtu.be" )) {
8892 if (lowercaseUrl .contains ("list=" )) {
8993 throw new ParsingException ("Error no suitable url: " + url );
90- } else if (url .contains ("v=" )) {
91- id = Parser .matchGroup1 ("v=" + ID_PATTERN , url );
92- } else {
93- id = Parser .matchGroup1 ("[Yy][Oo][Uu][Tt][Uu]\\ .[Bb][Ee]/" + ID_PATTERN , url );
9494 }
95- } else if (lowercaseUrl .contains ("hooktube" )) {
96- if (lowercaseUrl .contains ("&v=" )
97- || lowercaseUrl .contains ("?v=" )) {
98- id = Parser .matchGroup1 ("[?&]v=" + ID_PATTERN , url );
99- } else if (url .contains ("/embed/" )) {
100- id = Parser .matchGroup1 ("embed/" + ID_PATTERN , url );
101- } else if (url .contains ("/v/" )) {
102- id = Parser .matchGroup1 ("v/" + ID_PATTERN , url );
103- } else if (url .contains ("/watch/" )) {
104- id = Parser .matchGroup1 ("watch/" + ID_PATTERN , url );
105- } else {
106- throw new ParsingException ("Error no suitable url: " + url );
95+ if (url .contains ("v=" )) {
96+ return Parser .matchGroup1 ("v=" + ID_PATTERN , url );
10797 }
108- } else {
109- throw new ParsingException ("Error no suitable url: " + url );
98+ return Parser .matchGroup1 ("[Yy][Oo][Uu][Tt][Uu]\\ .[Bb][Ee]/" + ID_PATTERN , url );
11099 }
111-
112-
113- if (!id .isEmpty ()) {
114- return id ;
115- } else {
116- throw new ParsingException ("Error could not parse url: " + url );
100+ if (lowercaseUrl .contains ("hooktube" )) {
101+ if (lowercaseUrl .contains ("&v=" )
102+ || lowercaseUrl .contains ("?v=" )) {
103+ return Parser .matchGroup1 ("[?&]v=" + ID_PATTERN , url );
104+ }
105+ if (url .contains ("/embed/" )) {
106+ return Parser .matchGroup1 ("embed/" + ID_PATTERN , url );
107+ }
108+ if (url .contains ("/v/" )) {
109+ return Parser .matchGroup1 ("v/" + ID_PATTERN , url );
110+ }
111+ if (url .contains ("/watch/" )) {
112+ return Parser .matchGroup1 ("watch/" + ID_PATTERN , url );
113+ }
117114 }
115+ throw new ParsingException ("Error no suitable url: " + url );
118116 }
119117
120118 /**
@@ -170,19 +168,18 @@ private String getSharedId(URI uri) throws ParsingException {
170168 @ Override
171169 public boolean onAcceptUrl (final String url ) throws FoundAdException {
172170 final String lowercaseUrl = url .toLowerCase ();
173- if (lowercaseUrl .contains ("youtube" )
174- || lowercaseUrl .contains ("youtu.be" )
175- || lowercaseUrl .contains ("hooktube" )) {
176- // bad programming I know
177- try {
178- getId (url );
179- return true ;
180- } catch (FoundAdException fe ) {
181- throw fe ;
182- } catch (ParsingException e ) {
183- return false ;
184- }
185- } else {
171+ if (!lowercaseUrl .contains ("youtube" ) &&
172+ !lowercaseUrl .contains ("youtu.be" ) &&
173+ !lowercaseUrl .contains ("hooktube" )) {
174+ return false ;
175+ // bad programming I know <-- nice meme
176+ }
177+ try {
178+ getId (url );
179+ return true ;
180+ } catch (FoundAdException fe ) {
181+ throw fe ;
182+ } catch (ParsingException e ) {
186183 return false ;
187184 }
188185 }
0 commit comments