Skip to content

Instantly share code, notes, and snippets.

@oberhamsi
Created June 3, 2015 07:00
Show Gist options
  • Save oberhamsi/01def3872f8b955b15b1 to your computer and use it in GitHub Desktop.
Save oberhamsi/01def3872f8b955b15b1 to your computer and use it in GitHub Desktop.
video downloader
// Video Infos holen
var jsonString = $(".player_viewport").find("DIV:nth-child(4)").attr("data-jsb");
var videoObj = $.parseJSON(jsonString);
if (videoObj.playlist.videos.length == 1) {
var sources = videoObj.selected_video.sources;
// Download-Menü erstellen
createDownloadMenu(".mod_player > .service_footer", "Sendung herunterladen");
// Video-Quelle mit bester Auflösung suchen
$.each(sources, function(index, source) {
if (source.delivery == "progressive" &&
source.protocol == "http"
) {
source.name = videoObj.selected_video.title;
addSourceToDownloadMenu(".mod_player > .service_footer", source);
}
});
} else if (videoObj.playlist.videos.length > 1) {
var playListItems = $(".base_list_item");
$.each(playListItems, function(index, item) {
var jsonString = $(item).find(".service_link_play").attr("data-jsb");
var itemObj = $.parseJSON(jsonString);
var sources = itemObj.video.sources;
createDownloadMenu($(item).find(".service_footer"), "herunterladen");
$.each(sources, function(index, source) {
if (source.delivery == "progressive" &&
source.protocol == "http"
) {
source.name = itemObj.video.title;
addSourceToDownloadMenu($(item).find(".service_footer"), source);
}
});
});
}
// DropdownMenü schließen
$(document).click(function(event) {
if ($(event.target).closest(".downloadBox").size() == 0) {
$(".downloadSources").slideUp();
}
});
function createDownloadMenu(target, title) {
// Vorhandenen Service-Button laden und kopieren
var serviceLinkTemplate = $(target).find("A").first();
var downloadServiceLink = $(serviceLinkTemplate).clone();
// und danach anpassen und einfügen
$(downloadServiceLink).attr("href", "#");
$(downloadServiceLink).find(".icon_wrapper").contents().last()[0].textContent = title;
$(downloadServiceLink).addClass("service_link_download");
$(downloadServiceLink).removeClass("service_link_send");
$(downloadServiceLink).removeClass("service_link_play");
$(target).append("<span class='downloadBox'></span>");
$(target).find(".downloadBox").prepend(downloadServiceLink);
$(target).find(".downloadBox").append("<ul class='downloadSources' style='display: none;'></ul>");
$(target).find(".service_link_download").on("click", function(event) {
event.preventDefault();
$(target).find(".downloadSources").slideToggle();
});
}
function addSourceToDownloadMenu(target, source) {
// Anpassen und einfügen
var sourceElement = $("<li><a href=''></a></li>");
$(sourceElement).find("A").attr("href", source.src);
var sourceName = source.quality_string.toLowerCase().replace(/\b[a-z]/g, function(letter) {
return letter.toUpperCase();
});
var fileExtension = getFileExtension(source.src);
$(sourceElement).find("A").append('Qualität: ' + sourceName + " (" + fileExtension + ")");
$(sourceElement).find("A").attr("download", source.name);
$(target).find(".downloadSources").prepend(sourceElement);
}
function getFileExtension(filename) {
return (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename) : undefined;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment