PageRenderTime 28ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/web/scripts2/basic.php

https://github.com/ciudadanointeligente/legacy-legislativo
PHP | 247 lines | 241 code | 6 blank | 0 comment | 2 complexity | d5bf7b7af7b03fa3f77c894659da77f4 MD5 | raw file
  1. <?php
  2. require('simple_html_dom.php');
  3. require("class/cnx.php");
  4. require("class/controlador.php");
  5. $coneccion = coneccion();
  6. $o = new Controlador();
  7. $sil_url = 'http://sil.senado.cl/cgi-bin/';
  8. $fechas_url = $sil_url.'sil_ultproy.pl';
  9. $proyectos_url = $sil_url.'sil_proyectos.pl?';
  10. $autores_url = $sil_url.'sil_autores.pl?';
  11. $array = array('Boletín','Título','Fecha de Ingreso','Iniciativa','Cámara de origen','Etapa','Subetapa','Tipo de proyecto','Urgencia actual');
  12. $arrayMeses = Array("", "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
  13. extract($_POST);
  14. $fechas_html = str_get_html(file_get_contents_curl($fechas_url,$desde,$hasta));
  15. $num=0;
  16. foreach($fechas_html->find('td[class="TEXTpais"]') as $nro)
  17. {
  18. $nro_boletin = trim(str_replace("&nbsp;","",$nro->plaintext));
  19. $html = file_get_html($proyectos_url.$nro_boletin);
  20. $value=htmlentities($html->find('tr', 1)->plaintext);
  21. $value=str_replace("&amp;", "&", $value);
  22. foreach($html->find('a') as $e)
  23. {
  24. if(strstr($e->onclick,'MM_openBrWindow') == TRUE)
  25. {
  26. $leytext= array_pop(array_reverse(explode("'",substr($e->onclick,17))));
  27. $data=array_pop(array_reverse(explode("&",substr($e->onclick,49+strlen(array_pop(explode("?",array_pop(array_reverse(explode("=",$e->onclick))))))))));
  28. }
  29. }
  30. $v=null;
  31. $i=0;
  32. foreach($array as $a){
  33. $v[$i]=strrpos($value, htmlentities(utf8_decode($a)));
  34. $i++;
  35. }
  36. sort($v);
  37. $v[]='';
  38. for($c=0; $c<$i; $c++){
  39. $x=$c+1;
  40. $cal=$v[$x]-$v[$c];
  41. if($c<8)
  42. $dato=substr($value, $v[$c], $cal);
  43. else
  44. $dato=substr($value, $v[$c]);
  45. $res=explode(":",$dato);
  46. $contenido=@trim(str_replace("&nbsp;","",trim($res[1])));
  47. if(strstr($res[0],'Bolet&iacute;n') == TRUE){
  48. $nro_boletin = $contenido;
  49. }
  50. if(strstr($res[0],'T&iacute;tulo') == TRUE){
  51. $titulo = $contenido;
  52. }
  53. if(strstr($res[0],'Fecha de Ingreso') != FALSE)
  54. {
  55. $fechaText = $contenido;
  56. $fechaArray = explode(' ', $fechaText);
  57. for($j=1; $j<=12; $j++)
  58. {
  59. if($arrayMeses[$j]==substr($fechaArray[3], 0, -1))
  60. {
  61. (strlen($j)==1) ? $mes = "0".$j : $mes = $j;
  62. break;
  63. }
  64. }
  65. if($fechaArray[1]<10)
  66. $fechaArray[1]="0".$fechaArray[1];
  67. $fecha_ingreso = $fechaArray[4]."-".$mes."-".$fechaArray[1];
  68. $desdeY = explode("/",$desde);
  69. }
  70. if (strstr($res[0],'Iniciativa') != FALSE)
  71. $iniciativa = $contenido;
  72. if (strstr($res[0],'Tipo de proyecto') != FALSE)
  73. $tipo = $contenido;
  74. if (strstr($res[0],'C&aacute;mara de origen') != FALSE)
  75. $camara_origen = $contenido;
  76. if (strstr($res[0],'Urgencia actual') != FALSE)
  77. $urgencia = $contenido;
  78. if (strstr($res[0],'Etapa') != FALSE)
  79. {
  80. $etapa = $contenido;
  81. if(strstr($etapa,'Tramitaci&oacute;n terminada') != FALSE)
  82. {
  83. if (substr_count($leytext, "?idLey=")==1) // Ley
  84. {
  85. $ley=$data;
  86. $etapa=trim(substr($etapa, 0,strlen(strstr($etapa, 'Ley'))));
  87. $ley_bcn = $leytext;
  88. $fecha_publicacion = substr($ley_bcn,strpos($ley_bcn,"&idVersion")+11);
  89. }
  90. else if(substr_count($leytext, "?idNorma=")) // Decreto
  91. {
  92. $decreto=$data;
  93. $etapa=trim(substr($etapa, 0,strlen(strstr($etapa, 'D.S.'))));
  94. $decreto_bcn = $leytext;
  95. $fecha_publicacion = substr($decreto_bcn,strpos($decreto_bcn,"&idVersion")+11);
  96. }
  97. }
  98. }
  99. if (strstr($res[0],'Subetapa') != FALSE)
  100. $sub_etapa = $contenido;
  101. if (strstr($res[0],'Refundido con') != FALSE)
  102. $refundido = trim($val->next_sibling()->plaintext); //falta insertar a BBDD
  103. }
  104. $id_proyecto_sil = $html->find('a', -1)->href;
  105. $id_proyecto_sil = substr($id_proyecto_sil,strpos($id_proyecto_sil,"?")+1);
  106. $id_proyecto_sil = str_replace(",","",$id_proyecto_sil);
  107. $qry="SELECT if(count(*)=0,0,1) value1 FROM ProyectoLey WHERE nro_boletin='$nro_boletin'";
  108. $o->setQry($qry);
  109. if(strlen($ley)==0)
  110. $ley=0;
  111. if(strlen($decreto)==0)
  112. $decreto=0;
  113. if($o->selectQ()==0)
  114. {
  115. $qry = "INSERT INTO
  116. ProyectoLey
  117. (
  118. nro_boletin,
  119. titulo,
  120. fecha_ingreso,
  121. iniciativa,
  122. tipo,
  123. camara_origen,
  124. urgencia,
  125. etapa,
  126. sub_etapa,
  127. ley,
  128. ley_bcn,
  129. decreto,
  130. decreto_bcn,
  131. fecha_publicacion,
  132. nro_interno,
  133. created_at,
  134. updated_at
  135. ) VALUES (
  136. '$nro_boletin',
  137. '$titulo',
  138. '$fecha_ingreso',
  139. '$iniciativa',
  140. '$tipo',
  141. '$camara_origen',
  142. '$urgencia',
  143. '$etapa',
  144. '$sub_etapa',
  145. $ley,
  146. '$ley_bcn',
  147. $decreto,
  148. '$decreto_bcn',
  149. '$fecha_publicacion',
  150. $id_proyecto_sil,
  151. NOW(),
  152. NOW()
  153. )";
  154. $o->setQry(html_entity_decode($qry));
  155. $o->execute();
  156. $select="SELECT id_proyecto_ley value1 FROM ProyectoLey
  157. WHERE nro_boletin='$nro_boletin' AND fecha_ingreso='$fecha_ingreso' ORDER BY id_proyecto_ley DESC LIMIT 1";
  158. $o->setQry($select);
  159. $idProyectoLey = $o->selectQ();
  160. $autores_html = file_get_html($autores_url.$id_proyecto_sil);
  161. foreach($autores_html->find('span[class="TEXTarticulo"]') as $autor)
  162. {
  163. $autor = str_replace("&nbsp;","",$autor->plaintext);
  164. $autor = explode(",",$autor);
  165. $nombre = trim($autor[1]);
  166. $apellidos = trim($autor[0]);
  167. $qry = "SELECT if(count(id_autor)>0,id_autor,0) value1 FROM Autor WHERE nombre='$nombre' AND apellidos='$apellidos'";
  168. $o->setQry($qry);
  169. $idAutor = $o->selectQ();
  170. if($idAutor>0)
  171. {
  172. //echo "<br>Autor ya existe: [".$idAutor."] ".$apellidos.", ".$nombre;
  173. }
  174. else
  175. {
  176. /* INSERT AUTOR */
  177. $qry = "INSERT INTO Autor (nombre, apellidos, created_at, updated_at) VALUES ('$nombre', '$apellidos', NOW(), NOW())";
  178. //echo "<br><br><strong>".$sql."</strong><br>";
  179. $o->setQry(html_entity_decode($qry));
  180. $o->execute();
  181. $qry = "SELECT id_autor value1 FROM Autor WHERE nombre='$nombre' AND apellidos='$apellidos' ORDER BY id_autor DESC LIMIT 1";
  182. $o->setQry($qry);
  183. $idAutor = $o->selectQ();
  184. //echo "<br>Creado nuevo autor: [".$idAutor."] ".$apellidos.", ".$nombre;
  185. }
  186. // busca si existe el la relacion Proyecto - autor
  187. $qry="SELECT if(count(id_autor)>0,1,0) value1 FROM AutorProyectoLey WHERE id_autor=$idAutor AND id_proyecto_ley=$idProyectoLey LIMIT 1";
  188. $o->setQry($qry);
  189. $value=$o->selectQ();
  190. if($value>0)
  191. {
  192. //echo "<br>Ya existe la relación autor: $idAutor proyecto: $idProyectoLey";
  193. }
  194. else
  195. {
  196. /* INSERT AUTOR-PROYECTO-LEY*/
  197. $sql = "INSERT INTO AutorProyectoLey (id_autor, id_proyecto_ley) VALUES ($idAutor, $idProyectoLey)";
  198. //echo "<br><br><strong>".$sql."</strong><br>";
  199. $o->setQry(html_entity_decode($qry));
  200. $o->execute();
  201. //echo "<br>Creada la relación autor: ".$idAutor." proyecto: ".$idProyectoLey;
  202. }
  203. }
  204. $html->clear();
  205. unset($html);
  206. $autores_html->clear();
  207. unset($autores_html);
  208. $num++;
  209. echo "<br><br>proyecto de ley #$nro_boletin insertado<br><br>";
  210. }
  211. else
  212. {
  213. echo "<br><br>Ya existe el proyecto de ley #$nro_boletin<br><br>";
  214. }
  215. echo "<hr />";
  216. }
  217. function file_get_contents_curl($url,$desde,$hasta)
  218. {
  219. $ch = curl_init();
  220. curl_setopt($ch, CURLOPT_HEADER, 0);
  221. curl_setopt($ch, CURLOPT_POST, 1);
  222. curl_setopt($ch, CURLOPT_POSTFIELDS, "desde=".$desde."&hasta=".$hasta."&buscar=%3E%3E+Buscar");
  223. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  224. curl_setopt($ch, CURLOPT_URL, $url);
  225. $data = curl_exec($ch);
  226. curl_close($ch);
  227. return $data;
  228. }
  229. ?>
  230. <META HTTP-EQUIV="refresh" CONTENT="2; url=http://www.ecodig.com">