Search code examples
youtubeyoutube-apiyoutube-data-api

How to check oldest comments on a YouTube video?


Im trying to use the YouTube data api to get the oldest comments on a certain YouTube video but I don't believe that is possible through the api. Do I have to get every single comment on the video, and then order it to complete this task? Are there any websites that do this already that can help me other than hadzy.com?


Solution

    1. It is possible to retrieve the oldest comments on a certain YouTube video using YouTube Data API v3 but AFAIK not with a single query.

    2. By default comments are ordered by time with the most recent first, see CommentThreads: list

    3. I tested emap.cc but it doesn't work or is far too slow.

    If you are looking for the first comment writer on MrBeast 100 M subscribers video with his challenge winning 10,000 $ as announced. Then as far as I checked, MrBeast pinned for a few minutes that Troy Gaines has won (whose channel should be this one according to me with the comment $$$ published at 20:00:01). However by running the following Python algorithm you can verify that he isn't the first one and that's instead Marina Marcon having commented Meu at 19:59:59 (the video was made public at 19:59:59 (use Videos: list to check it), assuming that both of these persons haven't changed their name):

    import json, requests
    
    # https://www.youtube.com/watch?v=VIDEO_ID gives the approximate number of comments we have to retrieve
    
    def curl(url):
        return requests.get(url).text
    
    API_KEY = 'API_KEY'
    VIDEO_ID = '2isYuQZMbdU'
    
    nextPageToken = ''
    itemsCounter = 0
    
    while True:
        url = f'https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&videoId={VIDEO_ID}&key={API_KEY}&maxResults=100'
        if nextPageToken != '':
            url += f'&pageToken={nextPageToken}'
        responseStr = curl(url)
        try:
            responseJSON = json.loads(responseStr)
        except:
            print(url, responseStr)
        items = responseJSON['items']
        for item in items:
            if 'Troy Gaines' == item['snippet']['topLevelComment']['snippet']['authorDisplayName']:
                print(item)
        ids = [item['id'] for item in items]
        itemsCounter += len(items)
        print(nextPageToken, itemsCounter, len(items))
        if 'nextPageToken' in responseJSON and nextPageToken != responseJSON['nextPageToken']:
            nextPageToken = responseJSON['nextPageToken']
        else:
            print(url, items[-1])
            break