Card Array Script:
Edit and run this to download the includes file for the next script. Change the set name at the top to whatever set you want to snag, must be the exact name that gatherer uses to pull the set up with. Make the php file executable and run "~$ ./make-cards-array.php > cards.inc" . This file is used with the next script to download the images of the set
Code: Select all
#!/usr/bin/php -q
<?
//This script's purpose is to get all MTG card information from Wizard's Gatherer
//and create a php include file with an array of card information.
//Save as make-cards-array.php (not phps) and make it runnable: chmod 700 make-cards-array.php
//Generate cards.inc by running this script like: ./make-cards-array.php > cards.inc
//Change this to whatever set you wish to get the information of.
$cardsets = array(
"Masters Edition IV",
);
function clean_text($line) {
return html_entity_decode(strtr(htmlentities($line, ENT_QUOTES, 'UTF-8'), array(
'—' => '-',
'æ' => 'ae',
'Æ' => 'Ae',
'á' => 'a',
'â' => 'a',
'í' => 'i',
'ö' => 'o',
'ú' => 'u',
'û' => 'u',
' ' => ' '
)), ENT_QUOTES);
}
function get_card_data_piece($f) {
$rawline = fgets($f, 4096);
$rawline = fgets($f, 4096);
$rawline = fgets($f, 4096);
return clean_text(trim(strip_tags($rawline)));
}
ini_set('memory_limit', '64M');
$intable = false;
$cards = $card = $pieces = array();
foreach( $cardsets as $set ) {
$f = fopen('http://gatherer.wizards.com/Pages/Search/Default.aspx?output=spoiler&method=text&set=["'.urlencode($set).'"]', 'r');
$foundany = false;
while( !feof($f) ) {
$rawline = fgets($f, 4096);
if( strstr($rawline, '</table>') ) $intable = false;
if( $intable ) {
$line = clean_text(trim(strip_tags($rawline)));
if( substr($line, 0, 5) == 'Name:' ) {
$line = get_card_data_piece($f);
$second = '';
if( preg_match('/XX(.*) \((.*)\)/', $line, $matches) ) {
$line = $matches[1];
} else if( preg_match('/((.*) \/\/ (.*)) \((.*)\)/', $line, $matches) ) {
$line = $matches[1];
if( $matches[3] == $matches[4] ) {
$second = 'split';
}
} else if( preg_match('/(.*) \((.*)\)/', $line, $matches) ) {
$second = 'flip';
$line = $matches[1];
$card[extra] = "-----\n$matches[2]\n";
}
$card[name] = $line;
} else if( substr($line, 0, 5) == 'Cost:' ) {
$card[cost] = get_card_data_piece($f);
} else if( substr($line, 0, 5) == 'Type:' ) {
$card[type] = get_card_data_piece($f);
} else if( substr($line, 0, 8) == 'Pow/Tgh:' ) {
$line = substr(get_card_data_piece($f), 1, -1);
$pt = explode('/', $line);
$card[power] = $pt[0];
$card[toughness] = $pt[1];
} else if( substr($line, 0, 8) == 'Loyalty:' ) {
$card[toughness] = substr(get_card_data_piece($f), 1, -1);
} else if( substr($line, 0, 11) == 'Set/Rarity:' ) {
$sr = explode(',', strtr(get_card_data_piece($f), array('Mythic Rare' => 'Mythic')));
for( $i = 0; $i < count($sr); $i++ ) {
$p = strrpos($sr[$i], ' ');
$card[sets] .= trim(substr($sr[$i], 0, $p)) . ',';
}
$card[sets] = strtr(substr($card[sets], 0, -1), array('"Timeshifted"' => 'Timeshifted'));
for( $i = 0; $i < count($sr); $i++ ) {
if( strpos($sr[$i], $set) !== false ) break;
}
if( $i >= count($sr) ) $i = 0;
$sr = explode(' ', trim($sr[$i]));
$card[rarity] = $sr[count($sr) - 1];
} else if( substr($line, 0, 11) == 'Rules Text:' ) {
$card[rules] = get_card_data_piece($f);
for( $i = 0; $i < 100; $i++ ) {
$rawline = fgets($f, 4096);
if( strpos($rawline, '</td>') !== false ) break;
$card[rules] .= "\n" . clean_text(trim(strip_tags($rawline)));
}
} else if( strpos($rawline, '<br />') !== false ) {
$card[set] = strtr($set, array('"Timeshifted"' => 'Timeshifted'));
if( $card[rarity] == 'Special' ) {
if( $cards[$card[name]][rarity] != '' ) {
$card[rarity] = $cards[$card[name]][rarity];
}
}
if( $second != '' ) {
$card[second] = $second;
$pieces[$card[name]] = $card;
} else {
$cards[$card[name]] = $card;
}
$card = array();
$foundany = true;
}
}
if( strstr($rawline, '<div class="textspoiler">') ) $intable = true;
}
fclose($f);
if( !$foundany ) {
die("Found no cards for $set\n");
}
}
foreach( $pieces as $card ) {
if( !isset($cards[$card[name]]) ) {
$cards[$card[name]] = $card;
} else if( $card[second] == 'split' ) {
$cards[$card[name]][cost] .= ' // ' . $card[cost];
$cards[$card[name]][type] .= ' // ' . $card[type];
$cards[$card[name]][rules] .= '//' . $card[rules];
} else if( $card[second] == 'flip' ) {
$cards[$card[name]][rules] .= $card[extra] . $card[type] . "\n";
if( $card[power] != '' || $card[toughness] != '' ) $cards[$card[name]][rules] .= "$card[power]/$card[toughness]\n";
$cards[$card[name]][rules] .= $card[rules];
}
}
ksort($cards);
function someslashes($str) {
return strtr($str, array('"' => '\"', '\\' => '\\\\'));
}
echo "<" . "?\n\$cardsets = array(\n";
$first = true;
foreach( $cardsets as $set ) {
$set = strtr($set, array('"Timeshifted"' => 'Timeshifted'));
if( $first ) {
$first = false;
} else {
echo ",\n";
}
echo "\t\"$set\"";
}
echo "\n);\n";
echo "\$cards = array(\n";
$first = true;
foreach( $cards as $card ) {
if( $first ) {
$first = false;
} else {
echo ",\n";
}
echo "\t\"" . someslashes(strtolower($card[name])) . "\" => array(\n" .
"\t\t\"name\" => \"" . someslashes($card[name]) . "\",\n",
"\t\t\"cost\" => \"" . someslashes($card[cost]) . "\",\n",
"\t\t\"type\" => \"" . someslashes($card[type]) . "\",\n",
"\t\t\"set\" => \"" . someslashes($card[set]) . "\",\n",
"\t\t\"sets\" => \"" . someslashes($card[sets]) . "\",\n",
"\t\t\"rarity\" => \"" . someslashes($card[rarity]) . "\",\n",
"\t\t\"power\" => \"" . someslashes($card[power]) . "\",\n",
"\t\t\"toughness\" => \"" . someslashes($card[toughness]) . "\",\n",
"\t\t\"rules\" => \"" . strtr(someslashes($card[rules]), array("\n" => '\n', 'T ' => 'T: ')) . "\"\n" .
"\t)";
}
echo "\n);\n?" . ">\n";
?>
Code: Select all
#!/usr/bin/php -q
<?
//This script's purpose is to get all card images from the most recent MTG set and create 3 separate directories and zip files full of jpgs.
//Origs will contain the exact jpg file as downloaded.
//Fulls will contain a slighly cropped (to remove the rounded black border's corner), and slightly blown up and sharpened image for each jpg.
//Crops will contain the cropped artwork from the top portion of the card with no borders or rules text for each jpg.
//Save as get-gatherer-images.php (not phps) and make it runnable: chmod 700 get-gatherer-images.php
//Generate 3 directories full of jpgs as well as 3 zip files by running this script like: ./get-gatherer-images.php
//This script requires that you have access to the following 3 somewhat common linux command line utilities:
// wget located at /usr/bin/wget in order to quickly fetch and save an image from a url
// imagemagick's convert program at /usr/local/bin/convert in order to crop and resize images
// zip at /usr/bin/zip in order to create the zip files
//Please check that these utilities are available and at the locations listed above, or this script will not work.
ini_set('memory_limit', '64M');
require_once('cards.inc');
@mkdir('Origs');
@mkdir('PSP');
@mkdir('PSP/thumbnails');
$set = $cardsets[count($cardsets) - 1];
foreach( $cards as $card ) {
if( $card[set] == $set ) {
echo "Card:$card[name] ";
$searchname = strtr(urlencode(strtr($card[name], array(' ' => '\s'))), array(
'Ae' => '(Ae|%c3%86)',
'ae' => '(ae|%c3%a6)'
));
$f = fopen('http://gatherer.wizards.com/Pages/Search/Default.aspx?name=[m/^'.$searchname.'$/]', 'r');
$datas = stream_get_meta_data($f);
fclose($f);
foreach( $datas[wrapper_data] as $data ) {
if( substr($data, 0, 9) == 'Location:' ) {
$num = substr($data, strrpos($data, '=') + 1);
echo "ID:$num ";
$filename = $num;
if( $card[name] == 'Plains' || $card[name] == 'Island' || $card[name] == 'Swamp' || $card[name] == 'Mountain' || $card[name] == 'Forest' ) {
$filename .= '1';
}
shell_exec("/usr/bin/wget -q -O \"Origs/$filename.jpg\" \"http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=$num&type=card\" > /dev/null");
echo "Downloaded ";
shell_exec("/usr/bin/convert \"Origs/$num.jpg\" -crop 200x285+13+12 -quality 80 \"PSP/$num.jpg\"");
shell_exec("/usr/bin/convert \"PSP/$num.jpg\" -resize 45x64 -quality 80 \"PSP/thumbnails/$num.jpg\"");
echo "Converted for Wagic";
}
}
echo "\n";
}
}
shell_exec("/usr/bin/zip -9 Origs.$searchname.zip Origs/*");
shell_exec("/usr/bin/zip -0 $searchname.zip PSP/*");
?>