gaspar_schot / Archive.org Playlists

// ==UserScript==
// @name         Archive.org Playlists
// @namespace    https://openuserjs.org/users/gaspar_schot
// @version      1.0.1
// @description  Adds a link to download an m3u playlist of the mp3 files in the current archive.org page.
// @author       gaspar_schot
// @copyright    2019, gaspar_schot (https://openuserjs.org/users/gaspar_schot)
// @updateURL    https://openuserjs.org/meta/gaspar_schot/Archive.org_Playlists.meta.js
// @license      MIT
// @match        https://archive.org/*
// @require      http://code.jquery.com/jquery-latest.min.js

// ==/UserScript==

(function() {
    'use strict';
    const $ = window.jQuery;

 	function decodeURIComponentSafe(s) {
 		if ( !s ) { return s; }
        return decodeURIComponent(s.replace(/%(?![0-9a-fA-F]{2})/g, '%25') ); // replace % with %25 if not followed by two a-f/number
 	}
    window.onload = (event) => {
        const $download_cont = $('.boxy.item-download-options');
        const $mp3_download_cont = $download_cont.find('.format-summary[href*="MP3"]');
        let $download_playlist = '<div class="format-group"><a href="" id="download_playlist"><button style="color:#2a6496;border:0;color:#428bca;font-family:Helvetica Neue,Helvetica,Arial,sans-serif;font-weight:normal;font-size:14px;padding:0;background:transparent;">M3U PLAYLIST</button></a></div>';
        $download_cont.find('.format-group').last().after($download_playlist);
        let index = 0;

        // get times from "theatre"
        let times = [];
        $('#jw6__list').find('a').each(function() {
            let time = $(this).find('.tm').text().trim();
                time = time.split(':').reverse(); // 00:26:50
                time = Number.parseInt(time[0]) + Number.parseInt(time[1]*60) + Number.parseInt((time[2]|0)*3600);
            times.push(time);
        });
        // get links and build list
        let $mp3_links_list = '#EXTM3U\n';
        if ( $mp3_download_cont.length ) {
            let $mp3_links_cont = $mp3_download_cont.siblings('.quickdown');

            $mp3_links_cont.find('a').each(function() {
                let $mp3_link = $(this).attr('href');
                    $mp3_link = 'https://archive.org' + $mp3_link;
                let $mp3_file_name = $mp3_link.slice($mp3_link.lastIndexOf('/') + 1).replace(/_/g,' ');
                let $file_length = $(this).find('div.down-rite').text().trim();
                $mp3_links_list += '#EXTINF:' + times[index] + ',' + decodeURIComponentSafe($mp3_file_name) + '\n' + $mp3_link + '\n';
                index++;
            });
            // add download button
            $('#download_playlist').on('click',function() {
                download($mp3_links_list, $('.item-details-metadata > h1 > span').text() +'.m3u', 'text/plain');
            });

        };

        function download(text, name, type) {
            let $download_playlist = document.getElementById("download_playlist");
            let file = new Blob([text], {type: type});
            $download_playlist.href = URL.createObjectURL(file);
            $download_playlist.download = name;
            URL.revokeObjectURL(file);
        }
    }

})();