Parser.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. }
  18. public function index()
  19. {
  20. $fechaCarpeta = date("Y-m-d_H-i-s");
  21. if ($this->newFolder($this->rutaDiaria . $fechaCarpeta)) {
  22. $this->rutaDiariaFecha = $this->rutaDiaria . $fechaCarpeta . "/";
  23. $this->downloadProcess(URL_DIARIA_CONTRATOS_MENORES_PERFILES_CONTRATANES);
  24. $this->parserfile->index($this->rutaDiariaFecha, $fechaCarpeta);
  25. }
  26. }
  27. private function downloadProcess($urlXML)
  28. {
  29. $fileName = basename($urlXML);
  30. $this->downloadFile($fileName, $urlXML, $this->rutaDiariaFecha);
  31. $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName);
  32. $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
  33. //while ($nuevoAtomAttrNext !== "") {
  34. $aux = 0;
  35. while ($aux <= 3) {
  36. $fileName = basename($nuevoAtomAttrNext);
  37. $this->downloadFile($fileName, $nuevoAtomAttrNext, $this->rutaDiariaFecha);
  38. $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName);
  39. $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
  40. $aux++;
  41. }
  42. }
  43. private function getFile($url = URL_DIARIA, $fileName)
  44. {
  45. $feed = implode(file($url));
  46. $xml = simplexml_load_string($feed);
  47. $json = json_encode($xml);
  48. $array = json_decode($json, true);
  49. $this->formatXml($feed, $fileName);
  50. return $array;
  51. }
  52. private function newFolder($rutaFinal)
  53. {
  54. $result = false;
  55. if (!is_dir($rutaFinal)) {
  56. $result = mkdir($rutaFinal);
  57. mkdir($rutaFinal . "/format");
  58. }
  59. return $result;
  60. }
  61. private function formatXml($xml, $fileName)
  62. {
  63. $openingFormat = $this->regExIniciosEsquema($xml);
  64. $closureFormat = $this->regExFinalesEsquema($openingFormat);
  65. file_put_contents($this->rutaDiariaFecha . "/format/" . $fileName, $closureFormat);
  66. }
  67. private function readNextAttribute($archivo)
  68. {
  69. $enlace = "";
  70. foreach ($archivo['link'] as $row) {
  71. $attrNext = $row['@attributes']['rel'];
  72. $attrHref = $row['@attributes']['href'];
  73. if ($attrNext === "next") {
  74. $enlace = $attrHref;
  75. }
  76. }
  77. return $enlace;
  78. }
  79. private function downloadFile($fileName, $url, $ruta)
  80. {
  81. if (file_put_contents($ruta . $fileName, file_get_contents($url))) {
  82. return true;
  83. } else {
  84. return false;
  85. }
  86. }
  87. private function regExIniciosEsquema($xml)
  88. {
  89. $pattern = "/(<cac:)|(<cbc:)|(<cac-place-ext:)|(<cbc-place-ext:)/";
  90. $result = preg_replace($pattern, "<", $xml);
  91. return $result;
  92. }
  93. private function regExFinalesEsquema($xml)
  94. {
  95. $pattern = "/(<\/cac:)|(<\/cbc:)|(<\/cac-place-ext:)|(<\/cbc-place-ext:)/";
  96. $result = preg_replace($pattern, "</", $xml);
  97. return $result;
  98. }
  99. }