Skip to content

Instantly share code, notes, and snippets.

@kernelshreyak
Last active March 9, 2019 09:31
Show Gist options
  • Save kernelshreyak/ba09c4afdc9255f07802b25f07f820d9 to your computer and use it in GitHub Desktop.
Save kernelshreyak/ba09c4afdc9255f07802b25f07f820d9 to your computer and use it in GitHub Desktop.
Remotely get card images directly via webpages and load them into databases and filesystems
<?php
// This code uses a kind of web scraping to get card images and load it into your filesystem and database
//This requires card data to be present in XML format (Cockatrice format works)
function copyfile($file_source, $file_target) {
$rh = fopen($file_source, "rb");
$wh = fopen($file_target, "wb");
if ($rh===false || $wh===false) {
// error reading or opening file
return true;
}
while (!feof($rh)) {
if (fwrite($wh, fread($rh, 1024)) === FALSE) {
echo "<br>Download error: Cannot write to file ($file_target)<br>";
return false;
}
}
fclose($rh);
fclose($wh);
// No error
echo "<hr>$file_source Download completed to $file_target<hr>";
return true;
}
function loadCards(){
global $dbconnect;
$xml=simplexml_load_file("pokemon.xml") or die("Error: Cannot create object");
$i=0;
foreach($xml->cards->card as $card){
if($card->type=="Trainer" || $card->type=="Energy") //DONOT import these types
continue;
$picurl=$card->set['picURL'];
$hp=explode("/",$card->pt);
$hp=intval($hp[1]);
//echo "$i) $card->name<br>";
if(mysqli_query($dbconnect,"INSERT INTO tcg_cards SET Name='$card->name',picurl='$picurl',Type='$card->type',Color='$card->color'
,Mana=0,HP=$hp,Deck='$card->set',gametype=3"))
echo "$card->name added!<br>";
else echo mysqli_error($dbconnect)." $card->name not added<br>";
$i+=1;
}
}
function getHP($cardname){ //get HP value of DM card via API
// global $dbconnect;
$content=file_get_contents("https://duelmasters.fandom.com/wiki/$cardname");
$parts=explode(" Power Creatures",$content);
$parts2=explode("\",\"",$parts[0]);
return $parts2[count($parts2)-1];
}
function getImageURL($cardname){ //get image of DM card via API
$content=file_get_contents("https://duelmasters.fandom.com/wiki/$cardname");
$parts=explode('og:image" content="',$content);
$parts2=explode(".jpg",$parts[1]);
return str_replace(" ","",$parts2[0]).".jpg";
}
$cardq=mysqli_query($dbconnect,"SELECT ID,picurl FROM tcg_cards WHERE gametype=3");
// while($cardat=mysqli_fetch_array($cardq)){
// //$hp=intval(getHP(str_replace(" ","_",$cardat[1])));
// $imgurl=getImageURL(str_replace("_highres","",$cardat[1]));
// //echo "Image URL of $cardat[1] is $imgurl <br>";
// if(mysqli_query($dbconnect,"UPDATE tcg_cards SET picurl='$imgurl' WHERE ID=$cardat[0]"))
// echo "Image of $cardat[1] updated with $imgurl <br>";
// }
//loadCards();
//download card images
//$cardq=mysqli_query($dbconnect,"SELECT ID,picurl FROM tcg_cards WHERE gametype=2");
//
//while($cardat=mysqli_fetch_array($cardq)){
// $imgurl=$cardat[1];
// $fparts=explode("/",$imgurl);
// $localpath="card_images/".$fparts[count($fparts)-1];
// copyfile($imgurl,$localpath);
// //echo "copyfile($imgurl,$localpath)<br>";
//}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment