Last active
September 1, 2024 13:08
-
-
Save yshalsager/c8b54a9ddabd04ad4a3889b370fc0e97 to your computer and use it in GitHub Desktop.
al-badr.net links CSV Extractor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name al-badr.net copy download links | |
// @match https://www.al-badr.net/sub/* | |
// @namespace http://tampermonkey.net/ | |
// @version 2024-09-01 | |
// @description Extracts audio links data into a CSV file | |
// @author yshalsager | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=al-badr.net | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
let csvData = ""; | |
function extractDataFromPage() { | |
const listItems = document.querySelectorAll('.list-file li'); | |
listItems.forEach(item => { | |
const titleElement = item.querySelector('h3 a'); | |
const downloadLink = item.querySelector('.tahmilfile a[href*="/download/"]'); | |
if (downloadLink && titleElement) { | |
let title = titleElement.textContent.trim(); | |
// Remove quotes and colons | |
title = title.replace(/[":]/g, ''); | |
title = title.replace(/\.$/, ""); // Remove trailing dots | |
// Extract extension from URL and add to title | |
const extensionMatch = downloadLink.href.match(/\.(mp3|wav|ogg|etc)$/); // Adjust extensions as needed | |
if (extensionMatch) { | |
const extension = extensionMatch[1]; | |
title = `${title}.${extension}`; | |
} | |
const url = downloadLink.href; | |
csvData += `"${title}","${url}"\n`; | |
} | |
}); | |
} | |
function handlePagination() { | |
// Get all pagination links | |
const paginationLinks = Array.from(document.querySelectorAll('.pager a[href*="page="]')); | |
// Find the next page button (with text ">") using filter | |
const nextPageButton = paginationLinks.filter(link => link.textContent.trim() === '>')[0]; | |
if (nextPageButton) { | |
const nextPageUrl = nextPageButton.href; | |
fetch(nextPageUrl) | |
.then(response => response.text()) | |
.then(html => { | |
const parser = new DOMParser(); | |
const doc = parser.parseFromString(html, 'text/html'); | |
document.body.innerHTML = doc.body.innerHTML; // Replace current page content | |
extractDataFromPage(); | |
handlePagination(); // Recursively call to handle further pagination | |
}); | |
} else { | |
// No more pages, download the CSV | |
downloadCSV(); | |
} | |
} | |
function downloadCSV() { | |
const blob = new Blob([csvData], { type: 'text/csv' }); | |
const url = window.URL.createObjectURL(blob); | |
const a = document.createElement('a'); | |
a.setAttribute('href', url); | |
a.setAttribute('download', 'extracted_data.csv'); | |
document.body.appendChild(a); | |
a.click(); | |
document.body.removeChild(a); | |
} | |
// Function to start the extraction | |
function startExtraction() { | |
extractDataFromPage(); | |
handlePagination(); | |
} | |
// Create a button element | |
const button = document.createElement('button'); | |
button.textContent = 'Extract CSV'; | |
button.style.position = 'fixed'; | |
button.style.top = '10px'; | |
button.style.right = '10px'; | |
button.style.zIndex = '9999'; // Ensure it's on top | |
// Add click event listener to the button | |
button.addEventListener('click', startExtraction); | |
// Append the button to the body | |
document.body.appendChild(button); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment