Source

urnlex / main.py

from google.appengine.ext import db, webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from django.utils import simplejson as json
import re, urllib, logging, urllib2

from models import CCID, CodeArticle

def quoteurnre(urlre):
    return urllib.quote(urlre, safe='/^$()[]?+.*{}\\')

def resolveetat(query):
    m = re.search(r'urn:lex:fr:etat:code:(.*?)(~art(.*?))?$', query)
    onlycode = 'propriete.intellectuelle'
    if not m or m.group(1) != onlycode:
        return None
    article = m.group(3)
    if article:
        article = article.lower()
        id = CodeArticle.all().filter('code = ', m.group(1)).filter('article = ', article).get()
        if not id:
            return None
        args = {'idArticle': id.reflegi}
        return 'http://www.legifrance.gouv.fr/affichCodeArticle.do?'+urllib.urlencode(args)
    else:
        args = {'cidText': 'LEGITEXT000006069414'}
        return 'http://www.legifrance.gouv.fr/affichCode.do?'+urllib.urlencode(args)

def resolvesenat(query):
    typemapping = {
            'proposition.loi': 'ppl',
            'projet.loi': 'pjl',
            'proposition.resolution': 'ppr'
    }
    m = re.search(r'urn:lex:fr:senat:(.*?):(\d{4});(\d+)$', query)
    if not m:
        return None
    year = int(m.group(2), 10) % 100
    id = int(m.group(3), 10)
    try:
        type = typemapping[m.group(1)]
    except KeyError:
        return None
    return "http://www.senat.fr/leg/%s%02d-%03d.html" % (type, year, id)

def resolvean(query):
    typemapping = {
            'proposition.loi': 'propositions/pion',
            'projet.loi': 'projets/pl',
    }
    m = re.search(r'urn:lex:fr:assemblee.nationale:(.*?):(\d+)\.legislature;(\d+)$', query)
    if not m:
        return None
    legi = int(m.group(2), 10)
    id = int(m.group(3), 10)
    try:
        type = typemapping[m.group(1)]
    except KeyError:
        return None
    return "http://www.assemblee-nationale.fr/%02d/%s%04d.asp" % (legi, type, id)

def resolvecc(query):
    m = re.search(r'urn:lex:fr:conseil.constitutionnel:decision:(\d{4})(-\d{2}-\d{2})?;(\d+[^;:]*?)$', query)
    if not m:
        return None
    year = int(m.group(1), 10)
    if year < 2000:
        ccid = '%02d-%s' % (year % 100, m.group(3))
    else:
        ccid = '%d-%s' % (year, m.group(3))
    ccid = ccid.lower()
    id = CCID.all().filter('num = ', ccid).get()
    if id is None:
        key = re.match(r'\d+-\d+', ccid).group(0)
        try:
            url = 'http://recherche.conseil-constitutionnel.fr/?expert=2&filtres3=%s' % key
            logging.info('fetching... %s' % url)
            search = urllib2.urlopen(url).read()
            htmlid = re.search(r'<span class="resultatRechercheTitre"><a href=".*?\.(\d+)\.html">', search).group(1)
            id = CCID(num=ccid, htmlid=htmlid)
            id.put()
        except:
            raise
            return None
    return "http://www.conseil-constitutionnel.fr/decision.%s.html" % id.htmlid

resolve = {
        'etat': resolveetat,
        'senat': resolvesenat,
        'assemblee.nationale': resolvean,
        'conseil.constitutionnel': resolvecc,
}

class Resolver(webapp.RequestHandler):
    def get(self):
        query = self.request.query_string
        m = re.search(r'urn:lex:fr:(.*?):', query)
        if not m:
            self.response.out.write('got path: %s' % self.request.path_qs)
            self.error(404)
            return

        try:
            url = resolve[m.group(1)](query)
        except KeyError:
            self.error(404)
            return
        if url is None:
            self.error(404)
            return
        self.redirect(url)

class Fallback(webapp.RequestHandler):
    def get(self):
        self.response.out.write('%s\n' % self.request.path_qs)

application = webapp.WSGIApplication(
    [(r'/redirect/?', Resolver),
     #(r'/.*', Fallback),
    ], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()