Search code examples
pythonyoutubeyoutube-apigdata

Retrieving timestamp of YouTube comments


Here is the API response for retrieving comments of a YouTube video, taken from https://developers.google.com/youtube/2.0/developers_guide_protocol_comments:

<feed>
  <entry>
    ...
    <media:group>
      ...
    </media:group>
    <gd:comments>
      <gd:feedLink
        href='https://gdata.youtube.com/feeds/api/videos/VIDEO_ID/comments'/>
    </gd:comments>
  </entry>
</feed>

I am not sure which API to use to get this data. I have written my code in Python which gives me the author name and comment of a video. I want to fetch the timestamp of each comment for my research work.


Solution

  • Well, it would help to see the code you're using to get the author and comment text so I could give you the actual code that does it. But without that, looking at the API response, each comment is in an <entry> tag under the top-level <feed> tag.

    For each comment there is both a <published> and an <updated> tag with timestamps. I'm guessing those are the date of the original comment and the date of the last edit. If I had your code to see how you're parsing the xml to begin with, I could probably add a snippet for you to retrieve those.

    Edit: given the code in the link below. Here's a modification of the main loop that should do what you want.

    for comment in comments_generator(client, VIDEO_ID):
            author_name = comment.author[0].name.text
            text = comment.content.text
    
            post_date = comment.published.text
            last_update_date = comment.update.text            
    
            print("{}(date:{}): {}".format(author_name, post_date, text))
    

    Note that the dates are in text format. If you want to extract python datetime objects from them, check out dateutil and this question.