Parser.php 6.6KB

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