ProxyFiend / TumblrDown

// ==UserScript==
// @name         TumblrDown
// @namespace    ProxyFiend.TumblrDown
// @author       ProxyFiend
// @prefix       TD
// @copyright    2018, ProxyFiend (https://twitter.com/ProxyFiend)
// @license      MIT
// @version      1.1.5
// @description  Allows you to download posts of nearly any type.
// @include      /^.*:\/\/[^\/]*\/post\/(\d{12}).*/
// @updateURL    https://openuserjs.org/meta/ProxyFiend/TumblrDown.meta.js
// @downloadURL  https://openuserjs.org/src/scripts/ProxyFiend/TumblrDown.user.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js
// @require      https://openuserjs.org/src/libs/ProxyFiend/ProxyFiendLib.js
// @require      https://gist.githubusercontent.com/FlandreDaisuki/8970080786187e0333154b54869d079e/raw/abc01526232e05368bf8c75165d8d14606674f41/GM_download_polyfill.js
// @grant        GM_download
// @grant        GM_xmlhttpRequest
// @noframes
// ==/UserScript==

// ==OpenUserJS==
// @author ProxyFiend
// ==/OpenUserJS==

(function () {
    'use strict';

    const regex = /^.*:\/\/[^\/]*\/post\/(\d{12}).*/g;
    let m;

    m = regex.exec(window.location);

    $(window).bind('keydown', function (event) {
        if (event.ctrlKey || event.metaKey) {
            switch (String.fromCharCode(event.which).toLowerCase()) {
                case 's':
                    event.preventDefault();
                    GM_debug("Trying to download.");
                    downloadPost();
                    break;
            }
        }
    });

    var downloadPost = function () {
        GM_getJSON({
            url: "https://api.tumblr.com/v2/blog/" + window.location.host + "/posts",
            data: {
                api_key: "TCOBlLpiC1YV0J3pwZbihgm1I3bxD4tSdp5J5gpkxwzNClVuI4",
                id: m[1]
            },
            onload: function (json, raw) {
                json.response.posts.forEach(function (post) {
                    post.trail.forEach(function (trail) {
                        if (trail.is_root_item === true) {
                            post.op = trail.blog.name;
                            post.origin = trail;
                        }
                    });
                    switch (post.type) {
                        case "photo":
                            downloadPostPhotos(post);
                            break;
                        case "video":
                            downloadPostVideo(post);
                            break;
                    }
                });
            }
        });
    };

    var downloadPostPhotos = function (post) {
        post.photos.forEach(function (photo) {
            GM_debug("Downloading \"" + photo.original_size.url + "\" as \"" + post.op + "-" + post.id + "-" + post.photos.indexOf(photo) + "." + (photo.original_size.url.substring(photo.original_size.url.lastIndexOf(".") + 1)) + "\"");
            GM_download({
                url: photo.original_size.url,
                name: post.op + "-" + post.id + "-" + post.photos.indexOf(photo) + "." + (photo.original_size.url.substring(photo.original_size.url.lastIndexOf(".") + 1))
            });
        });
    };

    var downloadPostVideo = function (post) {
        GM_debug("Downloading \"" + post.video_url + "\" as \"" + post.op + "-" + post.id + "." + (post.video_url.substring(post.video_url.lastIndexOf(".") + 1)) + "\"");
        GM_download({
            url: post.video_url,
            name: post.op + "-" + post.id + "." + (post.video_url.substring(post.video_url.lastIndexOf(".") + 1))
        });
    };
})();