Azev / rarbgPlus

// ==UserScript==
// @name		rarbgPlus
// @namespace	Azev
// @homepage    https://openuserjs.org/scripts/Azev/rarbgPlus
// @description	Please visit: https://openuserjs.org/scripts/Azev/rarbgPlus
// @updateURL	https://openuserjs.org/install/Azev/rarbgPlus.user.js
// @downloadURL	https://openuserjs.org/install/Azev/rarbgPlus.user.js
// @author		Azev (leandro.azevedo@gmail.com)
// @version		5.1.1
// @grant		none
// @icon		http://www.rarbg.to/favicon.ico
// @include     http*://*rarbg.com/*
// @include     /.*rarbg\.*/
// ==/UserScript==

/*	Changelog

    5.1.1
	
	- Fixed include to match regex

	5.1
	- Display count of results omitted/blacklisted (if any)
	- Improved rxCleanNames
	- changed cover link from http://torcache.net to http://itorrents.org
		torcache not accepting different domain direct download of torrents
	
	
	
							check if thumbnail / torrent file exists : http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp
							404
	5.0.1
	- Fixed torrent date appearance
	
	5.0
	- New release from rargb Fixer (discontinued)

*/

/* OPTIONS */
	var THUMB_SIZE	 		= 200; // thumbnail size (width)
	var HIDE_RECOMMENDED	= true; // hide 'recommended'
	//var rxBlacklist			= /suicidegirls|evil|ztod|paintoy|DoctorAdventures|TeensLikeItBig|BigTitsAtSchool|pervsonpatrol|ThaiGirlsWild|letstryanal|HandsOnHarcore|doghousedigital|handsonhardcore|RestrainedElegance|demon/gi; // comment this line if you don't want filters (filter refers to title)

/* CODE */

var css, cleanName, i, j, t;
var catMusic = false;
var rxHash = /([a-f0-9]{40})/i;
var rxName = /title="(.*?)"/i;
var rxId = /href="\/torrent\/(.*?)"/i;
var rxTags = /DarkSlateGray">(.*?)<\/span>/i;
var rxImdb = /imdb=(.*?)"/i;
var rxDate = /(\d{4}-\d{2}-\d{2}).*?/i;
var rxSize = /([0-9\.]+ [MG]B)/i;
var rxSeeds = />(\d)<\/font>/i;
var blacklisted = 0;
if (typeof rxBlacklist == 'undefined'){
	var rxBlacklist = new RegExp(/^$/gi);
}

var rxCleanNames = /multi\d|\.\w+\.(org|net|com)|xxx|mvgroup.*/gi;

var rxTypes = /imageset|(x|h)\.?264|avc|(480|540|720|1080)p|korsub|(sd)\.|\W(true)?hd(tv)?([\d\.]+)?|(dvd|bd|hd|br|web)rip|(web|dvd)scr|bluray|mkv|mov|flv|wmv|web-dl|aac(\.?\d\.\d)?|dts(-hd)?|truehd|ac3(\.?\d\.\d)?|dd(\.?\d\.\d)|mp(3|4)|dv3|flac|xvid|divx\d?|(\[)?\d+\s?(kbps)(\])?|ps\d|pc|xbox\d|rip+|MA\.5\.1|multisub(s)?|\[techtools\]/gi;

//altTorrentLink = "http://torcache.net/torrent/"
//altTorrentLink = "http://thetorrent.org/"
altTorrentLink = "http://itorrents.org/torrent/"

var list = document.getElementsByClassName('lista2');
var data = [];
var results = '<div id="results">';
var icon_download = 'https://dyncdn.me/static/20/img/16x16/download.png';
var icon_imdb = 'https://dyncdn.me/static/20/images/imdb_thumb.gif';
var icon_youtube = 'https://dyncdn.me/static/20/images/youtube_thumb.gif';
//var icon_torcache = 'https://dyncdn.me/static/20/green_arrow_small.png';
//var icon_magnet = 'https://dyncdn.me/static/20/img/magnet.gif';

function rxGet(rx, str){ // return regEx match or ''
	var ret = rx.exec(str);
	ret = ( ret !== null ) ? ret[1] : '';
	return(ret);	
}

/*****************************************************************/

if (document.getElementsByClassName('lista2t').length > 0){ // check if there are results

	category = rxGet(/category=([;\d]+)/, decodeURIComponent(window.location.href)).split(';');
	if ( (category.indexOf('23') !== -1) || (category.indexOf('25') !== -1) )
		catMusic = true;
	
	// build data[]
	for (i=0; i<list.length; i++){
		if ( rxGet(rxName, list[i].innerHTML) !== ''){ // skip 'direct download' (ad)
			if ( !rxGet(rxName, list[i].innerHTML).match(rxBlacklist) ){ // skip blacklisted items
				data.push({
					"id" : rxGet(rxId, list[i].innerHTML)
					, "hash" : rxGet(rxHash, list[i].innerHTML)
					, "name" : rxGet(rxName, list[i].innerHTML)
					, "tags" : rxGet(rxTags, list[i].innerHTML).replace(/imdb.*/gi,'').split(/, | i/gi)
					, "imdb" : rxGet(rxImdb, list[i].innerHTML)
					, "date" : rxGet(rxDate, list[i].innerHTML)
					, "seeds" : rxGet(rxSeeds, list[i].innerHTML)
					, "size" : rxGet(rxSize, list[i].innerHTML)
				});
			} else {
				blacklisted++;
			}
		}
	}

	// process data[]
	for (i=0; i<data.length; i++){
		
		results += '<div class="result">';
		results += '<span class="t_title">$name</span>';
		results += '<a href="$altTorrentLink"><img class="cover" width="' + THUMB_SIZE + '" src="$cover"></a><br>';
		results += '<div id="labels">';
		results += '<span id="size">$size</span><span id="rDate">$date</span>$types$tags$imdb$rarbg</div>';
		results += '</div>';

		results = results.replace('$cover',	'http://dyncdn.me/posters2/' + data[i].hash.charAt(0) + '/' + data[i].hash + '.jpg');
		
		cleanName = data[i].name;
		
		if ( data[i].name.lastIndexOf('-') !== -1 ){ // remove tailing '-reLeASer'
			if ( data[i].name.charAt( data[i].name.lastIndexOf('-')-1) !== ' '){ // skip if preceded by space. eg: ' VA - '
				cleanName = data[i].name.substr(0, data[i].name.lastIndexOf('-')) // remove tailing '-reLeASer'
			}			
		}
		
		cleanName = cleanName
			.replace(rxCleanNames, ' ')
			.replace(rxTypes, ' ')
			.replace(/\.|\s{2,}|com_|_/gi, ' ')
			.replace(/(\d{4})/gi, '($1)') // year -> (year)
			.replace(/\[\s+\]|-\s+$|\[\s+$/gi, '') // clear artifacts
			.replace(/\[\(|\(\(/gi, '(')
			.replace(/\)\]|\)\)/gi, ')')
			.trim();
		results = results.replace('$name', '<a href="https://www.rarbg.to/torrent/'+ data[i].id + '">' + cleanName + '</a>'); // name link
		results = results.replace('$altTorrentLink', altTorrentLink + data[i].hash.toUpperCase() + '.torrent');
		results = results.replace('$size', data[i].size);	// size
		results = results.replace('$date', data[i].date);	// date
				
		// types
		t = '';
		types = data[i].name.match(rxTypes);
		if (types){
			for (j=0; j<types.length ; j++){
				t +=  '<span class="type">' + types[j].replace(/\[|\]/gi, '') + '</span>';
			}
		}
		results = results.replace( '$types' , t);
		
		// tags
		t = '';
		if (data[i].tags[0] !== ''){
			for (j=0; j<data[i].tags.length ; j++){
				t +=  '<span class="tag">' + data[i].tags[j] + '</span>';
			}
		}
		results = results.replace( '$tags' , t);
		
		// imdb & youtube
		t = '';
		if (data[i].imdb !== '') {
			t = '<br><a target="_blank" href="http://imdb.com/title/'+data[i].imdb+'"><img src="'+icon_imdb+'"></a>';
			t += '<a target="_blank" href="https://www.youtube.com/results?search_query='+cleanName+' trailer"><img src="'+icon_youtube+'"></a>';
		}
		results = results.replace('$imdb', t);
		
		// rarbg torrent link
		t = '';
		t = '<a href="https://www.rarbg.to/download.php?id='+data[i].id+'&f='+data[i].name+'.torrent"><img src="'+icon_download+'"></a>';
		results = results.replace('$rarbg', t);
		
	}

	
	// replace results
	results += '</div>';
	if (blacklisted>0){
		results += '<span id="blacklisted"> Omitted results (blacklisted): <b>' + blacklisted + '</b></span>';
	}
	document.getElementsByClassName('lista2t')[0].outerHTML = results;

	// css formating
	css = '#results{text-align: center; padding: 10px 0 0 10px}';
	css += '.result{background-color: #D3DDE7; display: inline-block; width: '+ THUMB_SIZE +'px; padding: 10px; margin: 0 10px 10px 0; border: 1px solid #9FAABC; border-radius: 3px; overflow: hidden; vertical-align: top}';
	css += '.t_title a {display: block; padding: 0 4px 5px 0; text-align: center; font-weight: 600; font-size: 1.1em; text-decoration: none}'; // torrent tile
	css += '#t_title a:hover {color: #000}';
	css += '.result span{display: block; vertical-align: middle;}';
	thumbHeight = catMusic ? THUMB_SIZE : THUMB_SIZE*3/2;
	css += 'img.cover {width: '+ THUMB_SIZE +'px; height: '+thumbHeight+'px; vertical-align: middle; object-fit: cover; border: 1px solid #777; margin-bottom: 5px;}';

	css += '#labels span, #size {border-radius: 3px; padding: 2px; margin: 2px; display: inline-block; cursor: default}';
	css += '#rDate {background-color: #6C8BD2; color: #fff}';
	css += '#size {background-color: #F8C85A; color: #000}';
	css += '.type {background-color: #30519E; color: #fff}';
	css += '.tag {background-color: #9EBD55; color: #000}';
	css += '#labels a {border-radius: 3px; margin: 2px; vertical-align: middle; display: inline-block; overflow: hidden}';
	css += '#blacklisted {display:block; text-align: center; color: #A91818}';
	
	// search box
	//css += '#searchTorrent{ height: 30px; overflow: hidden; transition: height .25s} #searchTorrent:hover{ height: 180px}';


	// apply css
	var style = document.createElement('style');
	if (style.styleSheet)
		style.styleSheet.cssText = css;
	else
		style.appendChild(document.createTextNode(css));
	document.getElementsByTagName('head')[0].appendChild(style);
}

// hide iframes
elms = document.getElementsByTagName("iframe");
for (i=0; i < elms.length; i++) {
	elms[i].style.display = 'none';
	elms[i].src = 'about:blank';
}

// hide 'recommended' covers
if (HIDE_RECOMMENDED) {
	if (  document.documentElement.innerHTML.indexOf('Recommended torrents') !=-1 ){
		elms = document.getElementsByClassName("lista");
		for (i=5; i <= 12; i++) {
				elms[i].style.display = 'none';
		}
	}
	elms = document.getElementsByTagName("b");
	for (i=0; i < elms.length; i++) {
		if ( elms[i].innerHTML.indexOf('Recommended torrents :') !=-1 ) elms[i].style.display = 'none';
	}
}

// hide bottom ad
elms = document.getElementsByTagName('a');
for (i=0; i < elms.length; i++) {
	if (elms[i].getAttribute('href') !== null){		
		if ( elms[i].getAttribute('href').indexOf('buy-vpn') !== -1 ){
			elms[i].style.display = 'none';
			elms[i].href='#';
		}
	}
}


// hide direct download links (ad)
elms = document.getElementsByTagName("a");
for (i=0; i < elms.length; i++) {
	if ( elms[i].outerHTML.indexOf('direct') !=-1 ){
		elms[i].parentNode.parentNode.style.display = 'none';
		elms[i].href='#';
	}
}

// add altTorrentLink
var anchor, hash;
elms = document.getElementsByTagName('a');
for (i=0; i < elms.length; i++) {
	if ( elms[i].getAttribute('href') !== null){
		if ( elms[i].getAttribute('href').indexOf('magnet:') !== -1 ){
			anchor = document.createElement('A');
			hash = (/:([0-9a-fA-F]{40})/.exec( elms[i].getAttribute('href') )[1].toUpperCase() );
			url = altTorrentLink + hash + '.torrent';
			anchor.setAttribute('href', url);
			anchor.style.display = 'block';
			anchor.style.padding = '10px';
			anchor.style.color = '#3c0';
			anchor.innerHTML = 'Download torrent from ' + altTorrentLink.match(/\/\/([\w\d\.]+)\//)[1];
			elms[i].parentNode.appendChild(anchor);
		}
	}
}