Parser.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. $this->downloadFile($fileName, $urlXML, $this->rutaDiariaFecha);
  50. $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName);
  51. $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
  52. //while ($nuevoAtomAttrNext !== "") {
  53. $aux = 0;
  54. while ($aux <= 3) {
  55. $fileName = basename($nuevoAtomAttrNext);
  56. $this->downloadFile($fileName, $nuevoAtomAttrNext, $this->rutaDiariaFecha);
  57. $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName);
  58. $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
  59. $aux++;
  60. }
  61. }
  62. private function getFile($url, $fileName)
  63. {
  64. $feed = implode(file($url));
  65. $xml = simplexml_load_string($feed);
  66. $json = json_encode($xml);
  67. $array = json_decode($json, true);
  68. $this->formatXml($feed, $fileName);
  69. return $array;
  70. }
  71. private function newFolder($rutaFinal)
  72. {
  73. $result = false;
  74. if (!is_dir($rutaFinal)) {
  75. $result = mkdir($rutaFinal);
  76. mkdir($rutaFinal . "/format");
  77. }
  78. return $result;
  79. }
  80. private function formatXml($xml, $fileName)
  81. {
  82. $openingFormat = $this->regExIniciosEsquema($xml);
  83. $closureFormat = $this->regExFinalesEsquema($openingFormat);
  84. file_put_contents($this->rutaDiariaFecha . "/format/" . $fileName, $closureFormat);
  85. }
  86. private function readNextAttribute($archivo)
  87. {
  88. $enlace = "";
  89. foreach ($archivo['link'] as $row) {
  90. $attrNext = $row['@attributes']['rel'];
  91. $attrHref = $row['@attributes']['href'];
  92. if ($attrNext === "next") {
  93. $enlace = $attrHref;
  94. }
  95. }
  96. return $enlace;
  97. }
  98. private function downloadFile($fileName, $url, $ruta)
  99. {
  100. if (file_put_contents($ruta . $fileName, file_get_contents($url))) {
  101. return true;
  102. } else {
  103. return false;
  104. }
  105. }
  106. private function regExIniciosEsquema($xml)
  107. {
  108. $pattern = "/(<cac:)|(<cbc:)|(<cac-place-ext:)|(<cbc-place-ext:)/";
  109. $result = preg_replace($pattern, "<", $xml);
  110. return $result;
  111. }
  112. private function regExFinalesEsquema($xml)
  113. {
  114. $pattern = "/(<\/cac:)|(<\/cbc:)|(<\/cac-place-ext:)|(<\/cbc-place-ext:)/";
  115. $result = preg_replace($pattern, "</", $xml);
  116. return $result;
  117. }
  118. }