Advertisement
milio48

scrape yt

Aug 11th, 2023 (edited)
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Fungsi untuk mendapatkan informasi tambahan dari halaman video
  2. function fetchVideoInformation(link) {
  3.   return fetch(`https://www.youtube.com${link}`)
  4.     .then(response => response.text())
  5.     .then(text => {
  6.       const date = findDate(text);
  7.       const description = findDesc(text);
  8.       return { date, description };
  9.     })
  10.     .catch(error => {
  11.       console.error("Error fetching video information:", error);
  12.       return { date: "N/A", description: "N/A" };
  13.     });
  14. }
  15.  
  16. // Fungsi untuk mendownload data sebagai file
  17. function downloadData(data, filename, type) {
  18.   const blob = new Blob([data], { type });
  19.   const url = URL.createObjectURL(blob);
  20.   const a = document.createElement('a');
  21.   a.href = url;
  22.   a.download = filename;
  23.   a.click();
  24.   URL.revokeObjectURL(url);
  25. }
  26.  
  27. // Fungsi untuk menghasilkan data dalam format CSV
  28. function convertToCSV(data) {
  29.   const header = ['Video ID', 'Title', 'Link', 'View', 'Ket', 'Durasi', 'Date', 'Description'];
  30.   const rows = data.map(item => [item.videoId, item.title, item.link, item.view, item.ket, item.durasi, item.date, item.description]);
  31.   const csvRows = [header, ...rows].map(row => row.join(',,,,,,,,,,'));
  32.   return csvRows.join('\n');
  33. }
  34.  
  35. // Fungsi untuk mendapatkan tanggal dari teks
  36. function findDate(text) {
  37.   const startIdx = text.split('"publishDate":"');
  38.   const endIdx = startIdx[1].split('","ownerChannelName"');
  39.   const foundString = endIdx[0];
  40.   return foundString;
  41. }
  42.  
  43. // Fungsi untuk mendapatkan deskripsi dari teks
  44. function findDesc(text) {
  45.   const startIdx = text.split('"shortDescription":"');
  46.   const endIdx = startIdx[1].split('","isCrawlable"');
  47.   const foundString = endIdx[0];
  48.   return foundString;
  49. }
  50.  
  51. // Fungsi untuk Membersihkan String durasi
  52. function cleanDurasi(inputString) {
  53.   return inputString.trim().replace(/\s+/g, "");
  54. }
  55.  
  56. // Fungsi untuk mendapatkan informasi video dari halaman channel
  57. async function scrapeVideoInformation() {
  58.   const videoElements = document.querySelectorAll('.style-scope ytd-rich-item-renderer');
  59.   const videoList = [];
  60.   const promises = [];
  61.  
  62.   for (let index = 0; index < videoElements.length; index++) {
  63.     const element = videoElements[index];
  64.     const titleElement = element.querySelector('a#video-title-link');
  65.     const title = titleElement ? titleElement.textContent : 'N/A';
  66.     const link = titleElement ? titleElement.getAttribute('href') : 'N/A';
  67.      
  68.     const metadata = element.querySelector('#metadata-line');
  69.     const durasi = cleanDurasi(element.querySelector("#time-status  #text").innerText);
  70.     const duaSpan = metadata.querySelectorAll("span");
  71.     const view = duaSpan[0].innerText;
  72.     const ket = duaSpan[1].innerText;
  73.  
  74.     promises.push(fetchVideoInformation(link).then(({ date, description }) => {
  75.       const videoId = index + 1; // Gunakan index + 1 sebagai ID
  76.       return { videoId, title, link, view, ket, durasi, date, description };
  77.     }));
  78.   }
  79.  
  80.   const sortedVideoList = await Promise.all(promises);
  81.  
  82.   // Urutkan videoList berdasarkan judul
  83.   sortedVideoList.sort((a, b) => a.title.localeCompare(b.title));
  84.  
  85.   const jsonOutput = JSON.stringify(sortedVideoList, null, 2);
  86.   const csvData = convertToCSV(sortedVideoList);
  87.  
  88.   downloadData(jsonOutput, 'output.json', 'application/json');
  89.   downloadData(csvData, 'output.csv', 'text/csv');
  90. }
  91.  
  92. // Panggil fungsi untuk memulai scraping
  93. scrapeVideoInformation();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement