RSI direct link

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

La data de 12-04-2016. Vezi ultima versiune.

// ==UserScript==
// @name        RSI direct link
// @namespace   http://andrealazzarotto.com/
// @include     http://rsi.ch/*
// @include     http://*.rsi.ch/*
// @version     4.0.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
// @connect     il.srgssr.ch
// @connect     codww-vh.akamaihd.net
// @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 appendQualities = function(element, id, qualities) {
	element.parent().find('.direct-links').remove();
	element.after('<div class="direct-links" id="' + id + '"></div>');
	for(var key in qualities) {
		var width = key;
		var height = key*9/16;
		var chunks = qualities[key].split(',');
		var last = chunks[chunks.length-1];
		var pos = parseInt(last.split('index_')[1]);
		
		var url = 'http://mediaww.rsi.ch/' + chunks[0].split('net/i/')[1] + chunks[pos+1] + '.mp4';
		console.log(url);
		
		$('#' + id)
			.prepend('<span class="sep"> | </span>')
			.prepend('<a href="' + url + '">' + width + 'x' + height + '</a>');
	}
	$('#' + id + ' span:last-child').remove();
	
	$('#' + id + ' a').css('font-weight', 'bold');
	$('#' + id).css({
		'margin': '.75em 0',
		'box-sizing': 'border-box',
		'width': 'auto',
		'text-align': 'center'
	});
};

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:']").each(function (){
		var frame = $(this);
		var id = parseInt(frame.attr('src').split('video:')[1].split('&')[0]);
		if (!!id && (frame.data('done') !== id)) {
			// set this immediately to avoid multiple requests
			frame.data('done', 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();
					manifestURL = manifestURL.replace('codch-vh', 'codww-vh');
					
					GM_xmlhttpRequest({
						method: 'GET',
						url: manifestURL,
						onload: function(responseDetails) {
							var r = responseDetails.responseText;
							var qualities = m3u8_qualities(r);
							
							var selector = 'link-' + id;
							appendQualities(frame.parent(), selector, qualities);
						}
					});
				}
			});
		}
	}); // end each
};

$(document).ready(function(){
	setup_frames();
	
	$('a[href^="#gallery"]').click(function(){
		setTimeout(setup_frames, 500);
	});
});