Thumb

Metacritic / RateYourMusic ratings for Waffles.FM

By AlmostKilledMe Last update Jul 2, 2009 — Installed 177 times. Daily Installs: 0, 0, 0, 0, 1, 0, 2, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 2, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0

There are 9 previous versions of this script.

// ==UserScript==
// @name           Metacritic - Waffles.FM
// @namespace     http://userscripts.org/scripts/show/51479
// @description    Add Metacritic ratings to albums on Waffles.FM
// @author         shrodes, based on code from Sean Flanigan [Metacritic - Good Old Games], Brice McIver [Metacritic - Google Movies] and Indieana [OiNKPlus]
// @include	https://*waffles.fm/details.php?id=*
// @include	http://*waffles.fm/details.php?id=*
// @require     http://updater.usotools.co.cc/51479.js
//
// @date          19/06/2009
// @version       1.10
// @since         14/06/2009
// ==/UserScript==
// ------------------------------------------------------------------------
// Copyright (c) 2009, shrodes, shrodes@gmail.com
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//			- TODO add: check artist name against metacritic result to ensure correct link is received
//
// CHANGELOG
//
// 1.10 - 19/06/2009 - Added RateYourMusic ratings. Added command to switch between layout modes.
// 1.02 - 15/06/2009 - Now checks to see if result found matches artist to ensure correct rating. Added different display method for under download link.
// 1.01 - 15/06/2009 - Refactor, added in alternate positioning so the rating can display in the torrent heading (default), or underneath the download link.
// 1.00 - 14/06/2009 - Initial version based on code from sources above. Basic checking and parsing Metacritic rating.


(function () {
	var ratingProvider = GM_getValue("ratingProvider");
	if(ratingProvider == null) {
		GM_setValue("ratingProvider","RateYourMusic");
		ratingProvider = "RateYourMusic";
	}
	GM_registerMenuCommand('MetaWaffles: Switch ratings provider (currently ' + ratingProvider + ')', changeProvider);
	GM_registerMenuCommand('MetaWaffles: Switch layout', changeLayout);
   // Set this to 0 for next to torrent title, or 1 for beneath download link
   var ratingLocation = GM_getValue("ratingLocation");
   if (ratingLocation == null) {
		GM_setValue("ratingLocation",0);
   }
   var CSS_CODE =		".MetaWaffles { position:relative; max-width:700px; }\
		.explore  { text-decoration:none; }\
		.leftinfo {clear:both;}\
		.floatright { border: solid #000000 0px;float:right;margin:0pt 0pt 10px 10px;padding:2px;text-align: left;}\
		.floatleft { border: solid #000000 0px;float:left;text-align: left; width:140px;  }\
		.floatleft-small { border: solid #000000 0px;float:left;text-align: left;   }\
		.floatmiddle { border: solid #000000 0px;text-align: left; margin-left:140px;}\
		.green { font-weight: bold; color: #000000; background-color: #33CC00; padding-right: 3px; padding-left: 3px; letter-spacing: 0px; }\
		.yellow { font-weight: bold; color: #000000; background-color: #FFFF00; padding-right: 3px; padding-left: 3px; letter-spacing: 0px; }\
		.red { font-weight: bold; color: #FFFFFF; background-color: #FF0000; padding-right: 3px; padding-left: 3px; letter-spacing: 0px; }\
		.noscore{ font-weight: bold; color: #000000; background-color: #808E9B; padding-right: 3px; padding-left: 3px; letter-spacing: 0px; }\
		.rym{ font-weight: bold; color: #000000; background-color: #33CC00; padding-right: 3px; padding-left: 3px; letter-spacing: 0px; }\ ";	
	var new_row;
	var centertable;
	var album;
	var artist;
	var headCode = "";
		   
	// -- [Main] --------------------------------------------------------------------------------------
	function main() {
		album = getAlbum();
		artist = getArtist();
		if(ratingProvider == "MetaCritic") {
			getMetaRating(artist,album);
		}
		else if(ratingProvider == "RateYourMusic") {
			getRYMRating(artist,album);
		}
		placeFrame();
	}
	
	function changeLayout() {
		if (GM_getValue("ratingLocation") == 0) {
			GM_setValue("ratingLocation", 1);
			location.reload(true)
		}
		else {
			GM_setValue("ratingLocation", 0);
			location.reload(true)
		}
	}
	
	function changeProvider() {
		if (GM_getValue("ratingProvider") == "RateYourMusic") {
			GM_setValue("ratingProvider", "MetaCritic");
			location.reload(true)
		}
		else {
			GM_setValue("ratingProvider", "RateYourMusic");
			location.reload(true)
		}
	}
	
	function getTable() {
		var tds = document.getElementsByTagName("td");
		for (var i = 0; i < tds.length; i++){
			if (tds[i].className == "rowhead") {
				centertable =  tds[i].parentNode.parentNode; 
				break;
			}
		}
	
		new_row = centertable.firstChild.nextSibling.cloneNode(true);
		
		if (new_row.firstChild.hasChildNodes()) {
				new_row.firstChild.innerHTML = '';
				new_row.lastChild.innerHTML = '';
		}
		
		if (ratingLocation == 1) {
			new_row.firstChild.appendChild(document.createTextNode(' ' + 'MetaWaffles'));
			new_row.firstChild.nextSibling.innerHTML = '<div id=\"MetaWaffles\"></div>';
			var parent = centertable.firstChild.nextSibling;
			centertable.insertBefore(new_row,parent);
		}
		
		else {
			var h1s = document.getElementsByTagName("h1");
			if (h1s.length > 0) {
				var torrentHeading = h1s[0]
			}
			torrentHeading.innerHTML = torrentHeading.innerHTML + ' ' + headCode;
		}
	}
	
	function placeFrame() {
		addGlobalStyle(CSS_CODE);
		getTable();
	}
	
	function addGlobalStyle(css) {
		var head, style;
		head = document.getElementsByTagName('head')[0];
		if (!head) 
			return;
		style = document.createElement('style');
		style.type = 'text/css';
		style.innerHTML = css;
		head.appendChild(style);
	}
	
	function getAlbum() {
		var h1s = document.getElementsByTagName("h1");
		if (h1s.length > 0) {
			var str = h1s[0].innerHTML;
			str = str.replace(/\\[[^\\]]*\\]/g, '');
			if (str.split(' - ').length > 1) {
				return str.split(' - ')[1].replace(/ {.+?\}+| \(.+?\)+| \[.+\]?.+| \[.+\]?.+/g,"");
			}
		}
		return '';
	}
	
	function getArtist() {
		var h1s = document.getElementsByTagName("h1");
		if (h1s.length > 0) {
			var str = h1s[0].innerHTML;
			str = str.replace(/\\[[^\\]]*\\]/g, '');
			if (str.split(' - ').length > 1) {
				return str.split(' - ')[0].replace(/^\\s+|\\s+$/g,"");
			}
		}
		return '';
	}

	function getMetaRating(artist,album) {
		if (!album) {
			noAlbumParse();
		}
		else if (!artist) {
			noArtistParse();
		}
		else {
			var src = 'http://www.metacritic.com/search/process?ty=2&tfs=album_all&sb=0&ts=' + encodeURIComponent(album);
			
			if (typeof(GM_xmlhttpRequest) === 'function') {
				GM_xmlhttpRequest({
					method: 'GET',
					url: src,
					headers: {
						'User-agent': 'Mozilla/5.0 (compatible) Greasemonkey'
					},
					onload: function (response) {
						if (ratingLocation == 1) {
							var theLink = response.responseText.match(/ <strong>Music:<\/strong> <a href="(.+)">/i);
						}
						else {
							var theLink = response.responseText.match(/ <strong>Music:<\/strong> <a href="(.+?)">/i);
						}
						var theArtist = response.responseText.match(/ <p>1+.+by <strong>(.+)<\/strong>/i);
						var theRating = response.responseText.match(/ <span class="(green|yellow|red|noscore)">[x 0-9]{1,3}<\/span>/i);

						if (theRating !== null)
						{
							if(theArtist.toString().match(artist) !== null) {
								var newElement = document.createElement("span");
								newElement.innerHTML = "<a href=\"" + theLink[1] + "\">" + theRating[0] + "</a>";
								headCode = newElement.innerHTML;
								new_row.firstChild.nextSibling.appendChild(newElement);
								if (ratingLocation == 0) {
									placeFrame();
								}
							}
							else {
								noRatingFound();
							}
						}
						else if (theRating == null) {
							noRatingFound();
						}
					}
				});
			}
		}
	}
	
	function getRYMRating(artist,album) {
		if (!album) {
			noAlbumParse();
		}
		else if (!artist) {
			noArtistParse();
		}
		else {
			var src = 'http://rateyourmusic.com/search?searchterm=' + encodeURIComponent(album) + '&type=l';
			if (typeof(GM_xmlhttpRequest) === 'function') {
				var albumPage = "";
				GM_xmlhttpRequest({
					method: 'GET',
					url: src,
					headers: {
						'User-agent': 'Mozilla/5.0 (compatible) Greasemonkey'
					},
					onload: function (response) {
						var re = new RegExp('class="artist">'+artist+'<\/a> - <i><a title=".+" href="([^a-z]+)" class="searchpage">'+album,'i');
						var theLink = response.responseText.match(re);
						if(theLink != null) {
							albumPage = "http://rateyourmusic.com"+ entityDecode(theLink[1]);
							GM_xmlhttpRequest({
								method: 'GET',
								url: albumPage,
								headers: {
									'User-agent': 'Mozilla/5.0 (compatible) Greasemonkey'
								},
								onload: function (response) {
									var theRating = response.responseText.match(/<span style=".+">([0-9.]+)<\/span> <span class=".+">([0-9]+)/i);
									
									
									var newElement = document.createElement("span");
									if (theRating == null) {
										if(ratingLocation == 1) {
											newElement.innerHTML = "<a href=\"" + albumPage + "\"><span class=\"noscore\">This album has not yet been rated!</span></a>";											headCode = newElement.innerHTML;
											new_row.firstChild.nextSibling.appendChild(newElement);
										}
										else {
												headCode = "<a href=\"" + albumPage + "\"><img title=\"ERROR: This album has not yet been rated!\" src=\"http://img23.imageshack.us/img23/2649/crossp.gif\"></a>";
												placeFrame();
											}
									}
									else {
										if(ratingLocation == 1) {
											if(theRating[1] > 3.05) {
												newElement.innerHTML = "<a href=\"" + albumPage + "\"><span class=\"green\">" + theRating[1] + "</span></a> from " + theRating[2] + " ratings";
											}
											else if(theRating[1] > 2) {
												newElement.innerHTML = "<a href=\"" + albumPage + "\"><span class=\"yellow\">" + theRating[1] + "</span></a> from " + theRating[2] + " ratings";
											}
											else {
												newElement.innerHTML = "<a href=\"" + albumPage + "\"><span class=\"red\">" + theRating[1] + "</span></a> from " + theRating[2] + " ratings";
											}
											headCode = newElement.innerHTML;
											new_row.firstChild.nextSibling.appendChild(newElement);
										}
										else if (ratingLocation == 0) {
											if(theRating[1] > 3.05) {
												newElement.innerHTML = "<a href=\"" + albumPage + "\"><span class=\"green\">" + theRating[1] + "</span></a>";
											}
											else if(theRating[1] > 2) {
												newElement.innerHTML = "<a href=\"" + albumPage + "\"><span class=\"yellow\">" + theRating[1] + "</span></a>";
											}
											else {
												newElement.innerHTML = "<a href=\"" + albumPage + "\"><span class=\"red\">" + theRating[1] + "</span></a>";
											}
											headCode = newElement.innerHTML;
											new_row.firstChild.nextSibling.appendChild(newElement);
											placeFrame();
										}
									}
								}
							});
						}
						else {
							noRatingFound();
						}
					}
				});
			}
		}
		
	}
	
	
	function entityDecode(strHTML) {
		var tmpTextArea = document.createElement("textarea");
		tmpTextArea.innerHTML = strHTML.replace(/</g,"<").replace(/>/g,">");
		var decodedStr = tmpTextArea.value;
		//document.removeChild(tmpTextArea);
		return decodedStr;
	}
	
	function noRatingFound() {
		var newElement = document.createElement("span");
		if(ratingProvider == "MetaCritic") {
			newElement.innerHTML = "<SPAN CLASS=\"noscore\">This album is not present in MetaCritic's database</SPAN>";
			headCode = newElement.innerHTML;
			new_row.firstChild.nextSibling.appendChild(newElement);
			if (ratingLocation == 0) {
				headCode = "<img title=\"ERROR: This album is not present in MetaCritic's database\" src=\"http://img23.imageshack.us/img23/2649/crossp.gif\">";
				placeFrame();
			}
		}
		else {
			newElement.innerHTML = "<SPAN CLASS=\"noscore\">This album is not present in RateYourMusic's database</SPAN>";
			headCode = newElement.innerHTML;
			new_row.firstChild.nextSibling.appendChild(newElement);
			if (ratingLocation == 0) {
				headCode = "<img title=\"ERROR: This album is not present in RateYourMusic's database\" src=\"http://img23.imageshack.us/img23/2649/crossp.gif\">";
				placeFrame();
			}
		}
	}
	
	function noAlbumParse() {
		var newElement = document.createElement("span");
		newElement.innerHTML = "<SPAN CLASS=\"noscore\">ERROR: Could not parse album title</SPAN>";
		new_row.firstChild.nextSibling.appendChild(newElement);
		headCode = "<img title=\"ERROR: Could not parse album title\" src=\"http://img23.imageshack.us/img23/2649/crossp.gif\">";
		if (ratingLocation == 0) {
			placeFrame();
		}
		return;
	}
	
	function noArtistParse() {
		var newElement = document.createElement("span");
		newElement.innerHTML = "<SPAN CLASS=\"noscore\">ERROR: Could not parse artist title</SPAN>";
		new_row.firstChild.nextSibling.appendChild(newElement);
		headCode = "<img title=\"ERROR: Could not parse artist title\" src=\"http://img23.imageshack.us/img23/2649/crossp.gif\">";
		if (ratingLocation == 0) {
			placeFrame();
		}
		return;
	}
	
	main();
})();