@@ -34,6 +34,8 @@ public class YoutubeCommentsExtractor extends CommentsExtractor {
3434
3535 private List <String > cookies ;
3636 private String sessionToken ;
37+ private String ytClientVersion ;
38+ private String ytClientName ;
3739 private String title ;
3840 private InfoItemsPage <CommentsInfoItem > initPage ;
3941
@@ -144,11 +146,17 @@ private void fetchTitle(JsonArray contents) {
144146
145147 @ Override
146148 public void onFetchPage (Downloader downloader ) throws IOException , ExtractionException {
147- DownloadResponse response = downloader .get (getUrl ());
149+ Map <String , List <String >> requestHeaders = new HashMap <>();
150+ requestHeaders .put ("User-Agent" , Arrays .asList (USER_AGENT ));
151+ DownloadRequest request = new DownloadRequest (null , requestHeaders );
152+ DownloadResponse response = downloader .get (getUrl (), request );
148153 String responseBody = response .getResponseBody ();
149154 cookies = response .getResponseCookies ();
150- sessionToken = findValue (responseBody , "XSRF_TOKEN" );
151- String commentsToken = findValue (responseBody , "COMMENTS_TOKEN" );
155+ sessionToken = findValue (responseBody , "XSRF_TOKEN\" :\" " , "\" " );
156+ ytClientVersion = findValue (responseBody , "INNERTUBE_CONTEXT_CLIENT_VERSION\" :\" " , "\" " );
157+ ytClientName = findValue (responseBody , "INNERTUBE_CONTEXT_CLIENT_NAME\" :" , "," );
158+ String commentsTokenInside = findValue (responseBody , "itemSectionRenderer" , "comment-item-section" );
159+ String commentsToken = findValue (commentsTokenInside , "continuation\" :\" " , "\" " );
152160 initPage = getPage (getNextPageUrl (commentsToken ));
153161 }
154162
@@ -167,8 +175,8 @@ private String makeAjaxRequest(String siteUrl) throws IOException, ReCaptchaExce
167175 requestHeaders .put ("Content-Type" , Arrays .asList ("application/x-www-form-urlencoded" ));
168176 requestHeaders .put ("Accept" , Arrays .asList ("*/*" ));
169177 requestHeaders .put ("User-Agent" , Arrays .asList (USER_AGENT ));
170- requestHeaders .put ("X-YouTube-Client-Version" , Arrays .asList ("2.20180815" ));
171- requestHeaders .put ("X-YouTube-Client-Name" , Arrays .asList ("1" ));
178+ requestHeaders .put ("X-YouTube-Client-Version" , Arrays .asList (ytClientVersion ));
179+ requestHeaders .put ("X-YouTube-Client-Name" , Arrays .asList (ytClientName ));
172180 DownloadRequest request = new DownloadRequest (postData , requestHeaders );
173181 request .setRequestCookies (cookies );
174182
@@ -190,10 +198,10 @@ private String getDataString(Map<String, String> params) throws UnsupportedEncod
190198 return result .toString ();
191199 }
192200
193- private String findValue (String doc , String key ) {
194- int beginIndex = doc .indexOf (key ) + key .length () + 4 ;
195- int endIndex = doc .indexOf (" \" " , beginIndex );
201+ private String findValue (String doc , String start , String end ) {
202+ int beginIndex = doc .indexOf (start ) + start .length ();
203+ int endIndex = doc .indexOf (end , beginIndex );
196204 return doc .substring (beginIndex , endIndex );
197205 }
198-
206+
199207}
0 commit comments