Created
October 2, 2010 15:23
-
-
Save k0001/607720 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf8 -*- | |
import re | |
import sys | |
import json | |
import logging | |
import random | |
from pprint import pprint | |
from django.db.models import ObjectDoesNotExist | |
from django.db.models import Q | |
from mediasancion.core.models import PartidoPolitico | |
from mediasancion.congreso.models import Proyecto, FirmaProyecto, Legislador, Mandato, Comision | |
logging.basicConfig(level=logging.INFO) | |
log = logging.getLogger(__name__) | |
add_queue = [] | |
def store_proyecto_item(x): | |
# [ | |
# "ProyectoItem", | |
# { | |
# "publicacion_fecha": "2010-03-22", | |
# "camara_origen": "d", | |
# "texto_mediasancion_diputados_url": "http://www1.hcdn.gov.ar/dependencias/dsecretaria/Periodo2010/PDF2010/SANCIONES/3306-D-2010.pdf", | |
# "tipo": "PROYECTO DE LEY", | |
# "resource_id": "1328-D-2010", | |
# "resource_source": "www1.hcdn.gov.ar", | |
# "camara_origen_expediente": "1328-D-2010", | |
# "texto_mediasancion_senadores_url": "http://www.senado.gov.ar/web/proyectos/verExpe.php?origen=S&nro_comision=&tipo=PL&numexp=286/10&tConsulta=4", | |
# "texto_completo_url": "http://www1.hcdn.gov.ar/proyxml/expediente.asp?fundamentos=si&numexp=1328-D-2010", | |
# "camara_revisora": null, | |
# "publicacion": "Tr\u00e1mite Parlamentario n\u00ba 19", | |
# "resource_url": "http://www1.hcdn.gov.ar/proyectos_search/proyectosd.asp?whichpage=1&proy_expdipN=1328&proy_expdipT=D&proy_expdipA=2010", | |
# "sumario": "CODIGO PENAL. MODIFICACION DEL ARTICULO 81, SOBRE INFANTICIDIO.", | |
# "id": "1328-D-2010", | |
# "comisiones_diputados": [ | |
# "LEGISLACION PENAL", | |
# "FAMILIA, MUJER, NI\u00d1EZ Y ADOLESCENCIA" | |
# ] | |
# } | |
# ] | |
try: | |
p, created = Proyecto.objects.get_or_create(codigo_expediente=x['camara_origen_expediente'], | |
camara_origen=x['camara_origen']) | |
except: | |
print x | |
raise | |
p.resource_source = x['resource_source'] | |
p.resource_url = x['resource_url'] | |
p.resource_id = x['resource_id'] | |
if 'LEY' in x['tipo']: | |
p.tipo = 'L' | |
elif 'RESOLUCION' in x['tipo']: | |
p.tipo = 'R' | |
elif 'DECLARACION' in x['tipo']: | |
p.tipo = 'D' | |
elif 'COMUNICACION' in x['tipo']: | |
p.tipo = 'C' | |
elif 'DECRETO' in x['tipo']: | |
p.tipo = 'E' | |
if 'MENSAJE' in x['tipo']: | |
p.mensaje = re.search('MENSAJE NRO: ([-\d]+)', x['tipo']).group(1) | |
else: | |
p.mensaje = '' | |
p.publicacion = x['publicacion'] or '' | |
p.publicacion_fecha = x['publicacion_fecha'] | |
p.texto_completo_url = x.get('texto_completo_url', '') | |
p.texto_mediasancion_senadores_url = x.get('texto_mediasancion_senadores_url', '') | |
p.texto_mediasancion_diputados_url = x.get('texto_mediasancion_diputados_url', '') | |
p.sumario = x['sumario'] | |
p.save() | |
for s in x.get('comisiones_diputados', ()): | |
s = s.capitalize() | |
try: | |
c = Comision.objects.get(camara='d', nombre__iexact=s) | |
except Comision.DoesNotExist: | |
c = Comision.objects.create(camara='d', nombre=s) | |
if not c in p.comisiones.all(): | |
p.comisiones.add(c) | |
for s in x.get('comisiones_senadores', ()): | |
s = s.capitalize() | |
try: | |
c = Comision.objects.get(camara='s', nombre__iexact=s) | |
except Comision.DoesNotExist: | |
c = Comision.objects.create(camara='s', nombre=s) | |
if not c in p.comisiones.all(): | |
p.comisiones.add(c) | |
if created: | |
log.info('Proyecto created %s' % p.uuid) | |
return True | |
else: | |
log.info('Proyecto updated %s' % p.uuid) | |
return True | |
def store_firmaproyecto_item(x): | |
# [ | |
# "FirmaProyectoItem", | |
# { | |
# "proyecto_expediente": "1328-D-2010", | |
# "firmante_partido_nombre": "UCR", | |
# "tipo_firma": "C", | |
# "resource_source": "www1.hcdn.gov.ar", | |
# "firmante_apellido": "Storni", | |
# "firmante_distrito_nombre": "C\u00f3rdoba", | |
# "resource_url": "http://www1.hcdn.gov.ar/proyectos_search/proyectosd.asp?whichpage=1&proy_expdipN=1328&proy_expdipT=D&proy_expdipA=2010", | |
# "id": "firma:1328-D-2010:STORNI, SILVIA", | |
# "firmante_nombre": "Silvia" | |
# } | |
# ] | |
try: | |
proyecto = Proyecto.objects.get(codigo_expediente=x['proyecto_expediente']) | |
if 'firmante_partido_nombre' in x: | |
partido = PartidoPolitico.objects.get(nombre=x['firmante_partido_nombre']) | |
elif 'EJECUTIVO' in x.get('firmante_poder', ''): | |
partido, created = PartidoPolitico.objects.get_or_create(nombre='000 PODER EJECUTIVO') | |
try: | |
legislador = Legislador.objects.get(apellido__iexact=x['firmante_apellido'], | |
nombre__iexact=x['firmante_nombre']) | |
except Legislador.DoesNotExist: | |
try: | |
legislador = Legislador.objects.get( | |
Q(nombre__icontains=x['firmante_nombre']) & | |
Q(nombre__icontains=x['firmante_apellido']) & | |
Q(apellido='')) | |
except Legislador.MultipleObjectsReturned: | |
pprint(x) | |
raise | |
# XXX No soportamos mas de un mandato por legislador... aún. | |
# El filtro es por precacución. | |
if proyecto.publicacion_fecha: | |
mandato = legislador.mandato_set.get(inicio__lte=proyecto.publicacion_fecha, | |
fin__gte=proyecto.publicacion_fecha) | |
else: | |
mandato = legislador.mandato_set.get() | |
mandato.partido = partido | |
mandato.save() | |
except ObjectDoesNotExist, e: | |
print e | |
return False | |
fp, created = FirmaProyecto.objects.get_or_create(proyecto=proyecto, mandato=mandato) | |
fp.tipo_firma = tipo_firma=x['tipo_firma'] | |
fp.save() | |
if created: | |
log.info('FirmaProyecto created %s' % fp.uuid) | |
return True | |
else: | |
log.info('FirmaProyecto updated %s' % fp.uuid) | |
return True | |
def store_item(t, x): | |
ts = { | |
'ProyectoItem': store_proyecto_item, | |
'FirmaProyectoItem': store_firmaproyecto_item } | |
try: | |
_store = ts[t] | |
except KeyError: | |
log.debug(u"Skiping %s" % t) | |
return | |
else: | |
if _store(x): | |
#log.info(u"Stored %s" % t) | |
return True | |
else: | |
log.info(u"Queueing %s" % t) | |
add_queue.append((t, x)) | |
return False | |
for line in sys.stdin: | |
store_item(*json.loads(line)) | |
while True: | |
if not add_queue: | |
log.info(u"Success!") | |
break | |
_t_add_queue, add_queue = tuple(add_queue), [] | |
nupdates = 0 | |
for i in _t_add_queue: | |
if store_item(*i): | |
nupdates += 1 | |
if nupdates == 0: | |
log.error(u"No item saved during last loop, aborting.") | |
break | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment