Parser.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. $downladedZip = $this->downloadFile($fileName, $urlXML, $this->rutaDiariaFecha);
  55. if ($downladedZip) {
  56. $zip = new ZipArchive;
  57. if ($zip->open($this->rutaDiariaFecha . $fileName)) {
  58. $zip->extractTo($this->rutaDiariaFecha);
  59. $zip->close();
  60. unlink($this->rutaDiariaFecha . $fileName);
  61. $archivosTotales = get_filenames($this->rutaDiariaFecha);
  62. foreach ($archivosTotales as $file) {
  63. $this->getFile($this->rutaDiariaFecha . $file, $file);
  64. }
  65. }
  66. }
  67. /*$fileName = basename($urlXML);
  68. $this->downloadFile($fileName, $urlXML, $this->rutaDiariaFecha);
  69. $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName);
  70. $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
  71. while ($nuevoAtomAttrNext !== "") {
  72. //$aux = 0;
  73. //while ($aux <= 3) {
  74. $fileName = basename($nuevoAtomAttrNext);
  75. $this->downloadFile($fileName, $nuevoAtomAttrNext, $this->rutaDiariaFecha);
  76. $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName);
  77. $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
  78. //$aux++;
  79. }*/
  80. }
  81. private function getFile($url, $fileName)
  82. {
  83. $feed = implode(file($url));
  84. $xml = simplexml_load_string($feed);
  85. $json = json_encode($xml);
  86. $array = json_decode($json, true);
  87. $this->formatXml($feed, $fileName);
  88. return $array;
  89. }
  90. private function newFolder($rutaFinal)
  91. {
  92. $result = false;
  93. if (!is_dir($rutaFinal)) {
  94. $result = mkdir($rutaFinal);
  95. mkdir($rutaFinal . "/format");
  96. }
  97. return $result;
  98. }
  99. private function formatXml($xml, $fileName)
  100. {
  101. $openingFormat = $this->regExIniciosEsquema($xml);
  102. $closureFormat = $this->regExFinalesEsquema($openingFormat);
  103. file_put_contents($this->rutaDiariaFecha . "/format/" . $fileName, $closureFormat);
  104. }
  105. private function readNextAttribute($archivo)
  106. {
  107. $enlace = "";
  108. foreach ($archivo['link'] as $row) {
  109. $attrNext = $row['@attributes']['rel'];
  110. $attrHref = $row['@attributes']['href'];
  111. if ($attrNext === "next") {
  112. if (!strpos($attrHref, "20220209_180023")) { //"20220127_")) {
  113. $enlace = $attrHref;
  114. } else {
  115. $enlace = "";
  116. }
  117. //$enlace = $attrHref;
  118. }
  119. }
  120. return $enlace;
  121. }
  122. private function downloadFile($fileName, $url, $ruta)
  123. {
  124. if (file_put_contents($ruta . $fileName, file_get_contents($url))) {
  125. return true;
  126. } else {
  127. return false;
  128. }
  129. }
  130. private function regExIniciosEsquema($xml)
  131. {
  132. $pattern = "/(<cac:)|(<cbc:)|(<cac-place-ext:)|(<cbc-place-ext:)/";
  133. $result = preg_replace($pattern, "<", $xml);
  134. return $result;
  135. }
  136. private function regExFinalesEsquema($xml)
  137. {
  138. $pattern = "/(<\/cac:)|(<\/cbc:)|(<\/cac-place-ext:)|(<\/cbc-place-ext:)/";
  139. $result = preg_replace($pattern, "</", $xml);
  140. return $result;
  141. }
  142. private function tablasCalculadas($fechaInicioParser)
  143. {
  144. $this->Parser_model->truncateTables("import_licitaciones_temp");
  145. $this->Parser_model->truncateTables("vista_datos_licitacion");
  146. $licitaciones = $this->Xlsexport_model->getLicitaciones("HAVING LI.fecha_creacion_log = '" . $fechaInicioParser . "'");
  147. insertBucleDuplicateOnKey($licitaciones['datos'], "vista_datos_licitacion", "");
  148. }
  149. }