Skip to content

Parse mtgsalvation spoiler data

LevelX2 edited this page Jul 5, 2016 · 2 revisions

Code to parse a http://www.mtgsalvation.com/spoilers site to create the mtg-cards-data.txt format.

  1. Start for example Firefox

  2. Open the web site with the spoiler data (e.g. http://www.mtgsalvation.com/spoilers/172-eldritch-moon)

  3. Copy and paste the script into the firefox firebug console window and set the correct expansion name in the script.

  4. Execute the script

  5. Copy the created data from the console output to the mtg-cards-data.txt file.

     (function(){
        var x = '';
        function printCard(card){
             var text = '';
             text += card.name + '|Eldritch Moon|';
             text += (card.number ? card.number : '') + '|';
             text += card.rarity + '|';
             text += card.cost + '|';
             text += card.type + '|';
             text += (card.pt ? card.pt.split('/').join("|") : '|') + '|';
             text += card.abilities + '|';
             return text;
        }  
        $.each($('div.t-spoiler-wrapper'), function(index, value) {
             var $value = $(value);
             var card = {};
             card.name = $value.find('a.j-search-html').text().replace(/Æ/g,"AE");	 
    
             var findResult = $value.find('span.t-spoiler-artist').text();
             if(findResult.length > 0){
             var parts = findResult.split('#');  
             card.number = parts[1].split('/')[0].trim();
             }
    
             var findResult = $value.find('span.t-spoiler-rarity').find('span:first').attr('class');
             if (findResult.substr(findResult.length - 8) == 'uncommon') {
             card.rarity = 'U';
             } else if (findResult.substr(findResult.length - 6) == 'common') {
             card.rarity = 'C';
             } else if (findResult.substr(findResult.length - 4) == 'rare') {
             card.rarity = 'R';
             } else if (findResult.substr(findResult.length - 6) == 'mythic') {
             card.rarity = 'M';
             } else if (findResult.substr(findResult.length - 4) == 'land') {
             card.rarity = 'L';
             } else {
             card.rarity = '?' + findResult;
             }
    
             var findCosts = $value.find('ul.t-spoiler-mana').find('span').text();	 
             card.cost = '';
             $.each(findCosts , function (i, rowValue) {
                     card.cost += '{' + rowValue + '}';
             });	 
    
             card.type = $value.find('span.t-spoiler-type:first').text();
    
             card.pt = $value.find('span.t-spoiler-stat').text().replace(/\*/g,"0");
    
             var abilities = $value.find('div.t-spoiler-ability');
             abilities.find('span').replaceWith(function() { return '{' + this.innerHTML + '}' ; });
    
             card.abilities = '';
             $.each(abilities.find('p'), function (i, rowValue) {
             if (i > 0) {    	 
             card.abilities += '$';
             }
             // rowValue.find('span').replaceWith(function() { return '{' + this.innerHTML + '}' ; });
             //text = rowValue.innerHTML.replace(/(\r\n|\n|\r)/gm,"");
             card.abilities += rowValue.innerHTML.replace(/(\r\n|\n|\r)/gm,"").replace(/(<em>)/g,"<i>").replace(/(<\/em>)/g,"</i>");
             });     	 
             card.abilities = card.abilities.replace(/Æ/g,"AE").replace(/[\$]+/g,"$");
    
             x += printCard(card) + '\n';
        });   
        console.log(x);
    
     })();    
    
Clone this wiki locally