Rai Play video download

This script allows you to download videos on Rai Play

目前為 2020-10-27 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name            Rai Play video download
// @namespace       http://andrealazzarotto.com
// @version         11.0.1
// @description     This script allows you to download videos on Rai Play
// @description:it  Questo script ti permette di scaricare i video su Rai Play
// @author          Andrea Lazzarotto
// @match           https://www.raiplay.it/*
// @require         https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js
// @require         https://cdnjs.cloudflare.com/ajax/libs/foundation-essential/6.2.2/js/foundation.min.js
// @require         https://cdnjs.cloudflare.com/ajax/libs/arrive/2.4.1/arrive.min.js
// @require         https://unpkg.com/@ungap/[email protected]/min.js
// @grant           GM_xmlhttpRequest
// @grant           GM.xmlHttpRequest
// @connect         rai.it
// @connect         akamaized.net
// @license         GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// ==/UserScript==

/* Greasemonkey 4 wrapper */
if (typeof GM !== "undefined" && !!GM.xmlHttpRequest) {
    GM_xmlhttpRequest = GM.xmlHttpRequest;
}

function fetch(params) {
    return new Promise(function(resolve, reject) {
        params.onload = resolve;
        params.onerror = reject;
        GM_xmlhttpRequest(params);
    });
}

(function() {
    'use strict';

    var $ = window.jQuery;
    var Foundation = window.Foundation;
    var download_icon = '<svg viewBox="0 0 48 48" xmlns="http://www.w3.org/2000/svg"><path d="M42 40v4H6v-4h36zM20 24v-8h8v8h7L24 37 13 24h7zm8-14v4h-8v-4h8zm0-6v4h-8V4h8z" /></svg>';

    var showModal = (title, content) => {
        var modal = $(`
            <div id="video-download-modal" class="small reveal" data-reveal aria-labelledby="Download video">
                <h2 id="modal-title">${title}</h2>
                <div id="modal-content"></div>
                <button class="close-button" data-close aria-label="Chiudi finestrella" type="button">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
        `);
        modal.css({
            'background-color': '#001623',
        });
        modal.find('#modal-content').append(content);
        var instance = new Foundation.Reveal(modal);
        instance.open();
        modal.find('.close-button').css({
            'color': 'white',
        }).click(() => instance.close());
    };

    var checkQuality = (url, rate) => {
        return fetch({
            method: 'HEAD',
            url: url,
            headers: {
                'User-Agent': 'raiweb',
            },
        }).then(
            (response) => {
                let headers = fromEntries(response.responseHeaders.split("\n").map(element => element.trim().split(":")));
                let size = +headers['content-length'];
                let megabytes = Math.round(size / 1024 / 1024);
                if (size > 500) {
                    return { quality: rate, url: url, megabytes: megabytes };
                } else {
                    return null;
                }
            },
            () => null
        );
    }

    var qualities = async (url) => {
        var ending = url.match(/_[1-9]+00\.mp4/);
        if (!ending || !ending.length) {
            let result = await checkQuality(url, '');
            return [result];
        }

        var results = await Promise.all([5000, 3200, 2400, 1800, 1500, 1200, 800, 600, 400].map(rate => {
            var quality_url = url.replace(/_[1-9]+00\.mp4/, `_${rate}.mp4`);
            return checkQuality(quality_url, rate);
        }));

        return results.filter(value => (value !== null));
    };

    var getVideo = () => {
        var path = location.href.replace(/\.html(\?.*)?$/, '.json');
        $.getJSON(path).then((data) => {
            var secure = data.video.content_url.replace('http://', 'https://');
            return fetch({
                method: 'HEAD',
                url: secure,
                headers: {
                    'User-Agent': 'raiweb',
                },
            }).then(
                (response) => {
                    var valid = response.finalUrl.indexOf('mp4') > 0;
                    if (!valid) {
                        showModal('Niente da fare...', "<p>Non è stato possibile trovare un link del video in formato MP4. Il video potrebbe essere protetto da DRM.</p>");
                    } else {
                        qualities(response.finalUrl).then(results => {
                            var buttons = '';
                            results.forEach(video => {
                                buttons += `<a href="${video.url}" class="button" target="_blank">MP4 ${video.quality} (${video.megabytes} MB)</a>`;
                            });

                            showModal('Link diretti', `
                                <p>Clicca un bottone per aprire il video in formato MP4. Usa il tasto destro per salvarlo.</p>
                                <p><strong>Per evitare interruzioni è raccomandato l'uso di un download manager.</strong></p>
                                <p>${buttons}</p>`);
                        });
                    }
                },
                () => {
                    showModal('Errore di rete', "<p>Si è verificato un errore di rete. Riprova più tardi.</p>");
                }
            );
        });
    };

    var downloadButton = () => {
        if ($('#video-download-button').length) {
            return;
        }

        $('.rai-player-infobutton').before(`
            <button id="video-download-button" class="vjs-control vjs-button" aria-disabled="false">
                <span aria-hidden="true" class="vjs-icon-placeholder">${download_icon}</span>
                <span class="vjs-control-text" aria-live="polite">Download</span>
            </button>
        `);
        $('#video-download-button').click(getVideo)
            .find('svg').css({
                'fill': '#039cf9',
                'height': '3.5em',
            });
    };

    $(document).arrive('rai-player .rai-player-infobutton', () => {
        downloadButton();
    });

    var isAnon = function() {
        return !!$('#accountPanelLoginPanel').is(':visible');
    };

    $(document).ready(() => {
        if (location.pathname.startsWith('/video')) {
            $('rai-sharing').after(`
                <a id="inline-download-button" class="cell small-4 medium-shrink highlight__share" aria-label="Download">
                    <div class="leaf__share__button button button--light-ghost button--circle float-center">${download_icon}</div>
                    <span class="button-label">Download</span>
                </a>
            `);
            $('#inline-download-button').click(getVideo);
        }

        $('body').on('touchstart mousedown', 'a.card-item__link', (event) => {
            if (isAnon() && event.which !== 3) {
                location.href = $(event.currentTarget).attr('href');
            }
        });

        $('body').on('touchstart mousedown', 'button[data-video-json]', (event) => {
            if (isAnon() && event.which !== 3) {
                location.href = $(event.currentTarget).data('video-json').replace(/\.json/, '.html');
            }
        });
    });
})();