Parser.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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->library('parserdatabaselimpio');
  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. $rutaPerfil = $this->rutaDiaria . "/" . $row->prefijos_tablas;
  30. $this->newFolder($rutaPerfil);
  31. if ($this->newFolder($rutaPerfil . "/" . $fechaCarpeta)) {
  32. $this->rutaDiariaFecha = $rutaPerfil . "/" . $fechaCarpeta . "/";
  33. $this->downloadProcess($row->url_descarga);
  34. $this->parserfile->index($this->rutaDiariaFecha, $fechaCarpeta, $row);
  35. }
  36. }
  37. $this->parserdatabaselimpio->index();
  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. }
  46. private function downloadProcess($urlXML)
  47. {
  48. $fileName = basename($urlXML);
  49. $downladedZip = $this->downloadFile($fileName, $urlXML, $this->rutaDiariaFecha);
  50. if ($downladedZip) {
  51. $zip = new ZipArchive;
  52. if ($zip->open($this->rutaDiariaFecha . $fileName)) {
  53. $zip->extractTo($this->rutaDiariaFecha);
  54. $zip->close();
  55. unlink($this->rutaDiariaFecha . $fileName);
  56. $archivosTotales = get_filenames($this->rutaDiariaFecha);
  57. foreach ($archivosTotales as $file) {
  58. $this->getFile($this->rutaDiariaFecha . $file, $file);
  59. }
  60. }
  61. }
  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 getFile($url, $fileName)
  75. {
  76. $feed = implode(file($url));
  77. $xml = simplexml_load_string($feed);
  78. $json = json_encode($xml);
  79. $array = json_decode($json, true);
  80. $this->formatXml($feed, $fileName);
  81. return $array;
  82. }
  83. private function newFolder($rutaFinal)
  84. {
  85. $result = false;
  86. if (!is_dir($rutaFinal)) {
  87. $result = mkdir($rutaFinal);
  88. mkdir($rutaFinal . "/format");
  89. }
  90. return $result;
  91. }
  92. private function formatXml($xml, $fileName)
  93. {
  94. $openingFormat = $this->regExIniciosEsquema($xml);
  95. $closureFormat = $this->regExFinalesEsquema($openingFormat);
  96. file_put_contents($this->rutaDiariaFecha . "/format/" . $fileName, $closureFormat);
  97. }
  98. private function readNextAttribute($archivo)
  99. {
  100. $enlace = "";
  101. foreach ($archivo['link'] as $row) {
  102. $attrNext = $row['@attributes']['rel'];
  103. $attrHref = $row['@attributes']['href'];
  104. if ($attrNext === "next") {
  105. $enlace = $attrHref;
  106. }
  107. }
  108. return $enlace;
  109. }
  110. private function downloadFile($fileName, $url, $ruta)
  111. {
  112. if (file_put_contents($ruta . $fileName, file_get_contents($url))) {
  113. return true;
  114. } else {
  115. return false;
  116. }
  117. }
  118. private function regExIniciosEsquema($xml)
  119. {
  120. $pattern = "/(<cac:)|(<cbc:)|(<cac-place-ext:)|(<cbc-place-ext:)/";
  121. $result = preg_replace($pattern, "<", $xml);
  122. return $result;
  123. }
  124. private function regExFinalesEsquema($xml)
  125. {
  126. $pattern = "/(<\/cac:)|(<\/cbc:)|(<\/cac-place-ext:)|(<\/cbc-place-ext:)/";
  127. $result = preg_replace($pattern, "</", $xml);
  128. return $result;
  129. }
  130. }