| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace app\commands;
- use app\models\Descarga;
- use yii\console\Controller;
- use yii\console\ExitCode;
- use yii\db\Expression;
- use yii\db\Query;
- use yii\helpers\ArrayHelper;
- class SyncCompleteController extends Controller {
- public function actionIndex() {
- $estados = [
- 'AGUASCALIENTES' => 'AGU',
- 'BAJA CALIFORNIA' => 'BCN',
- 'BAJA CALIFORNIA SUR' => 'BCS',
- 'CAMPECHE' => 'CAM',
- 'CHIAPAS' => 'CHP',
- 'CHIHUAHUA' => 'CHH',
- 'CIUDAD DE MÉXICO' => 'CMX',
- 'COAHUILA' => 'COA',
- 'COLIMA' => 'COL',
- 'DURANGO' => 'DUR',
- 'GUANAJUATO' => 'GUA',
- 'GUERRERO' => 'GRO',
- 'HIDALGO' => 'HID',
- 'JALISCO' => 'JAL',
- 'MEXICO' => 'MEX',
- 'MICHOACAN' => 'MIC',
- 'MORELOS' => 'MOR',
- 'NAYARIT' => 'NAY',
- 'NUEVO LEON' => 'NLE',
- 'OAXACA' => 'OAX',
- 'PUEBLA' => 'PUE',
- 'QUERETARO' => 'QUE',
- 'QUINTANA ROO' => 'ROO',
- 'SAN LUIS POTOSI' => 'SLP',
- 'SINALOA' => 'SIN',
- 'SONORA' => 'SON',
- 'TABASCO' => 'TAB',
- 'TAMAULIPAS' => 'TAM',
- 'TLAXCALA' => 'TLA',
- 'VERACRUZ' => 'VER',
- 'YUCATAN' => 'YUC',
- 'ZACATECAS' => 'ZAC'
- ];
- $order = SORT_ASC;
- $ciudadEstacion = (new Query())
- ->select(["idCiudad", "idEstacion"])
- ->from("box")
- ->innerJoin("CiudadEstacion", "box.id = {{CiudadEstacion}}.[[idCiudad]]")
- ->andWhere([
- "box.activo" => true
- ])
- ->andWhere("[[idGrupo]] is not null")
- ->all();
- $idCiudades = ArrayHelper::getColumn($ciudadEstacion, "idCiudad");
- $ciudades = (new Query())
- ->select(["id", "nombre", "idEstado", "timezone", "tipo"])
- ->from("box")
- ->where(["id" => $idCiudades])
- ->indexBy("id")
- ->all();
- $idEstaciones = ArrayHelper::getColumn($ciudadEstacion, "idEstacion");
- $estaciones = (new Query())
- ->select(["id", "clave", "siglas", "frecuencia", "descripcion"])
- ->from("Estacion")
- ->andWhere(["id" => $idEstaciones])
- ->indexBy("clave")
- ->all();
- while(true) {
- $inicio = time();
- $archivos = (new Query())
- ->select("hash, box, station, filename, [[timestamp]] at time zone box.timezone as timestamp")
- ->from("file")
- ->innerJoin("box", "box.id = file.box")
- ->andWhere([">=", "[[timestamp]] at time zone box.timezone", new Expression("now() - interval '2 day'")])
- // ->andWhere(["<=", "[[timestamp]] at time zone box.timezone", '2021-10-01 00:00:00'])
- ->orderBy(["timestamp" => $order]);
- // $this->stdout("{$archivos->createCommand()->getRawSql()}\n");
- $count = 0;
- foreach($archivos->each() as $archivo) {
- if(!isset($ciudades[$archivo["box"]])) {
- continue;
- }
- $modelo = new Descarga();
- $modelo->hash = $archivo["hash"];
- $modelo->estacion = $archivo["station"];
- $modelo->archivo = $archivo["filename"];
- $modelo->ciudad = $archivo["box"];
- $modelo->pc = null;
- $modelo->fecha = $archivo["timestamp"];
- $modelo->descargado = false;
- $modelo->nombre = basename($archivo["filename"]);
- $ciudad = $ciudades[$archivo["box"]];
- $estacion = $estaciones[$archivo["station"]];
- $fecha = \DateTime::createFromFormat("Y-m-d H:i:s", $archivo["timestamp"]);
- $siglas = explode("-", $estacion["siglas"]);
- $tipo = "";
- $senal = $siglas[0];
- if(isset($siglas[1])) {
- $tipo = $siglas[1];
- }
- if($tipo === "") {
- if($ciudad["tipo"] === "tv") {
- $tipo = "TDT";
- } elseif ($ciudad["tipo"] === "radio") {
- $tipo = "FM";
- } else {
- $tipo = "AM";
- }
- }
- $nombreCiudad = $ciudad["idEstado"];
- if($tipo === "AM") {
- $desc = explode(",", $estacion["descripcion"]);
- $nombreCiudad = $estacion["descripcion"];
- if(isset($desc[1])) {
- $nombreCiudad = $desc[1];
- }
- }
- if(isset($estados[$nombreCiudad])) {
- $nombreCiudad = $estados[$nombreCiudad];
- }
- $nombreCiudad = str_replace(" ", "_", trim($nombreCiudad));
- $y = $fecha->format("Y");
- $m = $fecha->format("m");
- $d = $fecha->format("d");
- $modelo->ruta = "{$y}/{$tipo}/{$nombreCiudad}/{$senal}/{$m}/{$d}";
- try {
- if(!$modelo->save()) {
- $errores = json_encode($modelo->getFirstErrors());
- $this->stdout("Error al guardar {$modelo->hash} {$errores}\n");
- }
- } catch(\Exception $e) {
- $this->stdout("Error al guardar {$modelo->hash} {$modelo->archivo}\n");
- }
- $count++;
- }
- $vuelta = $inicio - time();
- $this->stdout("Vuelta: {$vuelta}\n");
- }
- return ExitCode::OK;
- }
- }
|