Parser.php 5.3KB

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