瀏覽代碼

controlador de archivos totales, paquetes 500

Nermosis 3 年之前
父節點
當前提交
1a91958d88
共有 1 個文件被更改,包括 198 次插入0 次删除
  1. 198 0
      application/controllers/ParserCompleto.php

+ 198 - 0
application/controllers/ParserCompleto.php 查看文件

@@ -0,0 +1,198 @@
1
+<?php
2
+defined('BASEPATH') or exit('No direct script access allowed');
3
+
4
+/**
5
+ *
6
+ * Controller ParserCompleto
7
+ *
8
+ * This controller for ...
9
+ *
10
+ * @package   CodeIgniter
11
+ * @category  Controller CI
12
+ * @author    Setiawan Jodi <jodisetiawan@fisip-untirta.ac.id>
13
+ * @author    Raul Guerrero <r.g.c@me.com>
14
+ * @link      https://github.com/setdjod/myci-extension/
15
+ * @param     ...
16
+ * @return    ...
17
+ *
18
+ */
19
+
20
+class ParserCompleto extends CI_Controller
21
+{
22
+
23
+    //TODO: PARAMETRIZAR BBDD -> SI EXISTE UN PROCESO EN MARCHA, NO INICIAR SIGUIENTE DESCARGA, AVISAR
24
+    protected $rutaDiaria = FCPATH . "DOWNLOADS/DAILY/";
25
+    protected $rutaDiariaFecha = "";
26
+
27
+    protected $rutaMensual = FCPATH . "DOWNLOADS/MONTH/";
28
+    protected $enlaceAtom = "";
29
+    protected $fechaCreacionLog = "";
30
+
31
+    public function __construct()
32
+    {
33
+        parent::__construct();
34
+        $this->load->helper('file');
35
+        $this->load->helper('xml');
36
+        $this->load->library('parserfile');
37
+
38
+        $this->load->model("Parser_model");
39
+    }
40
+
41
+    public function index()
42
+    {
43
+        $existCronRun = $this->Parser_model->existCronRun();
44
+        $fechaCarpeta = "";
45
+        if ($existCronRun['num_rows'] > 0) {
46
+            if ($existCronRun['datos']->cron_ejecucion == 0) {
47
+
48
+                $this->Parser_model->inicioParser();
49
+
50
+                $resultSetPerfiles = $this->Parser_model->getUrlArchivos();
51
+                $fechaCarpeta = date("Y-m-d_H-i-s");
52
+                $fechaImportacionLog = date("Y-m-d H:i:s");
53
+
54
+                foreach ($resultSetPerfiles['datos'] as $row) {
55
+
56
+                    $idUltimaImportacion = $this->Parser_model->setFechaNuevaImportacion($fechaImportacionLog);
57
+                    $this->fechaCreacionLog = $fechaCarpeta;
58
+                    $rutaPerfil = $this->rutaDiaria . "/" . $row->prefijos_tablas;
59
+
60
+                    $this->newFolder($rutaPerfil);
61
+
62
+                    if ($this->newFolder($rutaPerfil . "/" . $fechaCarpeta)) {
63
+                        $this->rutaDiariaFecha = $rutaPerfil . "/" . $fechaCarpeta . "/";
64
+                        $this->downloadProcess($row->url_descarga . ".atom");
65
+                    }
66
+
67
+                    $files = get_filenames($this->rutaDiariaFecha . "/format");
68
+                    emailInicioProcesoDescarga($fechaImportacionLog, $files);
69
+
70
+                    $this->parserfile->index($this->rutaDiariaFecha, $fechaCarpeta, $row, $idUltimaImportacion);
71
+                }
72
+
73
+                $this->tablasCalculadas($idUltimaImportacion);
74
+
75
+                emailFinProcesoDescarga($fechaImportacionLog);
76
+
77
+            } else {
78
+                echo "EXISTE UN CRON EN MARCHA.";
79
+            }
80
+        } else {
81
+            echo "No existe parametro que iniciar.";
82
+        }
83
+
84
+        $this->Parser_model->stopParser();
85
+
86
+        /* $resultSetPerfiles = $this->Parser_model->getUrlArchivos();
87
+    foreach ($resultSetPerfiles['datos'] as $row) {
88
+    $this->parserfile->index($this->rutaDiaria . "PER_PCSP_EX/2022-02-17_20-38-16", date("Y-m-d"), $row);
89
+    } */
90
+    }
91
+
92
+    private function downloadProcess($urlXML)
93
+    {
94
+
95
+        $fileName = basename($urlXML);
96
+        $this->downloadFile($fileName, $urlXML, $this->rutaDiariaFecha);
97
+        $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName);
98
+        $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
99
+
100
+        while ($nuevoAtomAttrNext !== "") {
101
+
102
+            $fileName = basename($nuevoAtomAttrNext);
103
+            $this->downloadFile($fileName, $nuevoAtomAttrNext, $this->rutaDiariaFecha);
104
+            $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName);
105
+
106
+            $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
107
+        }
108
+    }
109
+
110
+    private function readNextAttribute($archivo)
111
+    {
112
+        $enlace = "";
113
+        foreach ($archivo['link'] as $row) {
114
+            $attrNext = $row['@attributes']['rel'];
115
+            $attrHref = $row['@attributes']['href'];
116
+
117
+            if ($attrNext === "next") {
118
+                if (!strpos($attrHref, "licitacionesPerfilesContratanteCompleto3_2021")) { //"20220127_")) {
119
+                    $enlace = $attrHref;
120
+                } else {
121
+                    $enlace = "";
122
+                }
123
+                //$enlace = $attrHref;
124
+            }
125
+        }
126
+
127
+        return $enlace;
128
+    }
129
+
130
+    private function getFile($url, $fileName)
131
+    {
132
+
133
+        $feed = implode(file($url));
134
+        $xml = simplexml_load_string($feed);
135
+        $json = json_encode($xml);
136
+        $array = json_decode($json, true);
137
+
138
+        $this->formatXml($feed, $fileName);
139
+        return $array;
140
+    }
141
+
142
+    private function newFolder($rutaFinal)
143
+    {
144
+        $result = false;
145
+        if (!is_dir($rutaFinal)) {
146
+            $result = mkdir($rutaFinal);
147
+            mkdir($rutaFinal . "/format");
148
+        }
149
+
150
+        return $result;
151
+    }
152
+
153
+    private function formatXml($xml, $fileName)
154
+    {
155
+        $openingFormat = $this->regExIniciosEsquema($xml);
156
+        $closureFormat = $this->regExFinalesEsquema($openingFormat);
157
+        file_put_contents($this->rutaDiariaFecha . "/format/" . $fileName, $closureFormat);
158
+    }
159
+
160
+    private function downloadFile($fileName, $url, $ruta)
161
+    {
162
+        if (file_put_contents($ruta . $fileName, file_get_contents($url))) {
163
+            return true;
164
+        } else {
165
+            return false;
166
+        }
167
+    }
168
+
169
+    private function regExIniciosEsquema($xml)
170
+    {
171
+        $pattern = "/(<cac:)|(<cbc:)|(<cac-place-ext:)|(<cbc-place-ext:)/";
172
+        $result = preg_replace($pattern, "<", $xml);
173
+
174
+        return $result;
175
+    }
176
+
177
+    private function regExFinalesEsquema($xml)
178
+    {
179
+        $pattern = "/(<\/cac:)|(<\/cbc:)|(<\/cac-place-ext:)|(<\/cbc-place-ext:)/";
180
+        $result = preg_replace($pattern, "</", $xml);
181
+
182
+        return $result;
183
+    }
184
+
185
+    private function tablasCalculadas($idUltimaImportacion)
186
+    {
187
+        $this->Parser_model->truncateTables("vista_datos_licitacion");
188
+
189
+        $licitaciones = $this->Xlsexport_model->getLicitaciones($idUltimaImportacion);
190
+
191
+        insertBucleDuplicateOnKey($licitaciones['datos'], "vista_datos_licitacion", "");
192
+
193
+    }
194
+
195
+}
196
+
197
+/* End of file ParserCompleto.php */
198
+/* Location: ./application/controllers/ParserCompleto.php */