33import org .schabi .newpipe .extractor .exceptions .FoundAdException ;
44import org .schabi .newpipe .extractor .exceptions .ParsingException ;
55import org .schabi .newpipe .extractor .linkhandler .ListLinkHandlerFactory ;
6- import org .schabi .newpipe .extractor .utils .Utils ;
76
8- import java .net .MalformedURLException ;
9- import java .net .URI ;
10- import java .net .URISyntaxException ;
11- import java .net .URL ;
127import java .util .List ;
138
149public class YoutubeCommentsLinkHandlerFactory extends ListLinkHandlerFactory {
@@ -19,160 +14,14 @@ public static YoutubeCommentsLinkHandlerFactory getInstance() {
1914 return instance ;
2015 }
2116
22- private static String assertIsID (String id ) throws ParsingException {
23- if (id == null || !id .matches ("[a-zA-Z0-9_-]{11}" )) {
24- throw new ParsingException ("The given string is not a Youtube-Video-ID" );
25- }
26-
27- return id ;
28- }
29-
3017 @ Override
3118 public String getUrl (String id ) {
3219 return "https://m.youtube.com/watch?v=" + id ;
3320 }
3421
3522 @ Override
3623 public String getId (String urlString ) throws ParsingException , IllegalArgumentException {
37- try {
38- URI uri = new URI (urlString );
39- String scheme = uri .getScheme ();
40-
41- if (scheme != null && (scheme .equals ("vnd.youtube" ) || scheme .equals ("vnd.youtube.launch" ))) {
42- String schemeSpecificPart = uri .getSchemeSpecificPart ();
43- if (schemeSpecificPart .startsWith ("//" )) {
44- urlString = "https:" + schemeSpecificPart ;
45- } else {
46- return assertIsID (schemeSpecificPart );
47- }
48- }
49- } catch (URISyntaxException ignored ) {
50- }
51-
52- URL url ;
53- try {
54- url = Utils .stringToURL (urlString );
55- } catch (MalformedURLException e ) {
56- throw new IllegalArgumentException ("The given URL is not valid" );
57- }
58-
59- String host = url .getHost ();
60- String path = url .getPath ();
61- // remove leading "/" of URL-path if URL-path is given
62- if (!path .isEmpty ()) {
63- path = path .substring (1 );
64- }
65-
66- if (!Utils .isHTTP (url ) || !(YoutubeParsingHelper .isYoutubeURL (url ) ||
67- YoutubeParsingHelper .isYoutubeServiceURL (url ) || YoutubeParsingHelper .isHooktubeURL (url ) ||
68- YoutubeParsingHelper .isInvidioURL (url ))) {
69- if (host .equalsIgnoreCase ("googleads.g.doubleclick.net" )) {
70- throw new FoundAdException ("Error found ad: " + urlString );
71- }
72-
73- throw new ParsingException ("The url is not a Youtube-URL" );
74- }
75-
76- if (YoutubePlaylistLinkHandlerFactory .getInstance ().acceptUrl (urlString )) {
77- throw new ParsingException ("Error no suitable url: " + urlString );
78- }
79-
80- // using uppercase instead of lowercase, because toLowercase replaces some unicode characters
81- // with their lowercase ASCII equivalent. Using toLowercase could result in faultily matching unicode urls.
82- switch (host .toUpperCase ()) {
83- case "WWW.YOUTUBE-NOCOOKIE.COM" : {
84- if (path .startsWith ("embed/" )) {
85- String id = path .split ("/" )[1 ];
86-
87- return assertIsID (id );
88- }
89-
90- break ;
91- }
92-
93- case "YOUTUBE.COM" :
94- case "WWW.YOUTUBE.COM" :
95- case "M.YOUTUBE.COM" :
96- case "MUSIC.YOUTUBE.COM" : {
97- if (path .equals ("attribution_link" )) {
98- String uQueryValue = Utils .getQueryValue (url , "u" );
99-
100- URL decodedURL ;
101- try {
102- decodedURL = Utils .stringToURL ("http://www.youtube.com" + uQueryValue );
103- } catch (MalformedURLException e ) {
104- throw new ParsingException ("Error no suitable url: " + urlString );
105- }
106-
107- String viewQueryValue = Utils .getQueryValue (decodedURL , "v" );
108- return assertIsID (viewQueryValue );
109- }
110-
111- if (path .startsWith ("embed/" )) {
112- String id = path .split ("/" )[1 ];
113-
114- return assertIsID (id );
115- }
116-
117- String viewQueryValue = Utils .getQueryValue (url , "v" );
118- return assertIsID (viewQueryValue );
119- }
120-
121- case "YOUTU.BE" : {
122- String viewQueryValue = Utils .getQueryValue (url , "v" );
123- if (viewQueryValue != null ) {
124- return assertIsID (viewQueryValue );
125- }
126-
127- return assertIsID (path );
128- }
129-
130- case "HOOKTUBE.COM" : {
131- if (path .startsWith ("v/" )) {
132- String id = path .substring ("v/" .length ());
133-
134- return assertIsID (id );
135- }
136- if (path .startsWith ("watch/" )) {
137- String id = path .substring ("watch/" .length ());
138-
139- return assertIsID (id );
140- }
141- // there is no break-statement here on purpose so the next code-block gets also run for hooktube
142- }
143-
144- case "WWW.INVIDIO.US" :
145- case "DEV.INVIDIO.US" :
146- case "INVIDIO.US" :
147- case "INVIDIOUS.SNOPYTA.ORG" :
148- case "DE.INVIDIOUS.SNOPYTA.ORG" :
149- case "FI.INVIDIOUS.SNOPYTA.ORG" :
150- case "VID.WXZM.SX" :
151- case "INVIDIOUS.KABI.TK" :
152- case "INVIDIOU.SH" :
153- case "WWW.INVIDIOU.SH" :
154- case "NO.INVIDIOU.SH" :
155- case "INVIDIOUS.ENKIRTON.NET" :
156- case "TUBE.POAL.CO" :
157- case "INVIDIOUS.13AD.DE" :
158- case "YT.ELUKERIO.ORG" : { // code-block for hooktube.com and Invidious instances
159- if (path .equals ("watch" )) {
160- String viewQueryValue = Utils .getQueryValue (url , "v" );
161- if (viewQueryValue != null ) {
162- return assertIsID (viewQueryValue );
163- }
164- }
165- if (path .startsWith ("embed/" )) {
166- String id = path .substring ("embed/" .length ());
167-
168- return assertIsID (id );
169- }
170-
171- break ;
172- }
173- }
174-
175- throw new ParsingException ("Error no suitable url: " + urlString );
24+ return YoutubeStreamLinkHandlerFactory .getInstance ().getId (urlString ); //we need the same id, avoids duplicate code
17625 }
17726
17827 @ Override
0 commit comments