Skip to content

Commit

Permalink
Otimizações no código e ajustes
Browse files Browse the repository at this point in the history
  • Loading branch information
leonetecbr committed Aug 9, 2021
1 parent 638a6f8 commit 2e98318
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
51 changes: 31 additions & 20 deletions app/Controller/Api.php → app/Api.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<?php

namespace Leone\Loteria\Federal\Controller;
namespace Leone\Loteria\Federal;

/**
* Pega o resultado da Federal de um site
*/
class Api{

private static $cookie_file = __DIR__.'/federal.txt';

/*
* Pega os dados da URL da API
* @param string $url
* @return array
*/
private static function getApi($url){
$c = curl_init();
$cookie_file = __DIR__.'/federal.txt';
$options = array(
CURLOPT_URL => $url,
$options = [CURLOPT_URL => $url,
CURLOPT_REFERER => 'http://www.loterias.caixa.gov.br',
CURLOPT_USERAGENT => 'Mozilla/5.0 (Linux; Android 12) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.120 Mobile Safari/537.36',
CURLOPT_RETURNTRANSFER => true,
Expand All @@ -25,9 +25,8 @@ private static function getApi($url){
CURLOPT_MAXREDIRS => 1,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIEFILE => $cookie_file,
CURLOPT_COOKIEJAR => $cookie_file
);
CURLOPT_COOKIEFILE => self::$cookie_file,
CURLOPT_COOKIEJAR => self::$cookie_file];
curl_setopt_array($c, $options);
$json = curl_exec($c);
curl_close($c);
Expand All @@ -36,7 +35,7 @@ private static function getApi($url){
$dados['resultados'] = $array['dezenasSorteadasOrdemSorteio'];
$dados['number'] = $array['numero'];
$dados['data'] = $array['dataApuracao'];
$dados['url'] = $url;
$dados['url'] = strstr($url, '?', true);
}else{
return 'Estamos com problemas para obter o resultado';
}
Expand All @@ -53,9 +52,7 @@ private static function getUrl($conc = ''){
$conc = '&concurso='.$conc;
}
$c = curl_init();
$cookie_file = __DIR__.'/federal.txt';
$options = array(
CURLOPT_URL => 'http://www.loterias.caixa.gov.br/wps/portal/loterias/landing/federal',
$options = [CURLOPT_URL => 'http://www.loterias.caixa.gov.br/wps/portal/loterias/landing/federal',
CURLOPT_REFERER => 'http://www.loterias.caixa.gov.br',
CURLOPT_USERAGENT => 'Mozilla/5.0 (Linux; Android 12) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.120 Mobile Safari/537.36',
CURLOPT_RETURNTRANSFER => true,
Expand All @@ -64,9 +61,8 @@ private static function getUrl($conc = ''){
CURLOPT_MAXREDIRS => 1,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIEFILE => $cookie_file,
CURLOPT_COOKIEJAR => $cookie_file
);
CURLOPT_COOKIEFILE => self::$cookie_file,
CURLOPT_COOKIEJAR => self::$cookie_file];
curl_setopt_array($c, $options);

try {
Expand Down Expand Up @@ -96,8 +92,8 @@ private static function getUrl($conc = ''){
*/
private static function get($conc = ''){
$cached = false;
$file = __DIR__.'/../../resources/cache/federal'.$conc.'.json';
$pfile = __DIR__.'/../../resources/cache/federal.json';
$file = __DIR__.'/../resources/cache/federal'.$conc.'.json';
$pfile = __DIR__.'/../resources/cache/federal.json';
if (empty($conc)) {
if (file_exists($file)) {
$day = date('D');
Expand All @@ -122,17 +118,29 @@ private static function get($conc = ''){
if (!$cached) {
if (file_exists($file)) {
$dado = json_decode(file_get_contents($file), true);
$url = $dado['url'];
$url = $dado['url'].'?timestampAjax='.str_replace('.', '', microtime(true));
$dados = self::getApi($url);
}elseif (!empty($conc) && file_exists($pfile)){
$dado = json_decode(file_get_contents($pfile), true);
$url = $dado['url'].'&concurso='.$conc;
$url = $dado['url'].'?timestampAjax='.str_replace('.', '', microtime(true)).'&concurso='.$conc;
$dados = self::getApi($url);
unset($dados['url']);
}
if (!is_array($dados)) {

if (empty($dados) || !is_array($dados)) {
$url = self::getUrl($conc);
$dados = self::getApi($url);
}

if (empty($conc)) {
$cfile = __DIR__.'/../resources/cache/federal'.$dados['number'].'.json';
if (!file_exists($cfile)) {
$dado = $dados;
unset($dado['url']);
file_put_contents($cfile, json_encode($dado));
}
}

file_put_contents($file, json_encode($dados));
return $dados;
}else{
Expand Down Expand Up @@ -178,8 +186,11 @@ public static function getCard($dados = null, $conc = ''){
public static function getNumber($number){
$dados = self::get();
if ($number>$dados['number']) {
$card = 'O concurso '.$number.' ainda não foi sorteado. O último concurso sorteado foi o '.$dados['number'].'.';
$dados['number'] = chunk_split($dados['number'], 2, ' ');
$number = chunk_split($number, 2, ' ');
$text = 'O concurso '.$number.' ainda não foi sorteado. O último concurso sorteado foi o '.$dados['number'].'.';
return ['card' => $text, 'text' => $text];
return ['card' => $card, 'text' => $text];
}elseif ($number==$dados['number']) {
return ['card' => self::getCard($dados, $number), 'text' => self::getText($dados, $number)];
}else{
Expand Down
4 changes: 2 additions & 2 deletions app/Controller/Skill.php → app/Skill.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Leone\Loteria\Federal\Controller;
namespace Leone\Loteria\Federal;

use \Alexa\Skill_Template;

Expand Down Expand Up @@ -71,7 +71,7 @@ private function sendRandomFederal(){
* Processa o Intent e direciona ao método correspondente
*/
private function processIntent(){
switch ($this->input()->request()->intent()->get_name()) {
switch ($this->input()->request()->intent()->get_name()??'') {
case 'AMAZON.StopIntent':
$this->end_request();
break;
Expand Down
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use \Dotenv\Dotenv;
use \Alexa\Exception;
use \Leone\Loteria\Federal\Controller\Skill;
use \Leone\Loteria\Federal\Skill;

$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->safeLoad();
Expand Down

0 comments on commit 2e98318

Please sign in to comment.