· 6 years ago · Feb 24, 2020, 10:52 AM
1(async function() {
2 const key = "МЕСТО ДЛЯ YT API KEY";
3
4 async function getVideosCount(channelId) {
5 let videosCount = 0;
6 let nextPageId = null;
7 let itemsLength = 0;
8 do {
9 const json = await fetch(`https://www.googleapis.com/youtube/v3/search?key=${key}&channelId=${channelId}&part=id&type=video&maxResults=50${ nextPageId == null ? "" : "&pageToken=" + nextPageId }`).then(r => r.json());
10 itemsLength = json.items.length;
11 videosCount += itemsLength;
12 nextPageId = json.nextPageToken;
13 } while(itemsLength === 50);
14 return videosCount;
15 }
16
17 function downloadBlob(blob, name) {
18 const url = window.URL.createObjectURL(blob);
19 var el = document.createElement('div');
20 el.innerHTML = `<a href="${ url }" download="${ name }"></a>`;
21 const link = document.body.appendChild(el.firstChild);
22 link.click();
23 }
24
25 const channels = document.querySelectorAll('a[href^="/channel/"][title]:not([title="YouTube Movies"])')
26 let rv = [];
27 const cCount = channels.length;
28 let i = 0;
29 for(const channel of channels) {
30 const name = channel.title;
31 console.log(`Обработка канала "${name}". Прогресс ${ ((i/cCount)*100).toFixed(0) }%`)
32 i++;
33 const href = channel.href.toString();
34 const id = channel.getAttribute('href').substring(9);
35 const vCount = await getVideosCount(id);
36 rv.push(`[${vCount} ${href}]`);
37 }
38 downloadBlob(new Blob([rv.join('\n')], { type: 'text/plain' }), "видосы.txt");
39})();