'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(); $fechaInicio = '2021-12-15 00:00:00'; $fechaFin = '2021-12-16 00:00:00'; $this->stdout("{$fechaInicio} - {$fechaFin}\n"); 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", $fechaInicio]) // ->andWhere([">=", "[[timestamp]] at time zone box.timezone", new Expression("now() - interval '6 day'")]) ->andWhere(["<", "[[timestamp]] at time zone box.timezone", $fechaFin]) ->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"); } $this->stdout("{$modelo->hash} {$modelo->archivo}\n"); } catch(\Exception $e) { $this->stdout("{$modelo->hash} {$modelo->archivo} ya existe\n"); } $count++; } $vuelta = time() - $inicio; $this->stdout("Vuelta: {$vuelta}\n"); } return ExitCode::OK; } public function actionPorFecha() { if(!$this->fi || !$this->ff) { $this->stdout("Fecha inicial y final requeridas\n"); return ExitCode::OK; } $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(); $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", "{$this->fi} 00:00:00"]) // ->andWhere([">=", "[[timestamp]] at time zone box.timezone", new Expression("now() - interval '6 day'")]) ->andWhere(["<=", "[[timestamp]] at time zone box.timezone", "{$this->ff} 00:00:00"]) ->orderBy(["timestamp" => $order]); $this->stdout("{$archivos->createCommand()->getRawSql()}\n"); $count = 0; foreach($archivos->each() as $archivo) { if(!isset($ciudades[$archivo["box"]]) || !isset($estaciones[$archivo["station"]])) { 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"); } $this->stdout("{$modelo->hash} {$modelo->archivo}\n"); } catch(\Exception $e) { $this->stdout("{$modelo->hash} {$modelo->archivo} ya existe\n"); } $count++; } $vuelta = time() - $inicio; $this->stdout("Vuelta: {$vuelta}\n"); return ExitCode::OK; } }