Youtube API 'nextPageToken' breaks when 'using order=relevance'












1















I am using python 3 to paginate through all comments in a video.
Let's say that the video, selected at random, has the id GFphNr0FK-0. As of now, there are 5450 comments.



I am paging through the results retrieved and checking if there is a nextPageToken key present. If there is, I grab the value from it and continue to loop until that key is no longer present.



With each iteration or page, I append the comments to a list. I get to 1600 comments (16 pages, 100 comments per page) and then I start getting error 400's.



When I inspect the nextPageToken, I see that with every request it gets longer and longer... I am not appending anything to it and it is literally retrieved using page_info['nextPageToken'].



For clarity, this is my paging:



self.COMMENT_URL_PAGED = 'https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&maxResults=100&order=relevance&videoId={videoId}&key={key}'

if 'nextPageToken' not in page_info:
run = False
else:
next_page_token = page_info['nextPageToken']

while run:

page_info = requests.get(self.COMMENT_URL_PAGED.format(videoId = video_id, key = self.KEY, pageToken = next_page_token))

while page_info.status_code != 200:
time.sleep(10)
page_info = requests.get(self.COMMENT_URL_PAGED.format(videoId = video_id, key = self.KEY, pageToken = next_page_token))
print('Something went wrong. Token is: {}'.format(next_page_token))

page_info = page_info.json()

for i in range(len(page_info['items'])):
comments.append(page_info['items'][i]['snippet']['topLevelComment']['snippet']['textOriginal'])

if 'nextPageToken' not in page_info:
run = False
next_page_token = None
else:
next_page_token = page_info['nextPageToken']


Anything obviously incorrect?



UPDATE



So, I have found that if I remove the order querystring parameter, it works as expected....



Why would &order=relevance cause it to break after 16 pages??










share|improve this question





























    1















    I am using python 3 to paginate through all comments in a video.
    Let's say that the video, selected at random, has the id GFphNr0FK-0. As of now, there are 5450 comments.



    I am paging through the results retrieved and checking if there is a nextPageToken key present. If there is, I grab the value from it and continue to loop until that key is no longer present.



    With each iteration or page, I append the comments to a list. I get to 1600 comments (16 pages, 100 comments per page) and then I start getting error 400's.



    When I inspect the nextPageToken, I see that with every request it gets longer and longer... I am not appending anything to it and it is literally retrieved using page_info['nextPageToken'].



    For clarity, this is my paging:



    self.COMMENT_URL_PAGED = 'https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&maxResults=100&order=relevance&videoId={videoId}&key={key}'

    if 'nextPageToken' not in page_info:
    run = False
    else:
    next_page_token = page_info['nextPageToken']

    while run:

    page_info = requests.get(self.COMMENT_URL_PAGED.format(videoId = video_id, key = self.KEY, pageToken = next_page_token))

    while page_info.status_code != 200:
    time.sleep(10)
    page_info = requests.get(self.COMMENT_URL_PAGED.format(videoId = video_id, key = self.KEY, pageToken = next_page_token))
    print('Something went wrong. Token is: {}'.format(next_page_token))

    page_info = page_info.json()

    for i in range(len(page_info['items'])):
    comments.append(page_info['items'][i]['snippet']['topLevelComment']['snippet']['textOriginal'])

    if 'nextPageToken' not in page_info:
    run = False
    next_page_token = None
    else:
    next_page_token = page_info['nextPageToken']


    Anything obviously incorrect?



    UPDATE



    So, I have found that if I remove the order querystring parameter, it works as expected....



    Why would &order=relevance cause it to break after 16 pages??










    share|improve this question



























      1












      1








      1








      I am using python 3 to paginate through all comments in a video.
      Let's say that the video, selected at random, has the id GFphNr0FK-0. As of now, there are 5450 comments.



      I am paging through the results retrieved and checking if there is a nextPageToken key present. If there is, I grab the value from it and continue to loop until that key is no longer present.



      With each iteration or page, I append the comments to a list. I get to 1600 comments (16 pages, 100 comments per page) and then I start getting error 400's.



      When I inspect the nextPageToken, I see that with every request it gets longer and longer... I am not appending anything to it and it is literally retrieved using page_info['nextPageToken'].



      For clarity, this is my paging:



      self.COMMENT_URL_PAGED = 'https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&maxResults=100&order=relevance&videoId={videoId}&key={key}'

      if 'nextPageToken' not in page_info:
      run = False
      else:
      next_page_token = page_info['nextPageToken']

      while run:

      page_info = requests.get(self.COMMENT_URL_PAGED.format(videoId = video_id, key = self.KEY, pageToken = next_page_token))

      while page_info.status_code != 200:
      time.sleep(10)
      page_info = requests.get(self.COMMENT_URL_PAGED.format(videoId = video_id, key = self.KEY, pageToken = next_page_token))
      print('Something went wrong. Token is: {}'.format(next_page_token))

      page_info = page_info.json()

      for i in range(len(page_info['items'])):
      comments.append(page_info['items'][i]['snippet']['topLevelComment']['snippet']['textOriginal'])

      if 'nextPageToken' not in page_info:
      run = False
      next_page_token = None
      else:
      next_page_token = page_info['nextPageToken']


      Anything obviously incorrect?



      UPDATE



      So, I have found that if I remove the order querystring parameter, it works as expected....



      Why would &order=relevance cause it to break after 16 pages??










      share|improve this question
















      I am using python 3 to paginate through all comments in a video.
      Let's say that the video, selected at random, has the id GFphNr0FK-0. As of now, there are 5450 comments.



      I am paging through the results retrieved and checking if there is a nextPageToken key present. If there is, I grab the value from it and continue to loop until that key is no longer present.



      With each iteration or page, I append the comments to a list. I get to 1600 comments (16 pages, 100 comments per page) and then I start getting error 400's.



      When I inspect the nextPageToken, I see that with every request it gets longer and longer... I am not appending anything to it and it is literally retrieved using page_info['nextPageToken'].



      For clarity, this is my paging:



      self.COMMENT_URL_PAGED = 'https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&maxResults=100&order=relevance&videoId={videoId}&key={key}'

      if 'nextPageToken' not in page_info:
      run = False
      else:
      next_page_token = page_info['nextPageToken']

      while run:

      page_info = requests.get(self.COMMENT_URL_PAGED.format(videoId = video_id, key = self.KEY, pageToken = next_page_token))

      while page_info.status_code != 200:
      time.sleep(10)
      page_info = requests.get(self.COMMENT_URL_PAGED.format(videoId = video_id, key = self.KEY, pageToken = next_page_token))
      print('Something went wrong. Token is: {}'.format(next_page_token))

      page_info = page_info.json()

      for i in range(len(page_info['items'])):
      comments.append(page_info['items'][i]['snippet']['topLevelComment']['snippet']['textOriginal'])

      if 'nextPageToken' not in page_info:
      run = False
      next_page_token = None
      else:
      next_page_token = page_info['nextPageToken']


      Anything obviously incorrect?



      UPDATE



      So, I have found that if I remove the order querystring parameter, it works as expected....



      Why would &order=relevance cause it to break after 16 pages??







      python-3.x python-requests youtube-api-v3






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 27 '18 at 12:38







      pookie

















      asked Nov 27 '18 at 12:24









      pookiepookie

      1,34031540




      1,34031540
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53499644%2fyoutube-api-nextpagetoken-breaks-when-using-order-relevance%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53499644%2fyoutube-api-nextpagetoken-breaks-when-using-order-relevance%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)