Skip to main content
Table of Contents

Reading comments

Each video carries a discussion of comment threads, and each thread can have replies. TTMSFNCCloudGoogleYouTube fetches a video's threads into its CommentThreads collection, and every thread exposes its own Replies. This chapter covers requesting the threads for a video and reading the threads together with their replies.

Reading comment threads

Comments belong to a video, so the request starts from a video object: AVideo.GetCommentThreads triggers the fetch, and OnGetCommentThreadsComplete fires when the threads are ready. Read them from the component's CommentThreads collection; each TTMSFNCCloudGoogleYouTubeCommentThread exposes AuthorDisplayName and TextDisplay. This example, combining a video from Working with videos with its comments, lists every thread and its replies.

Read comment threads and repliesPascalC++
procedure TForm1.LoadComments(AVideo: TTMSFNCCloudGoogleYouTubeVideo);
begin
  // GetCommentThreads is a method on the video object itself.
  AVideo.GetCommentThreads; // result arrives in OnGetCommentThreadsComplete
end;

procedure TForm1.YouTubeGetCommentThreads(Sender: TObject;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
var
  I, J: Integer;
  Thread: TTMSFNCCloudGoogleYouTubeCommentThread;
  Reply: TTMSFNCCloudGoogleYouTubeReply;
begin
  if not ARequestResult.Success then
    Exit;

  ListBox2.Clear;
  for I := 0 to FYouTube.CommentThreads.Count - 1 do
  begin
    Thread := FYouTube.CommentThreads[I];
    ListBox2.Items.AddObject(Thread.AuthorDisplayName + ': ' + Thread.TextDisplay, Thread);

    // Each top-level comment can carry replies.
    for J := 0 to Thread.Replies.Count - 1 do
    begin
      Reply := Thread.Replies[J];
      ListBox2.Items.Add('    ' + Reply.AuthorDisplayName + ': ' + Reply.TextDisplay);
    end;
  end;
end;
void __fastcall TForm1::LoadComments(TTMSFNCCloudGoogleYouTubeVideo *AVideo)
{
  // GetCommentThreads is a method on the video object itself.
  AVideo->GetCommentThreads(); // result arrives in OnGetCommentThreadsComplete
}

void __fastcall TForm1::YouTubeGetCommentThreads(TObject *Sender,
  TTMSFNCCloudBaseRequestResult *ARequestResult)
{
  if (!ARequestResult->Success)
    return;

  ListBox2->Clear();
  for (int I = 0; I < FYouTube->CommentThreads->Count; I++)
  {
    TTMSFNCCloudGoogleYouTubeCommentThread *Thread = FYouTube->CommentThreads->Items[I];
    ListBox2->Items->AddObject(Thread->AuthorDisplayName + ": " + Thread->TextDisplay, Thread);

    // Each top-level comment can carry replies.
    for (int J = 0; J < Thread->Replies->Count; J++)
    {
      TTMSFNCCloudGoogleYouTubeReply *Reply = Thread->Replies->Items[J];
      ListBox2->Items->Add("    " + Reply->AuthorDisplayName + ": " + Reply->TextDisplay);
    }
  }
}

Replies

A thread's Replies collection holds the responses to the top-level comment. Each TTMSFNCCloudGoogleYouTubeReply exposes the same AuthorDisplayName and TextDisplay, so you can render a thread and its responses with the same code, as the snippet does by indenting replies under their thread.

Pitfalls

  • Start from the video. GetCommentThreads is a method on the TTMSFNCCloudGoogleYouTubeVideo, not the component; load the videos first.
  • Read in the completion event. CommentThreads is only guaranteed after OnGetCommentThreadsComplete fires.
  • A video may have comments disabled, in which case the threads collection comes back empty — handle that case rather than assuming results.

See also