Skip to content

Instantly share code, notes, and snippets.

@zoutepopcorn
Created December 8, 2020 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoutepopcorn/c3cb0d0be7e262f4de6597b74a58ad98 to your computer and use it in GitHub Desktop.
Save zoutepopcorn/c3cb0d0be7e262f4de6597b74a58ad98 to your computer and use it in GitHub Desktop.
const API_KEY = `UsEYourOwnApIK3Yp|ea5e`; // https://console.developers.google.com/apis/credentials?project=phot-256520
const MAX_PAGES = 10 // * 50
const VIDEO_ID = new URLSearchParams((new URL(window.location.href)).search).get('v');
const API_URL = `https://www.googleapis.com/youtube/v3/commentThreads?key=${API_KEY}&textFormat=plainText&part=snippet&videoId=${VIDEO_ID}&maxResults=50`;
const API_NEXT_URL = `https://www.googleapis.com/youtube/v3/commentThreads?key=${API_KEY}&textFormat=plainText&part=snippet&maxResults=50&pageToken=`;
const getComments = async (nextToken) => {
const resp = await fetch(nextToken ? `${API_NEXT_URL}${nextToken}` : API_URL)
const json = await resp.json();
return json;
}
const getAllComments = async () => {
let hasNext = true;
let nextToken = null;
let pageCounter = 0;
const allComments = [];
while(hasNext) {
try {
const OUT = await getComments(nextToken);
allComments.push(...OUT.items);
console.log("output.nextPageToken ", OUT.nextPageToken);
console.log(pageCounter);
hasNext = OUT.nextPageToken && pageCounter++ < MAX_PAGES;
} catch (e) {
console.log("erreur ", e);
hasNext = false;
}
}
return allComments;
}
let ALL_COMMENTS;
getAllComments().then((allComments) => {
console.log(allComments);
ALL_COMMENTS = allComments;
})
// copy(ALL_COMMENTS) after that
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment