Skip to content

Instantly share code, notes, and snippets.

@xombra
Created May 28, 2014 21:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save xombra/8281392f531f21c4accb to your computer and use it in GitHub Desktop.
Save xombra/8281392f531f21c4accb to your computer and use it in GitHub Desktop.
<?php
# Extrae imagenes de un usuario y se filtra por los hastag
#
# Uso:
# Debe crear un archivo .txt de nombre data.txt
# Debe crear una carpeta de nombre grabar
#
/*
Script bajo los términos y Licencia
GNU GENERAL PUBLIC LICENSE
Ver Terminos en:
http://www.gnu.org/copyleft/gpl.html
*/
# cambiar solo esto
$suario = 'ebeaccesorios';
$hastag = '#ebaccesorios';
# fin cambiar
# -----------------------------------------------------
function GET_INSTAGRAM($url,$hastag,$archivo)
{ $html = file_get_contents($url);
$html = strstr(trim($html), '{"entry_data"');
$html = strstr($html, '</script>', true);
$html = rtrim($html,';');
$html = str_replace('\/','/',$html);
$escribir_archivo = fopen($archivo, 'w');
fwrite($escribir_archivo, $html);
fclose($escribir_archivo);
LEER_ARCHIVO($archivo,$hastag);
return $html;
}
function LEER_ARCHIVO($archivo,$hastag)
{ $leer_archivo = @fopen($archivo, 'r');
if ($leer_archivo) {
$busca = 'width":640,"height":640';
while (($buffer = fgets($leer_archivo, 4096)) !== FALSE) {
if(stristr($buffer, $hastag) !== FALSE)
{ $tmp = explode(':{',$buffer);
foreach ($tmp as $value) {
if(stristr($value, $busca ) !== FALSE){
$cadena = substr($value,0,strpos($value, $busca));
$img = explode('"',$cadena);
echo '<p>Descargando imagen: '.$img[3].'</p>';
$extrae_pos_nombre = strrpos($img[3], '/')+1;
$archivo = 'grabar/'.substr($img[3],$extrae_pos_nombre);
$descarga = file_get_contents($img[3]);
file_put_contents($archivo, $descarga);
echo '<p>Redimensionando imagen: '.$img[3].'</p>';
REDIMENSIONAR(400,450,$archivo);
}
unset($img);
}
}
unset($tmp);
}
if (!feof($leer_archivo)) {
echo "Error: Algo fallo al leer el archivo."; }
fclose($leer_archivo);
}
return;
}
function REDIMENSIONAR($anchura,$maxima_y,$nombre)
{ $img_nueva = $nombre;
$datos = getimagesize($nombre);
$maxima_y = $datos[1];
switch ($datos[2]) {
case 2:
$img = imagecreatefromjpeg($nombre);
break;
default:
die("Tipo de Imagen no valida");
}
$ratio = $datos[0] / $anchura;
$altura = $datos[1] / $ratio;
if($altura >= $maxima_y)
{ $anchura2 = $maxima_y * $anchura / $altura;
$altura = $maxima_y;
$anchura = $anchura2;
}
$thumb = imagecreatetruecolor($anchura,$altura);
$blanco = imagecolorallocate($thumb, 255, 255, 255);
imagecolortransparent($thumb, $blanco);
imagesavealpha($thumb, true);
$color = imagecolorallocatealpha($thumb,0x00,0x00,0x00,127);
imagefill($thumb, 0, 0, $color);
imagecopyresampled($thumb, $img, 0, 0, 0, 0, $anchura, $altura, $datos[0], $datos[1]);
switch ($datos[2]) {
case 2:
imagejpeg($thumb,$img_nueva,100);
break;
default:
die("Tipo de Imagen no valida");
}
imagedestroy($img);
imagedestroy($thumb);
return $img_nueva;
}
# -------------------------------------------------------
echo '<p>Leyendo <p>';
$archivo = 'data.txt';
$url = "http://instagram.com/$suario";
$html = GET_INSTAGRAM($url,$hastag,$archivo);
echo '<p>Listo</p>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment