java - Youtube CommentThread not returning all comments -
i trying create small app can retrieve comments youtube video. using youtube data api v3 try , retrieve comments, reason gives me fraction of comments on video.
thd id of video being tested on ozpqv1mylce
this code using count number of comments right now. uses auth package sample code provided google.
import com.google.api.client.auth.oauth2.credential; import com.google.api.services.samples.youtube.cmdline.auth; import com.google.api.services.youtube.youtube; import com.google.api.services.youtube.model.commentthread; import com.google.api.services.youtube.model.commentthreadlistresponse; import com.google.common.collect.lists; import java.util.list; public class youtubetest { public static void main(string args[]){ list<string> scopes = lists.newarraylist("https://www.googleapis.com/auth/youtube.force-ssl"); try{ credential credential = auth.authorize(scopes, "commentthreads"); youtube youtube = new youtube.builder(auth.http_transport, auth.json_factory, credential) .setapplicationname("youtubetest").build(); string videoid = "ozpqv1mylce"; int count = 0; string nextpage = null; commentthreadlistresponse response = youtube.commentthreads().list("snippet,replies") .setvideoid(videoid).settextformat("plaintext").setmaxresults(100l).execute(); nextpage = response.getnextpagetoken(); count += response.getitems().size(); for(commentthread t : response.getitems()){ if(t.getreplies() != null) count += t.getreplies().size(); } int = 0; while(nextpage != null){ response = youtube.commentthreads().list("snippet,replies") .setvideoid(videoid).settextformat("plaintext").setmaxresults(100l).setpagetoken(nextpage).execute(); nextpage = response.getnextpagetoken(); count += response.getitems().size(); for(commentthread t : response.getitems()){ if(t.getreplies() != null) count += t.getreplies().size(); } system.out.println(i++); } system.out.println("total number of comments: " + count); } catch (exception e){ e.printstacktrace(); } } }
the output running is:
0 1 total number of comments: 313
however @ time of posting thread thread has 5049 comments (can seen here: https://www.youtube.com/all_comments?v=ozpqv1mylce )
i believe pagination correct in code counting replies threads. limitation in youtube api? or doing wrong not aware of? in figuring problem out appreciated
Comments
Post a Comment