Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
smat-al opened this issue Sep 20, 2023 · 3 comments
Labels
META Indicates an issue or pull request that is related to the project’s management or organization, rath

Comments

@smat-al
Copy link

smat-al commented Sep 20, 2023

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-al smat-al added the Feature Request Feature Request label Sep 20, 2023
@xnetcat xnetcat added META Indicates an issue or pull request that is related to the project’s management or organization, rath and removed Feature Request Feature Request labels Nov 10, 2023
@xnetcat xnetcat changed the title usefull tampermokey script to help download (windowns version) Usefull TamperMonkey script to help download songs with spotDL (Windows version) Nov 10, 2023
@smat-all
Copy link

smat-all commented Oct 23, 2024

// ==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();
    };
})();

@MyPC8MyBrain
Copy link

having few issues with this nice script i am hoping there's a simple fix,
the main issue all files on my playlist are saved by default to c:\windows\system32,
is there a way to specify path to save downloaded files in?
next issue i am having is with metadata,
my username on Spotify is used as the "artist",
for "album" data my playlist name is used,
and in "comment" field the url for YouTube where the song was fetched from is saved,
the same album cover artwork is saved with each song (which is the artwork file for the playlist on spotify).

@xnetcat
Copy link
Member

xnetcat commented Nov 25, 2024

having few issues with this nice script i am hoping there's a simple fix, the main issue all files on my playlist are saved by default to c:\windows\system32, is there a way to specify path to save downloaded files in? next issue i am having is with metadata, my username on Spotify is used as the "artist", for "album" data my playlist name is used, and in "comment" field the url for YouTube where the song was fetched from is saved, the same album cover artwork is saved with each song (which is the artwork file for the playlist on spotify).

Hi, you will have to modify the link variable. You can find all the available options here

// Modify Path to spotdl
var link = "C:\\Users\\fboug\\OneDrive\\Belgeler\\spotify-dl\\spotdl " + url + " --output /some/other/path \npause";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
META Indicates an issue or pull request that is related to the project’s management or organization, rath
Projects
None yet
Development

No branches or pull requests

4 participants