123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- defined('BASEPATH') or exit('No direct script access allowed');
-
- class Parser extends CI_Controller
- {
-
- //TODO: PARAMETRIZAR BBDD -> SI EXISTE UN PROCESO EN MARCHA, NO INICIAR SIGUIENTE DESCARGA, AVISAR
- protected $rutaDiaria = FCPATH . "DOWNLOADS/DAILY/";
- protected $rutaDiariaFecha = "";
-
- protected $rutaMensual = FCPATH . "DOWNLOADS/MONTH/";
- protected $enlaceAtom = "";
- protected $fechaCreacionLog = "";
-
- public function __construct()
- {
- parent::__construct();
- $this->load->helper('file');
- $this->load->helper('xml');
- $this->load->library('parserfile');
-
- $this->load->model("Parser_model");
- }
-
- public function index()
- {
- $existCronRun = $this->Parser_model->existCronRun();
- $fechaCarpeta = "";
- if ($existCronRun['num_rows'] > 0) {
- if ($existCronRun['datos']->cron_ejecucion == 0) {
-
- $this->Parser_model->inicioParser();
-
- $resultSetPerfiles = $this->Parser_model->getUrlArchivos();
- $fechaCarpeta = date("Y-m-d_H-i-s");
- $fechaImportacionLog = date("Y-m-d H:i:s");
-
- foreach ($resultSetPerfiles['datos'] as $row) {
-
- $idUltimaImportacion = $this->Parser_model->setFechaNuevaImportacion($fechaImportacionLog);
- $this->fechaCreacionLog = $fechaCarpeta;
- $rutaPerfil = $this->rutaDiaria . "/" . $row->prefijos_tablas;
-
- $this->newFolder($rutaPerfil);
-
- if ($this->newFolder($rutaPerfil . "/" . $fechaCarpeta)) {
- $this->rutaDiariaFecha = $rutaPerfil . "/" . $fechaCarpeta . "/";
-
- //$this->downloadProcess($row->url_descarga);
- $this->downloadZip($row->url_descarga);
- }
-
- $files = get_filenames($this->rutaDiariaFecha . "/format");
- emailInicioProcesoDescarga($fechaImportacionLog, $files);
-
- $this->parserfile->index($this->rutaDiariaFecha, $fechaCarpeta, $row, $idUltimaImportacion);
- }
-
- $this->tablasCalculadas($idUltimaImportacion);
-
- emailFinProcesoDescarga($fechaImportacionLog);
-
- } else {
- echo "EXISTE UN CRON EN MARCHA.";
- }
- } else {
- echo "No existe parametro que iniciar.";
- }
-
- $this->Parser_model->stopParser();
-
- /* $resultSetPerfiles = $this->Parser_model->getUrlArchivos();
- foreach ($resultSetPerfiles['datos'] as $row) {
- $this->parserfile->index($this->rutaDiaria . "PER_PCSP_EX/2022-02-17_20-38-16", date("Y-m-d"), $row);
- } */
- }
-
- //TODO ELIMINAR PARA PROCESAR ZIPS, NO EL DIARIO
- private function downloadProcess($urlXML)
- {
-
- $fileName = basename($urlXML);
- $this->downloadFile($fileName, $urlXML, $this->rutaDiariaFecha);
- $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName);
- $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
-
- while ($nuevoAtomAttrNext !== "") {
- //$aux = 0;
- //while ($aux <= 3) {
-
- $fileName = basename($nuevoAtomAttrNext);
- $this->downloadFile($fileName, $nuevoAtomAttrNext, $this->rutaDiariaFecha);
- $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName);
-
- $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
- //$aux++;
- }
- }
-
- private function readNextAttribute($archivo)
- {
- $enlace = "";
- foreach ($archivo['link'] as $row) {
- $attrNext = $row['@attributes']['rel'];
- $attrHref = $row['@attributes']['href'];
-
- if ($attrNext === "next") {
- if (!strpos($attrHref, "licitacionesPerfilesContratanteCompleto3_2021")) { //"20220127_")) {
- $enlace = $attrHref;
- } else {
- $enlace = "";
- }
- //$enlace = $attrHref;
- }
- }
-
- return $enlace;
- }
-
- private function downloadZip($urlXML)
- {
- $fechaDescarga = $this->getFechaForDownloadZip();
- $fileName = basename($urlXML);
- $downladedZip = $this->downloadFile($fileName, $urlXML . $fechaDescarga, $this->rutaDiariaFecha);
-
- if ($downladedZip) {
- $zip = new ZipArchive;
- if ($zip->open($this->rutaDiariaFecha . $fileName)) {
- $zip->extractTo($this->rutaDiariaFecha);
- $zip->close();
- unlink($this->rutaDiariaFecha . $fileName);
- $archivosTotales = get_filenames($this->rutaDiariaFecha);
-
- foreach ($archivosTotales as $file) {
- $this->getFile($this->rutaDiariaFecha . $file, $file);
- }
- }
- }
- }
-
- private function getFechaForDownloadZip(): String
- {
- $result = "_";
- $anyo = date("Y");
- $mes = date("m", strtotime("-1 months"));
- $dia = date("d");
-
- if ($dia == "01") {
- $result .= $anyo . $mes . ".zip";
- } else {
- $result .= $anyo . $mes . ".zip";
- }
- return $result;
- }
-
- private function getFile($url, $fileName)
- {
-
- $feed = implode(file($url));
- $xml = simplexml_load_string($feed);
- $json = json_encode($xml);
- $array = json_decode($json, true);
-
- $this->formatXml($feed, $fileName);
- return $array;
- }
-
- private function newFolder($rutaFinal)
- {
- $result = false;
- if (!is_dir($rutaFinal)) {
- $result = mkdir($rutaFinal);
- mkdir($rutaFinal . "/format");
- }
-
- return $result;
- }
-
- private function formatXml($xml, $fileName)
- {
- $openingFormat = $this->regExIniciosEsquema($xml);
- $closureFormat = $this->regExFinalesEsquema($openingFormat);
- file_put_contents($this->rutaDiariaFecha . "/format/" . $fileName, $closureFormat);
- }
-
- private function downloadFile($fileName, $url, $ruta)
- {
- if (file_put_contents($ruta . $fileName, file_get_contents($url))) {
- return true;
- } else {
- return false;
- }
- }
-
- private function regExIniciosEsquema($xml)
- {
- $pattern = "/(<cac:)|(<cbc:)|(<cac-place-ext:)|(<cbc-place-ext:)/";
- $result = preg_replace($pattern, "<", $xml);
-
- return $result;
- }
-
- private function regExFinalesEsquema($xml)
- {
- $pattern = "/(<\/cac:)|(<\/cbc:)|(<\/cac-place-ext:)|(<\/cbc-place-ext:)/";
- $result = preg_replace($pattern, "</", $xml);
-
- return $result;
- }
-
- private function tablasCalculadas($idUltimaImportacion)
- {
- $this->Parser_model->truncateTables("vista_datos_licitacion");
-
- $licitaciones = $this->Xlsexport_model->getLicitaciones($idUltimaImportacion);
-
- insertBucleDuplicateOnKey($licitaciones['datos'], "vista_datos_licitacion", "");
-
- }
-
- }
|