Skip to content

Commit

Permalink
updating seguimientos only when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
aniversarioperu committed Sep 25, 2014
1 parent e7c76b0 commit 866ed96
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 6 deletions.
1 change: 1 addition & 0 deletions proyectos_de_ley/pdl/management/commands/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ def get_seguimientos(self, soup):
:param: BeautifulSoup object
:return: list of tuples (date object, event)
"""
events = ''
for i in soup.find_all(width='112'):
if i.text == 'Seguimiento:':
events = i.next_sibling.text
Expand Down
39 changes: 33 additions & 6 deletions proyectos_de_ley/pdl/management/commands/update_seguimientos.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.core.management.base import BaseCommand

from pdl.management.commands.scraper import Command as ScraperCommand
from pdl.models import Proyecto
from pdl.models import Proyecto, Seguimientos


class Command(ScraperCommand):
Expand All @@ -19,17 +19,44 @@ class Command(ScraperCommand):
help='Use when running tests to stop after one iteration.',
),
)

def handle(self, *args, **options):
self.tor = False
self.mysocket = ""

proyectos = Proyecto.objects.all()
for i in proyectos:
codigo = i.codigo
soup = self.get(i.seguimiento_page)
events = self.get_seguimientos(soup)

self.save_seguimientos(events, codigo)
if self.is_law(i.codigo) is True:
self.stdout.write('Nothing to update for %s.' % str(i.codigo))
else:
soup = self.get(i.seguimiento_page)
events = self.get_seguimientos(soup)

self.save_seguimientos(events, codigo)

if options['test'] is True:
break

def is_law(self, codigo):
"""Check if this project is already a `law` in our database model
Seguimiento.
:param codigo:
:return: True or False
"""
proyecto = Proyecto.objects.filter(codigo=codigo)[0]
items = Seguimientos.objects.filter(proyecto=proyecto)

promulgado, publicado = False, False
for i in items:
if 'promulgado' in i.evento.lower():
promulgado = True
if 'publicado' in i.evento.lower():
publicado = True

if options['test'] is True:
break
if promulgado is True and publicado is True:
return True
else:
return False
29 changes: 29 additions & 0 deletions proyectos_de_ley/pdl/tests/test_update_seguimientos_cmd.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# -*- encoding: utf-8 -*-
import codecs
from datetime import date
import os

from bs4 import BeautifulSoup

from django.test import TestCase

Expand Down Expand Up @@ -40,3 +44,28 @@ def test_handle(self):
self.cmd.handle(self, *args, **options)
seguimientos = Seguimientos.objects.all()
self.assertGreater(len(seguimientos), 1)

def test_is_law(self):
codigo = '00002'
# save an item to our test database
item = {
'codigo': '00002',
'seguimiento_page': 'dummy',
'fecha_presentacion': date.today(),
}
b = Proyecto(**item)
b.save()

this_folder = os.path.abspath(os.path.dirname(__file__))
html_folder = os.path.join(this_folder, 'update_seguimientos')
html_file = os.path.join(html_folder, codigo + '.html')
with codecs.open(html_file, 'r', 'latin-1') as f:
soup = BeautifulSoup(f.read())

# save its seguimientos
events = self.cmd.get_seguimientos(soup)
self.cmd.save_seguimientos(events, codigo)

# check if it is law so that we don't need to update it
result = self.cmd.is_law(codigo)
self.assertEqual(True, result)
128 changes: 128 additions & 0 deletions proyectos_de_ley/pdl/tests/update_seguimientos/00002.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Seguimiento</title>
<script language="JavaScript" type="text/javascript">
<!--
var pathname = window.location.pathname;filename = pathname.substring(0,(pathname.lastIndexOf('nsf')+4)) function opendoc(){// document.forms[0].submit()// var d = document.forms[0]// var numero = d.Codlni.value; alert("Prompt mensaje de texto " + Codlni ); var newWindow = window.open('http://w2kdesa/Paracas/textoProyectos2001.nsf/PorNumero?OpenView&RestrictToCategory=0987' )}
// -->
</script>

<script language="JavaScript" type="text/javascript">
<!--
document._domino_target = "_self";
function _doClick(v, o, t) {
var returnValue = false;
var url="/Sicr/TraDocEstProc/CLProLey2011.nsf/0/35d1748d19fd8e4c052578e200057f2a?OpenDocument&Click=" + v;
if (o.href != null) {
o.href = url;
returnValue = true;
} else {
if (t == null)
t = document._domino_target;
window.open(url, t);
}
return returnValue;
}
// -->
</script>
</head>
<body text="#000000" bgcolor="#FFFFFF">

<form action=""><link href="http://www.congreso.gob.pe/css/newclass.css" rel="stylesheet" type="text/css">
<!--link href="http://www.congreso.gob.pe/proyectos_ley/stylesheets/estilos.css" rel="stylesheet" type="text/css"-->
<div id="titulo03"><span id="titulo03"></span><br></div><u><font size="5" face="Calibri">Ficha de Seguimiento, &quot;Proyecto de Ley </font></u><u><font size="5" face="Calibri">00002/2011-CR</font></u><u><font size="4" face="Calibri"> &quot;</font></u>
<p>
<table align="left" border="0" cellspacing="0" cellpadding="0">
<tr valign="top"><td width="594"><div align="right"><a href="" onclick="return _doClick('052578A000570BA8.eb71d0cf91d8294e05256cdf006b5706/$Body/0.126A', this, null)"><u><font size="4" color="#0000FF" face="Arial">Ver Expediente Digital</font></u></a></div>
<table width='630', align="left", border=1,height="2", bordercolor="#6583A0", cellpadding = "2", cellspacing = "1" border="1">
<tr valign="top"><td width="112"><font size="4" face="Calibri">Período:</font></td><td width="151"><font size="4" face="Calibri">Periodo de Gobierno 2011 - 2016</font><font size="4" face="Calibri">.</font></td><td width="113"><font size="4" face="Calibri">Legislatura:</font></td><td width="181"><font size="4" face="Calibri">Primera Legislatura Ordinaria 2011</font></td></tr>

<tr valign="top"><td width="112"><font size="4" face="Calibri">Número:</font></td><td width="151"><font size="4" face="Calibri">00002/2011-CR</font><font size="4" face="Calibri"> </font></td><td width="113"><font size="4" face="Calibri">Fecha Presentación:</font></td><td width="181"><font size="4" face="Calibri">08/03/2011</font></td></tr>

<tr valign="top"><td width="112"><font size="4" face="Calibri">Proponente:</font></td><td width="446" colspan="3"><font size="4" face="Calibri">Congreso</font></td></tr>

<tr valign="top"><td width="112"><font size="4" face="Calibri">Grupo Parlamentario:</font></td><td width="446" colspan="3"><font size="4" face="Calibri">Nacionalista Gana Perú</font></td></tr>

<tr valign="top"><td width="112"><font size="4" face="Calibri">Título:</font></td><td width="446" colspan="3"><font size="4" face="Calibri">LEY QUE DEROGA LA LEY N° 29760</font></td></tr>

<tr valign="top"><td width="112"><font size="4" face="Calibri">Sumilla:</font></td><td width="446" colspan="3"><font size="4" face="Calibri">Propone derogar la Ley N° 29760, Ley que declara de necesidad pública e interés nacional la ejecución del proyecto de trasvase del río Marañón y el represamiento y la derivación del río Huallaga para fines hidroenergético y agrícola.</font></td></tr>

<tr valign="top"><td width="112"><font size="4" face="Calibri">Autores </font><font size="4" color="#0000FF" face="Calibri">(*)</font><font size="4" face="Calibri">:</font></td><td width="446" colspan="3"><font size="4" face="Calibri"><font size="4" face="Calibri"> </font><a href=mailto:visla@congreso.gob.pe>Isla Rojas Víctor</a></a>,<a href=mailto:jurquizo@congreso.gob.pe>Urquizo Maggia José Antonio</a></a>,<a href=mailto:marivas@congreso.gob.pe>Rivas Texeira Martín Amado</a></a>,<a href=mailto:tzamudio@congreso.gob.pe>Zamudio Briceño Tomás Martín</a></a>,<a href=mailto:tgamarra@congreso.gob.pe>Gamarra Saldivar Teofilo</a></a>,<a href=mailto:jreynaga@congreso.gob.pe>Reynaga Soto Jhon Arquimides</a></a>,<a href=mailto:jrimarachin@congreso.gob.pe>Rimarachin Cabrera Jorge Antonio</a></a><font size="4" face="Calibri"> </font></font></td></tr>

<tr valign="top"><td width="112"><font size="4" face="Calibri">Adherentes</font><font size="4" color="#0000FF" face="Calibri">(**)</font><font size="4" face="Calibri">:</font></td><td width="446" colspan="3"><font size="4" face="Calibri">Saavedra Vela Esther,Yrupailla Montes Cesar Elmer</font></td></tr>

<tr valign="top"><td width="112"><font size="4" face="Calibri">Seguimiento</font><font size="4" face="Calibri">:</font></td><td width="446" colspan="3"><font size="4" face="Calibri"><font size="4" face="Calibri"> </font>15/08/2011 Decretado a... Agraria<br>
17/08/2011 Oficio - 035-2011-2012-ESV/CR solicita exoneración de envío a Comisiones.-<br>
17/08/2011 En comisión Agraria<br>
17/08/2011 Oficio - 035-2011-2012-ESV-CR, Cong. Saavedra Vela, solicita exoneración trámite envío a Comisiones<br>
07/10/2011 Oficio - 083-2011-2012/VIR-CR, Cong. Isla Rojas, solicita exoneración envío a Comisiones<br>
13/10/2011 Ac.Portavoces - acordó exoneración dictamen de Com. Agraria, ampliación de Agenda y prioridad debate<br>
13/10/2011 Orden del Día<br>
13/10/2011 Aprobado 1ra. votación<br>
13/10/2011 Dispensado 2da Votación - por Acuerdo del Pleno<br>
14/10/2011 Autógrafa Sobre Nº: 18 - vence: 08.11.2011.<br>
15/10/2011 Promulgado Ley Nº: 29791<br>
16/10/2011 Publicado - Ley N° 29791/ al Archivo 06.07.2012 (53 folios)<font size="4" face="Calibri"> </font></font></td></tr>

<tr valign="top"><td width="112"><font size="4" face="Calibri">Iniciativas Agrupadas:</font></td><td width="446" colspan="3"><font size="4" face="Calibri">00011, 00036, 00161</font></td></tr>

<tr valign="top"><td width="112"><font size="4" face="Calibri">Número de Ley:</font></td><td width="446" colspan="3"><font size="4" face="Calibri">Ley Nº: 29791</font></td></tr>

<tr valign="top"><td width="112"><font size="4" face="Verdana">Título de la Ley:</font></td><td width="446" colspan="3"><font size="4" face="Calibri">LEY QUE DEROGA LA LEY 29760, LEY QUE DECLARA DE NECESIDAD PÚBLICA E INTERÉS NACIONAL LA EJECUCIÓN DEL PROYECTO DE TRASVASE DEL RÍO MARAÑÓN Y EL REPRESAMIENTO Y LA DERIVACIÓN DEL RÍO HUALLAGA PARA FINES HIDROENERGÉTICO Y AGRÍCOLA.</font></td></tr>

<tr valign="top"><td width="112"><font size="4">Sumilla de la Ley</font></td><td width="446" colspan="3"><font size="4" face="Calibri"><font size="4" face="Calibri"> </font>Propone derogar la Ley N° 29760, Ley que declara de necesidad pública e interés nacional la<br>
ejecución del proyecto de trasvase del río Marañón y el represamiento y la derivación del río<br>
Huallaga para fines hidroenergéticos y agrícolas.</font></td></tr>
</table>
</td></tr>

<tr valign="top"><td width="594"><br><br>
<font size="2" color="#0000FF" face="Times New Roman">(*) Proyectos firmados por el Congresista</font><br>
<font size="2" color="#0000FF" face="Times New Roman">(**) Proyectos de otros Congresistas a los que se ha adherido, son independientes de los Proyectos presentados por cada Congresista</font><br>
<font size="2" color="#0000FF" face="Times New Roman">(***) El último estado corresponde al último movimiento del Proyecto de Ley. Para los Proyectos que han sido asignados a mas de una comisión el último estado puede variar. Por favor verifique la información en el seguimiento del proyecto.</font></td></tr>
</table>
<div align="center"></div>
<input name="CodIni_web_1" type="hidden" value="00002/2011-CR">
<input name="DesGrupPol" type="hidden" value="Gana Perú">
<input name="Distrito" type="hidden" value=",,,,UCAYALI,APURIMAC,CAJAMARCA">
<input name="fechapre" type="hidden" value="03/08/2011">
<input name="Promulgado" type="hidden" value="C">
<input name="contex" type="hidden" value="1">
<input name="conquechua" type="hidden" value="0">
<input name="conexp" type="hidden" value="1">
<input name="DesPerio" type="hidden" value="Periodo de Gobierno 2011 - 2016">
<input name="DesLegis" type="hidden" value="Primera Legislatura Ordinaria 2011">
<input name="CodIni" type="hidden" value="00002">
<input name="CodIni_web" type="hidden" value="00002/2011-CR">
<input name="FecPres" type="hidden" value="08/03/2011">
<input name="DesPropo" type="hidden" value="Congreso">
<input name="DesGrupParla" type="hidden" value="Nacionalista Gana Perú">
<input name="TitIni" type="hidden" value="LEY QUE DEROGA LA LEY N° 29760">
<input name="SumIni" type="hidden" value="Propone derogar la Ley N° 29760, Ley que declara de necesidad pública e interés nacional la ejecución del proyecto de trasvase del río Marañón y el represamiento y la derivación del río Huallaga para fines hidroenergético y agrícola.">
<input name="NomCongre" type="hidden" value="Isla Rojas Víctor,Urquizo Maggia José Antonio,Rivas Texeira Martín Amado,Zamudio Briceño Tomás Martín,Gamarra Saldivar Teofilo,Reynaga Soto Jhon Arquimides,Rimarachin Cabrera Jorge Antonio">
<input name="NomCongre_mail" type="hidden" value="[&lt;a href=mailto:visla@congreso.gob.pe&gt;]Isla Rojas Víctor[&lt;/a&gt;][&lt;/a&gt;],[&lt;a href=mailto:jurquizo@congreso.gob.pe&gt;]Urquizo Maggia José Antonio[&lt;/a&gt;][&lt;/a&gt;],[&lt;a href=mailto:marivas@congreso.gob.pe&gt;]Rivas Texeira Martín Amado[&lt;/a&gt;][&lt;/a&gt;],[&lt;a href=mailto:tzamudio@congreso.gob.pe&gt;]Zamudio Briceño Tomás Martín[&lt;/a&gt;][&lt;/a&gt;],[&lt;a href=mailto:tgamarra@congreso.gob.pe&gt;]Gamarra Saldivar Teofilo[&lt;/a&gt;][&lt;/a&gt;],[&lt;a href=mailto:jreynaga@congreso.gob.pe&gt;]Reynaga Soto Jhon Arquimides[&lt;/a&gt;][&lt;/a&gt;],[&lt;a href=mailto:jrimarachin@congreso.gob.pe&gt;]Rimarachin Cabrera Jorge Antonio[&lt;/a&gt;][&lt;/a&gt;]">
<input name="NomCongreCoautor" type="hidden" value="">
<input name="Adherentes" type="hidden" value="Saavedra Vela Esther,Yrupailla Montes Cesar Elmer">
<input name="Adherentes_mail" type="hidden" value="[&lt;a href=mailto:esaavedra@congreso.gob.pe&gt;]Saavedra Vela, Esther[&lt;/a&gt;][&lt;/a&gt;];[&lt;a href=mailto:cyrupailla@congreso.gob.pe&gt;]Yrupailla Montes, Cesar Elmer[&lt;/a&gt;][&lt;/a&gt;]">
<input name="CodIniSecu" type="hidden" value="00011, 00036, 00161">
<input name="NumLey" type="hidden" value="Ley Nº: 29791">
<input name="NumLeyWeb" type="hidden" value="[&lt;b&gt;&lt;a href=http://www.congreso.gob.pe/ntley/imagenes/Leyes/29791.pdf&gt;]Ver Ley Nº: 29791[&lt;/a&gt;&lt;/b&gt;] 10/16/2011 LEY QUE DEROGA LA LEY 29760, LEY QUE DECLARA DE NECESIDAD PÚBLICA E INTERÉS NACIONAL LA EJECUCIÓN DEL PROYECTO DE TRASVASE DEL RÍO MARAÑÓN Y EL REPRESAMIENTO Y LA DERIVACIÓN DEL RÍO HUALLAGA PARA FINES HIDROENERGÉTICO Y AGRÍCOLA.">
<input name="TitLey" type="hidden" value="LEY QUE DEROGA LA LEY 29760, LEY QUE DECLARA DE NECESIDAD PÚBLICA E INTERÉS NACIONAL LA EJECUCIÓN DEL PROYECTO DE TRASVASE DEL RÍO MARAÑÓN Y EL REPRESAMIENTO Y LA DERIVACIÓN DEL RÍO HUALLAGA PARA FINES HIDROENERGÉTICO Y AGRÍCOLA.">
<input name="CodUltEsta" type="hidden" value="Publicado El Peruano">
<input name="FecUltimo" type="hidden" value="10/16/2011">
<input name="DesComi" type="hidden" value="Agraria">
<input name="DesSegui2_1" type="hidden" value="">
<input name="NombreDelEstado" type="hidden" value="Si">
<input name="NombreDelEnlace" type="hidden" value="http://www2.congreso.gob.pe/Sicr\TraDocEstProc\CLProLey2011.nsf/B53C83ABAC4D7A2105256CDF006C92C9/35D1748D19FD8E4C052578E200057F2A?OpenDocument">
<input name="NombreDelFormulario" type="hidden" value="Proyectos de Ley">
<input name="NombreDeLaBaseDeDatos" type="hidden" value="">
<input name="NombreDeLaComision" type="hidden" value="Comisión Agraria">
<input name="NombreDelPeriodoParlamentario" type="hidden" value="2006-2011">
<input name="_59pnmqojicl26ar21khnkopb7d5pmoobkd5r6u_" type="hidden" value="">
<input name="Fecha_1" type="hidden" value="03/08/2011">
<input name="Titulo" type="hidden" value="Propone derogar la Ley N° 29760, Ley que declara de necesidad pública e interés nacional la ejecución del proyecto de trasvase del río Marañón y el represamiento y la derivación del río Huallaga para fines hidroenergético y agrícola.">
<input name="NombredelGrupoParlamentario" type="hidden" value="Nacionalista Gana Perú">
<input name="NumeroDeProyectoLey" type="hidden" value="00002"></form>
</body>
</html>
Expand Down

0 comments on commit 866ed96

Please sign in to comment.