Parser.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. defined('BASEPATH') or exit('No direct script access allowed');
  3. class Parser extends CI_Controller
  4. {
  5. //TODO: PARAMETRIZAR BBDD -> SI EXISTE UN PROCESO EN MARCHA, NO INICIAR SIGUIENTE DESCARGA, AVISAR
  6. protected $rutaDiaria = FCPATH . "DOWNLOADS/DAILY/";
  7. protected $rutaDiariaFecha = "";
  8. protected $rutaMensual = FCPATH . "DOWNLOADS/MONTH/";
  9. protected $enlaceAtom = "";
  10. protected $fechaCreacionLog = "";
  11. public function __construct()
  12. {
  13. parent::__construct();
  14. $this->load->helper('file');
  15. $this->load->helper('xml');
  16. $this->load->library('parserfile');
  17. $this->load->model("Parser_model");
  18. }
  19. public function index()
  20. {
  21. $existCronRun = $this->Parser_model->existCronRun();
  22. $fechaCarpeta = "";
  23. if ($existCronRun['num_rows'] > 0) {
  24. if ($existCronRun['datos']->cron_ejecucion == 0) {
  25. $this->Parser_model->inicioParser();
  26. $resultSetPerfiles = $this->Parser_model->getUrlArchivos();
  27. $fechaCarpeta = date("Y-m-d_H-i-s");
  28. $fechaImportacionLog = date("Y-m-d H:i:s");
  29. foreach ($resultSetPerfiles['datos'] as $row) {
  30. $idUltimaImportacion = $this->Parser_model->setFechaNuevaImportacion($fechaImportacionLog);
  31. $this->fechaCreacionLog = $fechaCarpeta;
  32. $rutaPerfil = $this->rutaDiaria . "/" . $row->prefijos_tablas;
  33. $this->newFolder($rutaPerfil);
  34. if ($this->newFolder($rutaPerfil . "/" . $fechaCarpeta)) {
  35. $this->rutaDiariaFecha = $rutaPerfil . "/" . $fechaCarpeta . "/";
  36. //$this->downloadProcess($row->url_descarga);
  37. $this->downloadZip($row->url_descarga);
  38. }
  39. $files = get_filenames($this->rutaDiariaFecha . "/format");
  40. emailInicioProcesoDescarga($fechaImportacionLog, $files);
  41. $this->parserfile->index($this->rutaDiariaFecha, $fechaCarpeta, $row, $idUltimaImportacion);
  42. }
  43. $this->tablasCalculadas($idUltimaImportacion);
  44. emailFinProcesoDescarga($fechaImportacionLog);
  45. } else {
  46. echo "EXISTE UN CRON EN MARCHA.";
  47. }
  48. } else {
  49. echo "No existe parametro que iniciar.";
  50. }
  51. $this->Parser_model->stopParser();
  52. /* $resultSetPerfiles = $this->Parser_model->getUrlArchivos();
  53. foreach ($resultSetPerfiles['datos'] as $row) {
  54. $this->parserfile->index($this->rutaDiaria . "PER_PCSP_EX/2022-02-17_20-38-16", date("Y-m-d"), $row);
  55. } */
  56. }
  57. //TODO ELIMINAR PARA PROCESAR ZIPS, NO EL DIARIO
  58. private function downloadProcess($urlXML)
  59. {
  60. $fileName = basename($urlXML);
  61. $this->downloadFile($fileName, $urlXML, $this->rutaDiariaFecha);
  62. $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName);
  63. $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
  64. while ($nuevoAtomAttrNext !== "") {
  65. //$aux = 0;
  66. //while ($aux <= 3) {
  67. $fileName = basename($nuevoAtomAttrNext);
  68. $this->downloadFile($fileName, $nuevoAtomAttrNext, $this->rutaDiariaFecha);
  69. $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName);
  70. $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
  71. //$aux++;
  72. }
  73. }
  74. private function readNextAttribute($archivo)
  75. {
  76. $enlace = "";
  77. foreach ($archivo['link'] as $row) {
  78. $attrNext = $row['@attributes']['rel'];
  79. $attrHref = $row['@attributes']['href'];
  80. if ($attrNext === "next") {
  81. if (!strpos($attrHref, "licitacionesPerfilesContratanteCompleto3_2021")) { //"20220127_")) {
  82. $enlace = $attrHref;
  83. } else {
  84. $enlace = "";
  85. }
  86. //$enlace = $attrHref;
  87. }
  88. }
  89. return $enlace;
  90. }
  91. private function downloadZip($urlXML)
  92. {
  93. $fechaDescarga = $this->getFechaForDownloadZip();
  94. $fileName = basename($urlXML);
  95. $downladedZip = $this->downloadFile($fileName, $urlXML . $fechaDescarga, $this->rutaDiariaFecha);
  96. if ($downladedZip) {
  97. $zip = new ZipArchive;
  98. if ($zip->open($this->rutaDiariaFecha . $fileName)) {
  99. $zip->extractTo($this->rutaDiariaFecha);
  100. $zip->close();
  101. unlink($this->rutaDiariaFecha . $fileName);
  102. $archivosTotales = get_filenames($this->rutaDiariaFecha);
  103. foreach ($archivosTotales as $file) {
  104. $this->getFile($this->rutaDiariaFecha . $file, $file);
  105. }
  106. }
  107. }
  108. }
  109. private function getFechaForDownloadZip(): String
  110. {
  111. $result = "_";
  112. $anyo = date("Y");
  113. $mes = date("m", strtotime("-1 months"));
  114. $dia = date("d");
  115. if ($dia == "01") {
  116. $result .= $anyo . $mes . ".zip";
  117. } else {
  118. $result .= $anyo . $mes . ".zip";
  119. }
  120. return $result;
  121. }
  122. private function getFile($url, $fileName)
  123. {
  124. $feed = implode(file($url));
  125. $xml = simplexml_load_string($feed);
  126. $json = json_encode($xml);
  127. $array = json_decode($json, true);
  128. $this->formatXml($feed, $fileName);
  129. return $array;
  130. }
  131. private function newFolder($rutaFinal)
  132. {
  133. $result = false;
  134. if (!is_dir($rutaFinal)) {
  135. $result = mkdir($rutaFinal);
  136. mkdir($rutaFinal . "/format");
  137. }
  138. return $result;
  139. }
  140. private function formatXml($xml, $fileName)
  141. {
  142. $openingFormat = $this->regExIniciosEsquema($xml);
  143. $closureFormat = $this->regExFinalesEsquema($openingFormat);
  144. file_put_contents($this->rutaDiariaFecha . "/format/" . $fileName, $closureFormat);
  145. }
  146. private function downloadFile($fileName, $url, $ruta)
  147. {
  148. if (file_put_contents($ruta . $fileName, file_get_contents($url))) {
  149. return true;
  150. } else {
  151. return false;
  152. }
  153. }
  154. private function regExIniciosEsquema($xml)
  155. {
  156. $pattern = "/(<cac:)|(<cbc:)|(<cac-place-ext:)|(<cbc-place-ext:)/";
  157. $result = preg_replace($pattern, "<", $xml);
  158. return $result;
  159. }
  160. private function regExFinalesEsquema($xml)
  161. {
  162. $pattern = "/(<\/cac:)|(<\/cbc:)|(<\/cac-place-ext:)|(<\/cbc-place-ext:)/";
  163. $result = preg_replace($pattern, "</", $xml);
  164. return $result;
  165. }
  166. private function tablasCalculadas($idUltimaImportacion)
  167. {
  168. $this->Parser_model->truncateTables("vista_datos_licitacion");
  169. $licitaciones = $this->Xlsexport_model->getLicitaciones($idUltimaImportacion);
  170. insertBucleDuplicateOnKey($licitaciones['datos'], "vista_datos_licitacion", "");
  171. }
  172. }