spotDL / spotify-downloader

Download your Spotify playlists and songs along with album art and metadata (from YouTube if a match is found).
https://spotdl.readthedocs.io/en/latest/
MIT License
17.38k stars 1.6k forks source link

Usefull TamperMonkey script to help download songs with spotDL (Windows version) #1907

Open smat-al opened 1 year ago

smat-al commented 1 year ago

helpfull Feature

i created this tm/script that help you to create a download .bat directly from desidred song or list all you have to do is click from the keybord in alt+q ( you can modify as you want), then a file will be downloded, click on it, and it will start downloading

this is it:

// ==UserScript==
// @name         Spotify Downloader
// @namespace    Moroccan way to say thank you
// @version      1.0
// @description  Alt+Q to download Spotify playlist/song using spotdl
// @author       Your name
// @match        https://open.spotify.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to create the download link
    function createDownloadLink(url) {
        // Modify the link with the appropriate spotdl command
        var link = "C:\\Users\\Said\\Documents\\spotify-dl\\spotdl " + url + " --playlist-numbering --bitrate 320k\npause";
        // Create a Blob object and save it as a txt file
        var blob = new Blob([link], {type: "text/plain;charset=utf-8"});
        var fileName = document.querySelector('h1[data-encore-id="type"]').textContent + ".bat";
        var fileUrl = URL.createObjectURL(blob);
        // Save the file in the specified folder
        var downloadLink = document.createElement("a");
        downloadLink.href = fileUrl;
        downloadLink.download = fileName;
        downloadLink.style.display = "none";
        document.body.appendChild(downloadLink);
        downloadLink.click();
        // Remove the link from the DOM after clicking it
        document.body.removeChild(downloadLink);
    }

    // Function to handle the key press event
    function handleKeyPress(event) {
        if (event.keyCode == 81 && event.altKey) { // Alt+Q
            // Get the URL of the current playlist/song
            var url = window.location.href;
            // Call the function to create the download link
            createDownloadLink(url);
        }
    }

    // Add event listener to handle the key press event
    document.addEventListener('keydown', handleKeyPress, false);
})();
smat-all commented 6 days ago

// ==UserScript== // @name Spotify Downloader // @namespace http://tampermonkey // @version 2.3 // @description Alt+X to download Spotify using spotdl, or click the button // @author SG // @match ://open.spotify.com/ // @grant none // ==/UserScript==

(function() { 'use strict';

// Function to create the download button with the specified style
function createDownloadButton() {
    var targetDiv = document.querySelector('div.paiZmlAHHhmZonuGJRAr');
    if (targetDiv) {
        var downloadButton = document.createElement("button");
        downloadButton.innerHTML = '<i class="fa fa-download"></i> Download';
        downloadButton.classList.add("btn");
        downloadButton.addEventListener('click', downloadSpotify); // Call downloadSpotify() directly

        // Add CSS styles to the download button
        downloadButton.style.backgroundColor = "#808080"; // Grey background color
        downloadButton.style.borderRadius = "10px"; // Slightly rounder
        downloadButton.style.color = "white"; // Text color
        downloadButton.style.padding = "6px 20px"; // Padding
        downloadButton.style.cursor = "pointer"; // Cursor style
        downloadButton.style.fontSize = "15px"; // Font size

        // Darker background on mouse-over
        downloadButton.addEventListener('mouseover', function() {
            downloadButton.style.backgroundColor = "#6c6c6c";
        });
        // Restore original background color on mouse-out
        downloadButton.addEventListener('mouseout', function() {
            downloadButton.style.backgroundColor = "#808080";
        });

        targetDiv.appendChild(downloadButton);
    } else {
        // Retry after a short delay if the target div is not found yet
        setTimeout(createDownloadButton, 1000);
    }
}

// Function to create the download link
function createDownloadLink(url, fileName) {
    // Modify the link with the appropriate spotdl command
    var link = "C:\\Users\\fboug\\OneDrive\\Belgeler\\spotify-dl\\spotdl " + url + " --playlist-numbering --bitrate 320k\npause";
    // Create a Blob object and save it as a txt file
    var blob = new Blob([link], {type: "text/plain;charset=utf-8"});
    var fileUrl = URL.createObjectURL(blob);
    // Save the file in the specified folder
    var downloadLink = document.createElement("a");
    downloadLink.href = fileUrl;
    downloadLink.download = fileName;
    downloadLink.style.display = "none";
    document.body.appendChild(downloadLink);
    downloadLink.click();
    // Remove the link from the DOM after clicking it
    document.body.removeChild(downloadLink);
}

// Function to extract the song name from the specified h1 element
function extractSongName() {
    var songNameElement = document.querySelector('h1[data-encore-id="text"]');
    if (songNameElement) {
        return songNameElement.textContent.trim();
    }
    return null;
}

// Function to download Spotify content
function downloadSpotify() {
    var url, fileName;
    // Extract the type (track, artist, playlist) from the URL
    var type = null;
    url = window.location.href;
    if (url.includes("/track/")) {
        type = "Track";
    } else if (url.includes("/artist/")) {
        type = "Artist";
    } else if (url.includes("/playlist/")) {
        type = "Playlist";
    } else if (url.includes("/album/")) {
        type = "Album";
    }
    if (!type) {
        console.log("Unsupported Spotify content type.");
        return;
    }

    // If it's an album, extract URL from the current URL
    if (type === "Album") {
        url = window.location.href; // Use current URL directly
        fileName = document.title.split(" · ")[0]; // Get the album name from the page title
        fileName += " (Album)";
    } else {
        // Extract the song name
        fileName = extractSongName();
        if (!fileName) {
            console.log("Unable to retrieve song name.");
            return;
        }
        // Replace invalid characters in file name
        fileName = fileName.replace(/[\/\\:*?"<>|]/g, ''); // Remove invalid characters
        fileName += " (" + type + ")";
    }

    // Call the function to create the download link
    createDownloadLink(url, fileName + ".bat");
}

// Function to handle the key press event
function handleKeyPress(event) {
    if (event.keyCode == 88 && event.altKey) { // Alt+X
        downloadSpotify();
    }
}

// Add event listener to handle the key press event
document.addEventListener('keydown', handleKeyPress, false);

// Inject the styled download button once the page has fully loaded
window.onload = function() {
    createDownloadButton();
};

})();