Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fungsi untuk mendapatkan informasi tambahan dari halaman video
- function fetchVideoInformation(link) {
- return fetch(`https://www.youtube.com${link}`)
- .then(response => response.text())
- .then(text => {
- const date = findDate(text);
- const description = findDesc(text);
- return { date, description };
- })
- .catch(error => {
- console.error("Error fetching video information:", error);
- return { date: "N/A", description: "N/A" };
- });
- }
- // Fungsi untuk mendownload data sebagai file
- function downloadData(data, filename, type) {
- const blob = new Blob([data], { type });
- const url = URL.createObjectURL(blob);
- const a = document.createElement('a');
- a.href = url;
- a.download = filename;
- a.click();
- URL.revokeObjectURL(url);
- }
- // Fungsi untuk menghasilkan data dalam format CSV
- function convertToCSV(data) {
- const header = ['Video ID', 'Title', 'Link', 'View', 'Ket', 'Durasi', 'Date', 'Description'];
- const rows = data.map(item => [item.videoId, item.title, item.link, item.view, item.ket, item.durasi, item.date, item.description]);
- const csvRows = [header, ...rows].map(row => row.join(',,,,,,,,,,'));
- return csvRows.join('\n');
- }
- // Fungsi untuk mendapatkan tanggal dari teks
- function findDate(text) {
- const startIdx = text.split('"publishDate":"');
- const endIdx = startIdx[1].split('","ownerChannelName"');
- const foundString = endIdx[0];
- return foundString;
- }
- // Fungsi untuk mendapatkan deskripsi dari teks
- function findDesc(text) {
- const startIdx = text.split('"shortDescription":"');
- const endIdx = startIdx[1].split('","isCrawlable"');
- const foundString = endIdx[0];
- return foundString;
- }
- // Fungsi untuk Membersihkan String durasi
- function cleanDurasi(inputString) {
- return inputString.trim().replace(/\s+/g, "");
- }
- // Fungsi untuk mendapatkan informasi video dari halaman channel
- async function scrapeVideoInformation() {
- const videoElements = document.querySelectorAll('.style-scope ytd-rich-item-renderer');
- const videoList = [];
- const promises = [];
- for (let index = 0; index < videoElements.length; index++) {
- const element = videoElements[index];
- const titleElement = element.querySelector('a#video-title-link');
- const title = titleElement ? titleElement.textContent : 'N/A';
- const link = titleElement ? titleElement.getAttribute('href') : 'N/A';
- const metadata = element.querySelector('#metadata-line');
- const durasi = cleanDurasi(element.querySelector("#time-status #text").innerText);
- const duaSpan = metadata.querySelectorAll("span");
- const view = duaSpan[0].innerText;
- const ket = duaSpan[1].innerText;
- promises.push(fetchVideoInformation(link).then(({ date, description }) => {
- const videoId = index + 1; // Gunakan index + 1 sebagai ID
- return { videoId, title, link, view, ket, durasi, date, description };
- }));
- }
- const sortedVideoList = await Promise.all(promises);
- // Urutkan videoList berdasarkan judul
- sortedVideoList.sort((a, b) => a.title.localeCompare(b.title));
- const jsonOutput = JSON.stringify(sortedVideoList, null, 2);
- const csvData = convertToCSV(sortedVideoList);
- downloadData(jsonOutput, 'output.json', 'application/json');
- downloadData(csvData, 'output.csv', 'text/csv');
- }
- // Panggil fungsi untuk memulai scraping
- scrapeVideoInformation();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement