Thursday, May 9, 2013

Rai.tv native video player and direct links


// ==UserScript==
// @name        Rai.tv native video player and direct links
// @namespace   http://andrealazzarotto.com
// @description	This script allows you to watch and download videos on Rai.tv.
// @include     http://www*.rai.*/dl/RaiTV/programmi/media/*
// @include     http://www*.rai.*/dl/RaiTV/tematiche/*
// @include     http://www*.rai.*/dl/*PublishingBlock-*
// @include     http://www*.rai.*/dl/replaytv/replaytv.html*
// @include     http://*.rai.it/*
// @version     4.3
// @require     http://code.jquery.com/jquery-latest.min.js
// @grant       GM_xmlhttpRequest
// @updateURL	https://userscripts.org/scripts/source/89326.user.js
// @downloadURL https://userscripts.org/scripts/source/89326.user.js
// ==/UserScript==

placeHolder = function(url, kind) {
    PL = $("div.Player").get(0);
    if (!PL)
        PL = $("div#Player").get(0);
    if (!PL)
        PL = $("div#idPlayer").get(0);
    
	$("#direct-link").remove();
	$( PL ).after("<div id='direct-link' />");
	wi = $( PL ).width();
	w = wi*.6;
	m = wi*.19;
	// some styling
	$("#direct-link")
		.css('padding', '5px')
		.css('margin', '10px '+m+'px')
		.css('width', w+'px')
		.css('border', '1px solid #888')
		.css('text-align','center')
		.css('box-shadow', '0px 5px 15px 0px rgba(0, 0, 0, .7)')
		.css('background-color','#cfc')
		.css('float','left');
	// place the link
	$("#direct-link")
		.append("<a href='"+url+"'>" + kind + " Direct Link</a>");
	$("#direct-link a")
		.css('color','black')
		.css('font-size','1.2em');
    
    // fix the position of the link
    min = $( PL ).prev().height() + $( PL ).height() + parseInt($( PL ).css("margin-top"))
    	+ parseInt($( PL ).css("margin-bottom")) + $("#direct-link").height();
    if ($("#direct-link").parent().height() < min) {
        $("#direct-link").parent().height(min+30).css("position","relative");
        $("#direct-link").css("position","absolute").css("left",0)
        	.css("bottom", parseInt($("#direct-link").parent().css("padding-bottom"))*.4 );
    }
}

setUP = function(url, kind, selector) {
	placeHolder(url, kind);
	// place the video
    PL2 = $("div.Player div").get(0);
    if(!PL2)
        PL2 = $("div#Player div").get(0);
    if(!PL2)
        PL2 = $("div#idPlayer object").get(0);

    $(PL2).replaceWith("<embed width='100%'"
		+ "height='100%' src='"
		+ url + "' type='application/x-mplayer2' "
		+ "autoplay='true' />");
};

decide = function(videoURL, videoURL_MP4, estensioneVideo) {
    if (videoURL_MP4) { setUP(videoURL_MP4, "MP4"); }
	else if (videoURL) {
		if(estensioneVideo != 'WMV' || videoURL.indexOf("relinker") == -1) {
			if(videoURL.toLowerCase().indexOf("mms") == 0)
				setUP(videoURL, "MMS Stream");
			else
				setUP(videoURL, estensioneVideo);
		}
		else {// it's a relinker to an MMS stream
            GM_xmlhttpRequest({
                method: 'GET',
                url: videoURL,
                headers: {
                    'Accept': 'application/atom+xml,application/xml,text/xml'
                },
                onload: function(responseDetails) {
                    var r = responseDetails.responseText;
                    var doc = $.parseXML(r);
                    $xml = $( doc );
                    
                    var url = $xml.find("REF").attr("HREF");
                    url = url.replace("http://", "mms://");
                    
                    setUP(url, "MMS Stream");
                }
            });
		} // end relinker

	} // end if (videoURL)
}

$(document).ready(function(){

	if(unsafeWindow.videoURL || unsafeWindow.videoURL_MP4) {

		videoURL = $("meta[name=videourl]").attr("content");
        if(!videoURL)
            videoURL = unsafeWindow.videoURL;
		videoURL_MP4 = $("meta[name=videourl_mp4]").attr("content");
        if(!videoURL_MP4)
            videoURL_MP4 = unsafeWindow.videoURL_MP4;
		estensioneVideo = unsafeWindow.estensioneVideo;
		if(estensioneVideo)
			estensioneVideo = estensioneVideo.toUpperCase();
		else
			estensioneVideo = "Unknown";
        if(unsafeWindow.MediaItem.type == 'WMV')
            // avoid bug when estensioneVideo = CSM and MediaItem.type = WMV
            estensioneVideo = "WMV";
		
		decide(videoURL, videoURL_MP4, estensioneVideo);

	} // end Rai.tv "standard"
    
    else if($("script:contains('draw')").length) {
        var videoURL = $("script:contains('draw')").text().split("'")[1];
        if(videoURL.indexOf("relinker") > 0) {
            GM_xmlhttpRequest({
                method: 'GET',
                url: videoURL,
                headers: {
                    'Accept': 'application/atom+xml,application/xml,text/xml'
                },
                onload: function(responseDetails) {
                    var r = responseDetails.responseText;
                    var doc = $.parseXML(r);
                    $xml = $( doc );
                    
                    var url = $xml.find("REF").attr("HREF");
                    url = url.replace("http://", "mms://");
                    
                    setUP(url, "MMS Stream");
                }
            });
        }
    } // end pages like report.rai.it
	
	// ========================================
	
	else if(window.location.href.indexOf("PublishingBlock") != -1
           || window.location.href.indexOf("tematiche") != -1) {
	
		setInterval(function() {
			document.HprevId = document.Hid;
			document.Hid = $("div.Player").attr("data-id");
            
            // remove video list click events to allow opening of "real" pages if not on "tematiche"
            if(window.location.href.indexOf("tematiche") < 0)
                $(".listaVideo a").unbind("click");
		
			if(document.Hid && (document.Hid != document.HprevId)) {
				completeURL = "http://www.rai.tv/dl/RaiTV/programmi/media/"
					+ document.Hid + ".html";
				
				// get the original page content
				GM_xmlhttpRequest({
					method: 'GET',
					url: completeURL,
					onload: function(responseDetails) {
						var r = responseDetails.responseText;
						// kill script tags to avoid execution (and errors!)
						r = r.replace(new RegExp('script', 'g'), 'dummy');
						r = $('<div></div>').append(r);
						
						var data = $(r).find("div#silverlightControlHost dummy").text();
						
						// set the correct variables
						var videoURL = data.match(/videoURL = ["'](.*?)["']/)[1];
						var videoURL_MP4 = data.match(/videoURL_MP4 = ["'](.*?)["']/)[1];
						var estensioneVideo = data.match(/estensioneVideo = ["'](.*?)["']/)[1];
						
						decide(videoURL, videoURL_MP4, estensioneVideo);
					}
				});
			}
		}, 400);
	
	} // end Tematiche
	
	// ========================================
	
	else if(window.location.href.indexOf("replaytv") != -1) {
	
		unsafeWindow.isTVBrowser = true;
	
		setInterval(function() {
			document.HprevUrl = document.Hurl;
			document.Hurl = $("video").attr("src");
		
			if(document.Hurl && (document.Hurl != document.HprevUrl)) {
				// call the link placeHolder
				placeHolder(document.Hurl, "MP4");
				// place the video
				$("video").replaceWith("<embed width='100%' height='100%' src='"
					+ document.Hurl + "' type='application/x-mplayer2' "
					+ "autoplay='true' />");
			}
		}, 400);
	
	} // end Rai Replay

    // handle RTMP based flash objects on Rai.it
    i = 0;
    $("object").not("object object").each(function() {
        o = $(this);
        flashvars = o.find("param[name=flashvars]").attr("value");
        path = flashvars.replace(/.*percorso=/gi, "").replace(/&.*/gi, "").replace(/\?.*/gi, "");
        if(path.toLowerCase().indexOf("rtmp")!=-1) {
            host = (path.split("/"))[2];
            app = (path.replace(/\/mp4:.*/gi,'').split("/")).slice(3).join("/");
            split = path.split("mp4:");
            playPath = "mp4:" + split[1];
            name = playPath.replace(/mp4/gi,"").replace(/[^A-Za-z0-9]/gi," ").trim().replace(/\ /gi,"_") + ".mp4";
            tcUrl = split[0];
            player = "http://" + window.location.host + o.find("param[name=movie]").attr("value");
            referer = window.location.href;
            
            
            commandline = "rtmpdump -n " + host + " -t " + tcUrl + " -y " + playPath + " -a " + app
                + " -p " + referer + " --swfVfy " + player + " -o " + name;
            
            // write the command line under the video
            o.after("<div class='rtmp-command-line' id='rtmp-command-line-" + i + "' />");
            $("#rtmp-command-line-"+i).append("<div>RTMPdump command line</div><pre>" + commandline + "</pre>");
            i++;
        }

    });
    $(".rtmp-command-line div").css("font-weight","bold").css('text-align','center');
    $(".rtmp-command-line")
    	.css('padding', '5px')
		.css('margin', '10px auto')
		.css('width', '95%')
		.css('border', '1px solid #888')
		.css('box-shadow', '0px 5px 15px 0px rgba(0, 0, 0, .7)')
		.css('background-color','#ddd');
    $(".rtmp-command-line pre")
    	.css("font-size",".85em")
    	.css("white-space","pre-wrap")
    	.css("word-break","break-all");
    // end code for flash videos

}); // end document.ready

0 comments:

Post a Comment