RSI direct link

This script gives you the direct links while watching a video on rsi.ch.

As of 2015-09-24. See the latest version.

// ==UserScript==
// @name        RSI direct link
// @namespace   http://andrealazzarotto.com/
// @include     http://rsi.ch/*
// @include     http://*.rsi.ch/*
// @version     3.1
// @description This script gives you the direct links while watching a video on rsi.ch.
// @copyright   2013+, Andrea Lazzarotto - GPLv3 License
// @require     http://code.jquery.com/jquery-latest.min.js
// @grant       GM_xmlhttpRequest
// @license     GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// ==/UserScript==

var get_title = function() {
	var title = $('h1').text().trim() || 'output';
	return title.replace(/\W+/g, '_');
}

var appendURL = function(element, id, url) {
	var software = "avconv";
	
	// clean corner cases
	$('#' + id + ', #' + id + '-show').remove();

	// position hidden box
	$('body').append('<div id="' + id + '"></div>');
	$('#' + id).css({
		'position': 'fixed',
		'top': '30%',
		'left': '20%',
		'right': '20%',
		'z-index': '9999',
	}).append('<p>Per registrare il video, usa <code>' + software + '</code> con la seguente riga di comando:</p>' + 
			  '<pre>' + software + ' -i "' + url + '" -codec copy -qscale 0 ' +
			  get_title() + '.mp4</pre>' + '<p>Per istruzioni sul software, ' +
			  '<a href="http://lazza.me/1PLyi12">clicca qui</a>. ' +
			  'Alternativamente, puoi provare a registrare il flusso M3U8 con VLC.</p>' +
			  '<p style="margin: 0"><a id="' + id + '-close">[Chiudi finestra]</a></p>');
	
	$('#' + id).hide();
	
	// set up small button
	element.after('<div id="' + id + '-show"><a>Salva</a></div>');
	
	// bind actions
	$('#' + id + '-close').click(function(){
		$('#' + id).hide();
	});
	$('#' + id + '-show a').click(function(){
		$('#' + id).show();
	});
	
	// finish style
	// see also: http://www.colourlovers.com/palette/725298/Strawberry_Mousse
	$('#' + id + ' pre, #' + id + ' code').css({
		'white-space': 'normal',
		'word-break': 'break-word',
		'margin': '.75em 0',
		'padding': '.75em',
		'background-color': '#6B5344',
		'color': '#F8ECC9',
		'border': 0
	});
	$('#' + id + ' code').css({
		'padding': '.5em',
		'line-height': '1em'
	});
	$('#' + id + ' p').css('color', 'black');
	$('#' + id + ' a, #' + id + '-show a').css({
		'color': '#6B5344',
		'cursor': 'pointer',
		'font-weight': 'bold'
	});
	$('#' + id + ' p:last-child a').parent().css('text-align', 'right');
	
	$('#' + id + ', #' + id + '-show').css({
		'margin': '.75em .75em 1.25em',
		'padding': '.75em',
		'border': '1px solid #F1BBBA',
		'box-shadow': '0px 20px 150px 0px rgba(0, 0, 0, .95)',
		'font-family': 'sans-serif',
		'box-sizing': 'border-box',
		'background-color': '#F8ECC9',
		'width': 'auto',
	});
	$('#' + id + '-show').css({
		'text-align': 'center',
		'padding': '.5em',
		'box-shadow': '0px 5px 15px 0px rgba(0, 0, 0, .7)'
	});
}

var m3u8_qualities = function(contents) {
	var lines = contents.split('\n');
	var streams = {};
	for (var i = 0; i < lines.length - 1; i++) {
		var h = lines[i];
		var u = lines[i+1];
		if(h.indexOf('#EXT-X-STREAM-INF') == 0 && u.indexOf('m3u8') > 0) {
			var q = parseInt(h.split('RESOLUTION=')[1].split('x')[0]);
			if(h.indexOf('audio-') > 0)
				q = q*100 + parseInt(lines[i].split('audio-')[1].split('"')[0]);
			if (u.indexOf('://') > 0)
				streams[q] = u;
			else
				streams[q] = m3u8_url.object.split('/').slice(0,-1).join('/') + '/' + u;
			i++;
		}
	}
	return streams;
}

var get_biggest = function(dict) {
	var s = 0;
	var o = null;
	for(var key in dict) {
		key = parseInt(key) || 0;
		if (key > s) {
			s = key;
			o = dict[key];
		}
	}
	return {'size': s, 'object': o};
}

var layerURL = "http://il.srgssr.ch/integrationlayer/1.0/ue/rsi/video/play/";
var isPlay = location.href.indexOf('/play/tv') > 0;

var setup_frames = function() {
	$("iframe[src*='video:']:not(*.done)").each(function (){
		var frame = $(this);
		var id = parseInt(frame.attr('src').split('video:')[1].split('&')[0]);
		if (!!id) {
			GM_xmlhttpRequest({
				method: 'GET',
				url: layerURL + id.toString() + '.xml',
				onload: function(responseDetails) {
					var r = responseDetails.responseText;
					var doc = $.parseXML(r);
					var $xml = $(doc);
					
					var manifestURL = $($xml.find('Playlist[protocol="HTTP-HLS"] > url').get(0)).text();
					
					GM_xmlhttpRequest({
						method: 'GET',
						url: manifestURL,
						onload: function(responseDetails) {
							var r = responseDetails.responseText;
							var biggest = get_biggest(m3u8_qualities(r));
							
							var selector = 'link-' + id;
							if(isPlay)
								appendURL(frame, selector, biggest.object);
							else
								appendURL(frame.parent(), selector, biggest.object);
							frame.addClass('done');
						}
					});
				}
			});
		}
	}); // end each
}

$(document).ready(function(){
	setInterval(setup_frames, 500);
});