Skip to content

Instantly share code, notes, and snippets.

@rbarros
Created March 15, 2012 19:00
Show Gist options
  • Save rbarros/2046065 to your computer and use it in GitHub Desktop.
Save rbarros/2046065 to your computer and use it in GitHub Desktop.
Update YQL Json
<?php
class consulta_horarios_m extends CI_Model
{
public function get_horarios()
{
$this->update_horarios();
$this->db->select('*');
$this->db->from('consulta_horario');
$this->db->limit('1');
$this->db->order_by('id','desc');
$query = $this->db->get();
$result = $query->row();
$destino = json_decode($result->destino);
$origem = json_decode($result->origem);
$horarios->destino = $destino->query->results->select->option;
$horarios->origem = $origem->query->results->select->option;
return $horarios;
}
/**
*
* @var string $table define o nome da tabela
*/
public $table;
/**
*
* Seta o nome da tabela a ser usada
*/
public function table($tabla)
{
$this->table = $tabla;
return $this;
}
/**
*
* Retorna todos os registros da tabela
*
* @param array $order order by para a query
* @param array $limit limit para a query
* @return object
*/
public function get_all($order = array(), $limit = array())
{
foreach ($order as $key => $value)
{
$this->db->order_by($key, $value);
}
if (count($limit) > 0)
{
$this->db->limit($limit[0], $limit[1]);
}
$query = $this->db->get($this->table);
return $query->result();
}
/**
* Atualiza os horarios utilizando o YQL
*
*/
public function update_horarios(){
$this->db->select("update,destino");
$this->db->from('consulta_horario');
$this->db->limit('1');
$this->db->order_by('id','desc');
$query = $this->db->get();
$result = $query->row();
/*
* Define o tempo (horas) para atualizar
* Exp: 24 horas = 24 * 3600 => 86400
* 2 dias = 24 * 2 = 48 * 3600 => 172800
* 1 ano
* 24h * 365 dias = 8760 * 3600 => 31536000
*/
$up = 24;
/*
* (30 dias 2764800) (48 horas 172800) (24 horas 86400) (1 ano 31536000)
* Retorna a data da ultima atualização
* Apos 24 horas atualiza novamente
*/
$update = strtotime( $result->update )+($up*3600);
/*
* Ano, mes e dia atual date('Y-m-d')
*/
$tmpdata = strtotime( date('Y-m-d') );
/*
* Verifica se o dia atual é maior/igual a ultima atualização + 48h
*/
//echo "<!-- ".$result->destino." -->";
if($tmpdata>=$update || preg_match("/(\"results\")+([:][\"']?null[\"']?)/", $result->destino)){
if (function_exists('curl_init')) {
log_message('info', "Update Destinos ".date('Y-m-d'));
echo "<!-- Update Destinos ".date('Y-m-d')." -->";
$yql_base_url = "http://query.yahooapis.com/v1/public/yql";
$yql_query = "select * from html where url='https://e-commerce.passagensweb.net/FrameWebSsl.aspx?codPro=25&tipPro=R' and xpath='//select[@id=\"cmbDestinos\"]'";
//$yql_query_url = $yql_base_url . "?q=" . str_replace("%2B","+",urlencode($yql_query));
$yql_query_url = $yql_base_url . "?q=" . urlencode($yql_query);
$yql_query_url .= "&format=json";
$yql_query_url;
echo "<!-- Update Destinos $yql_query_url -->";
$session = curl_init($yql_query_url);
curl_setopt($session, CURLOPT_RETURNTRANSFER,true);
$json = curl_exec($session);
//$phpObj = json_decode($json);
if( preg_match("/(\"results\")+([:][\"']?null[\"']?)/", $json) ){
$this->update_horarios();
}
$this->db->where('id', '1');
$values['update'] = date('Y-m-d');
$values['destino'] = $json;
$this->db->update('consulta_horario', $values);
}else{
log_message('error', 'Update Destinos error.');
echo "<!-- Update Destinos error -->";
}
}else{
log_message('error', "Update Destinos Atual => $tmpdata = ".date('Y-m-d', $tmpdata)." | Proximo => $update ".date('Y-m-d', $update));
echo "<!-- Update Destinos Atual => $tmpdata = ".date('Y-m-d', $tmpdata)." | Proximo => $update ".date('Y-m-d', $update)." -->";
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment