Last active
April 13, 2016 23:25
-
-
Save ikarius6/07acdc27ca8dc39e045b5eb667d2e204 to your computer and use it in GitHub Desktop.
Getting GIFs of pokemons (1 hour lazy script), separed by national (n_*) and johto (j_*) numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Getting GIFs of pokemons by Jack | |
*/ | |
set_time_limit( 0 ); | |
$dwl_folder = "./img"; //edit this | |
$p1 = get("http://es.pokemon.wikia.com/wiki/Lista_de_Pok%C3%A9mon_de_la_primera_generaci%C3%B3n"); | |
preg_match_all('/<table class="tabpokemon sortable mergetable" style="text-align:center; width:100%;">(?<trs>.*?)<\/table>/si', $p1['response'], $matches); | |
$pokemones = $matches['trs'][0]; | |
preg_match_all('/<tr>(?<tds>.*?)<\/tr>/si', $pokemones, $matches_2); | |
$atributos = $matches_2['tds']; | |
$urls = array(); | |
foreach($atributos as $atributo){ | |
preg_match_all('/<td> <a href="(?<url>[0-9a-z\/]+)" title=".*?">.*?<\/a>/si',$atributo, $matches_3); | |
if( count( $matches_3['url'] ) ) | |
array_push( $urls, $matches_3['url'][0] ); | |
} | |
foreach( $urls as $url ){ | |
$pokemon_p = get( "http://es.pokemon.wikia.com".$url ); | |
preg_match('/<span id="\s*numeronacional\s*">(?<nacional>.*?)<\/span>/si', $pokemon_p['response'], $matches_5); | |
preg_match('/<span id="\s*numerojohto\s*">(?<numerojohto>.*?)<\/span>/si', $pokemon_p['response'], $matches_6); | |
preg_match_all('/data-src="(?<asset>.*?)"/si', $pokemon_p['response'], $matches_4); | |
$nacional = str_pad( $matches_5['nacional']+0, 3, "0", STR_PAD_LEFT); | |
$numerojohto = str_pad( $matches_6['numerojohto']+0, 3, "0", STR_PAD_LEFT); | |
$assets = $matches_4['asset']; | |
foreach( $assets as $i=>$asset){ | |
if( strstr( $asset, "_NB.gif") || strstr( $asset, "_NB_variocolor.gif") ){ | |
echo $asset."<br/>"; | |
$image = get( $asset ); | |
if(!empty($image['response'])){ | |
file_put_contents($dwl_folder."/n_".$nacional.".gif", $image['response']); | |
file_put_contents($dwl_folder."/j_".$numerojohto.".gif", $image['response']); | |
} | |
} | |
} | |
} | |
function get( $url ){ | |
$curl = curl_init(); | |
$verbose = fopen('php://temp', 'w+'); | |
curl_setopt_array($curl, array( | |
CURLOPT_URL => $url, | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_FOLLOWLOCATION => 1, | |
CURLOPT_SSL_VERIFYPEER => false | |
)); | |
if( $result = curl_exec($curl) ){ | |
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
curl_close($curl); | |
if($httpCode != 200) { | |
$err = json_decode( $result ); | |
return array("success"=>false, "error"=>$err->mensagem ); | |
}else{ | |
return array("success"=>true, "response"=>$result); | |
} | |
}else{ | |
$error = curl_error($curl); | |
curl_close($curl); | |
return array("success"=>false, "error"=>$error); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example



