Skip to content

Instantly share code, notes, and snippets.

@klebinhopk
Last active April 11, 2018 00:35
Show Gist options
  • Save klebinhopk/4ff634cfcc0c99aea75330439dcb98d4 to your computer and use it in GitHub Desktop.
Save klebinhopk/4ff634cfcc0c99aea75330439dcb98d4 to your computer and use it in GitHub Desktop.
<?php
header('Content-Type: text/html; charset=utf-8');
set_time_limit(0);
require "classes/SimpleImage.php";
require "funcoes/functions.php";
//FAZ A CONEXÃO COM O BANCO
$conn = conn();
$page = 1;
do {
//ACESSA A API DE FILMES
$content = api ($page);
foreach ($content as $key => $val) {
$titulo = $val["titulo"];
$sinopse = $val["sinopseFull"];
$imagem = $val["poster"];
$nomeImagem = urlFriendly($titulo);
$urlImagem = $nomeImagem.'.jpg';
getImages($imagem, $titulo, 250, 600);
$genero = $val["genero"];
$genero = explode(", ", $genero);
$cn = 0;
$ids = array();
foreach ($genero as $k => $v) {
$verifica = $conn->prepare("SELECT categoryId, category FROM categories WHERE category = '$v'");
$verifica->execute();
$result = $verifica->fetch(PDO::FETCH_OBJ);
if($verifica->rowCount() > 0) { // SE A CATEGORIA EXISTIR
$ids[$cn] = $result->categoryId;
$category = $result->category;
echo $ids[$cn].' - Já existe a categoria '.$v.'<br/>';
}else
{ // SE A CATEGORIA NÃO EXISTIR
$stmt = $conn->prepare("INSERT INTO categories (category) VALUES (:category)");
$stmt->bindParam(':category', $v, PDO::PARAM_STR);
$stmt->execute();
$id = $conn->lastInsertId();
$category = $v;
//SELECIONA O ULTIMO ID DA CATEGORIA
$stmt = $conn->prepare("SELECT * FROM categories ORDER BY categoryId DESC limit 1");
$stmt->execute();
$resultado = $stmt->fetch(PDO::FETCH_OBJ);
$ids[$cn] = $resultado->categoryId;
}
$cn++; // SOMA +1 NA VARIÁVEL CONTADORA
$category2 = $v; // PEGA O VALOR DO JSON
}
$id2 = '';
foreach ($ids as $n) {
$id2 .= $n.',';
}
$id2 = rtrim($id2, ',');
$verifica = $conn->prepare("SELECT titulo FROM filmes WHERE titulo = '$titulo'");
$verifica->bindParam(':titulo', $titulo, PDO::PARAM_STR);
$verifica->execute();
if($verifica->rowCount() > 0) {
echo "<h4><strong>Já existe o filme</strong> ".$titulo." <br/></h4>";
}else
{
#INSERE O FILME
$stmt = $conn->prepare("INSERT INTO filmes (categoryId, category, titulo, imagem, sinopse) VALUES (:categoryId, :category, :titulo, :imagem, :sinopse)");
$stmt->bindParam(':categoryId', $id2, PDO::PARAM_STR);
$stmt->bindParam(':category', $category2, PDO::PARAM_STR);
$stmt->bindParam(':titulo', $titulo, PDO::PARAM_STR);
$stmt->bindParam(':imagem', $urlImagem, PDO::PARAM_STR);
$stmt->bindParam(':sinopse', $sinopse, PDO::PARAM_STR);
$stmt->execute();
echo "O Filme <strong>".$titulo."</strong> foi inserido com sucesso<br/>";
}
}
echo "Pagina ".$page;
$page++;
} while ($page <= 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment