Parser.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. if ($existCronRun['num_rows'] > 0) {
  24. if ($existCronRun['datos']->cron_ejecucion == 0) {
  25. $this->Parser_model->inicioParser();
  26. $resultSetPerfiles = $this->Parser_model->getUrlArchivos();
  27. foreach ($resultSetPerfiles['datos'] as $row) {
  28. $fechaCarpeta = date("Y-m-d_H-i-s");
  29. $this->fechaCreacionLog = $fechaCarpeta;
  30. $rutaPerfil = $this->rutaDiaria . "/" . $row->prefijos_tablas;
  31. $this->newFolder($rutaPerfil);
  32. if ($this->newFolder($rutaPerfil . "/" . $fechaCarpeta)) {
  33. $this->rutaDiariaFecha = $rutaPerfil . "/" . $fechaCarpeta . "/";
  34. $this->downloadProcess($row->url_descarga);
  35. }
  36. }
  37. $this->parserfile->index($this->rutaDiariaFecha, $fechaCarpeta, $row);
  38. } else {
  39. echo "EXISTE UN CRON EN MARCHA.";
  40. }
  41. } else {
  42. echo "No existe parametro que iniciar.";
  43. }
  44. $this->Parser_model->stopParser();
  45. $this->tablasCalculadas($this->fechaCreacionLog);
  46. /* $resultSetPerfiles = $this->Parser_model->getUrlArchivos();
  47. foreach ($resultSetPerfiles['datos'] as $row) {
  48. $this->parserfile->index($this->rutaDiaria . "PER_PCSP_EX/2022-02-17_20-38-16", date("Y-m-d"), $row);
  49. } */
  50. }
  51. private function downloadProcess($urlXML)
  52. {
  53. $fileName = basename($urlXML);
  54. $this->downloadFile($fileName, $urlXML, $this->rutaDiariaFecha);
  55. $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName);
  56. $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
  57. while ($nuevoAtomAttrNext !== "") {
  58. //$aux = 0;
  59. //while ($aux <= 3) {
  60. $fileName = basename($nuevoAtomAttrNext);
  61. $this->downloadFile($fileName, $nuevoAtomAttrNext, $this->rutaDiariaFecha);
  62. $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName);
  63. $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
  64. //$aux++;
  65. }
  66. }
  67. private function downloadZip($urlXML)
  68. {
  69. $fileName = basename($urlXML);
  70. $downladedZip = $this->downloadFile($fileName, $urlXML, $this->rutaDiariaFecha);
  71. if ($downladedZip) {
  72. $zip = new ZipArchive;
  73. if ($zip->open($this->rutaDiariaFecha . $fileName)) {
  74. $zip->extractTo($this->rutaDiariaFecha);
  75. $zip->close();
  76. unlink($this->rutaDiariaFecha . $fileName);
  77. $archivosTotales = get_filenames($this->rutaDiariaFecha);
  78. foreach ($archivosTotales as $file) {
  79. $this->getFile($this->rutaDiariaFecha . $file, $file);
  80. }
  81. }
  82. }
  83. }
  84. private function getFile($url, $fileName)
  85. {
  86. $feed = implode(file($url));
  87. $xml = simplexml_load_string($feed);
  88. $json = json_encode($xml);
  89. $array = json_decode($json, true);
  90. $this->formatXml($feed, $fileName);
  91. return $array;
  92. }
  93. private function newFolder($rutaFinal)
  94. {
  95. $result = false;
  96. if (!is_dir($rutaFinal)) {
  97. $result = mkdir($rutaFinal);
  98. mkdir($rutaFinal . "/format");
  99. }
  100. return $result;
  101. }
  102. private function formatXml($xml, $fileName)
  103. {
  104. $openingFormat = $this->regExIniciosEsquema($xml);
  105. $closureFormat = $this->regExFinalesEsquema($openingFormat);
  106. file_put_contents($this->rutaDiariaFecha . "/format/" . $fileName, $closureFormat);
  107. }
  108. private function readNextAttribute($archivo)
  109. {
  110. $enlace = "";
  111. foreach ($archivo['link'] as $row) {
  112. $attrNext = $row['@attributes']['rel'];
  113. $attrHref = $row['@attributes']['href'];
  114. if ($attrNext === "next") {
  115. if (!strpos($attrHref, "licitacionesPerfilesContratanteCompleto3_2021")) { //"20220127_")) {
  116. $enlace = $attrHref;
  117. } else {
  118. $enlace = "";
  119. }
  120. //$enlace = $attrHref;
  121. }
  122. }
  123. return $enlace;
  124. }
  125. private function downloadFile($fileName, $url, $ruta)
  126. {
  127. if (file_put_contents($ruta . $fileName, file_get_contents($url))) {
  128. return true;
  129. } else {
  130. return false;
  131. }
  132. }
  133. private function regExIniciosEsquema($xml)
  134. {
  135. $pattern = "/(<cac:)|(<cbc:)|(<cac-place-ext:)|(<cbc-place-ext:)/";
  136. $result = preg_replace($pattern, "<", $xml);
  137. return $result;
  138. }
  139. private function regExFinalesEsquema($xml)
  140. {
  141. $pattern = "/(<\/cac:)|(<\/cbc:)|(<\/cac-place-ext:)|(<\/cbc-place-ext:)/";
  142. $result = preg_replace($pattern, "</", $xml);
  143. return $result;
  144. }
  145. private function tablasCalculadas($fechaInicioParser)
  146. {
  147. $this->Parser_model->truncateTables("import_licitaciones_temp");
  148. $this->Parser_model->truncateTables("vista_datos_licitacion");
  149. $licitaciones = $this->Xlsexport_model->getLicitaciones("HAVING LI.fecha_creacion_log = '" . $fechaInicioParser . "'");
  150. insertBucleDuplicateOnKey($licitaciones['datos'], "vista_datos_licitacion", "");
  151. }
  152. }