base.py 3.73 KB
Newer Older
Nicolas Joyard committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
# -*- coding: utf-8 -*-

from builtins import filter
from datetime import datetime
import logging
import os
from zipfile import ZipFile

from bs4 import BeautifulSoup
import dateparser
import requests

from ..models import db, Job


class BaseJob(object):
    @property
    def job_name(self):
        raise NotImplementedError()

    @property
    def job(self):
        if not self._job:
            self._job = self.get_or_create(Job, nom=self.job_name)

        return self._job

    def __init__(self, app):
        self.app = app
        self._job = None

    def debug(self, msg):
        self.app.logger.debug('<%s> %s' % (self.job_name, msg))

    def info(self, msg):
        self.app.logger.info('<%s> %s' % (self.job_name, msg))

    def warn(self, msg):
        self.app.logger.warn('<%s> %s' % (self.job_name, msg))

    def error(self, msg):
        self.app.logger.error('<%s> %s' % (self.job_name, msg))

    def get_or_create(self, model, **kwargs):
        item = model.query.filter_by(**kwargs).first()
        if not item:
            item = model(**kwargs)
            db.session.add(item)

        return item

    def update_status(self, status=None, file=None, filedate=None):
        job = self.job
        job.date_exec = datetime.now()
        job.resultat = status or ''

        if file:
            job.url_fichier = file
        if filedate:
            job.date_fichier = filedate

        db.session.commit()


class BaseANJob(BaseJob):
    base_url = 'http://data.assemblee-nationale.fr'

    def __init__(self, app, url):
        super(BaseANJob, self).__init__(app)
        self.url = 'http://data.assemblee-nationale.fr%s' % url

    def parse_json(self, json_filename, json_stream):
        raise NotImplementedError()

    def run(self, ignore_lmd=False):
        self.info(u'Téléchargement %s' % self.url)

        soup = BeautifulSoup(requests.get(self.url).content, 'html5lib')

        def match_link(a):
            return a['href'].endswith('.json.zip')

        try:
            link = next(filter(match_link, soup.select('a[href]')))
        except:
            self.error(u'Lien vers dump JSON introuvable')
            self.update_status('error:json-link')
            return

        jsonzip_url = link['href']
        if jsonzip_url.startswith('/'):
            jsonzip_url = '%s%s' % (self.base_url, jsonzip_url)

        self.info(u'URL JSON zippé : %s' % jsonzip_url)

        try:
            lastmod = requests.head(jsonzip_url).headers['Last-Modified']
        except:
            self.error(u'Date du dump JSON introuvable')
            self.update_status('error:json-lastmod')
            return

        self.info(u'Date modification JSON zippé: %s' % lastmod)

        jsonzip_lmd = dateparser.parse(lastmod)
        if not ignore_lmd:
            if self.job.date_fichier and self.job.date_fichier >= jsonzip_lmd:
                self.info(u'JSON zippé non modifié')
                self.update_status('ok')
                return

        self.info(u'Téléchargement JSON zippé')

        localzip = os.path.join(self.app.config['DATA_DIR'],
                                os.path.basename(jsonzip_url))

        with open(localzip, 'wb') as out:
            r = requests.get(jsonzip_url, stream=True)
            for block in r.iter_content(1024):
                out.write(block)

        with ZipFile(localzip, 'r') as z:
            for f in [f for f in z.namelist() if f.endswith('.json')]:
                self.info(u'JSON extrait : %s' % f)
                with z.open(f) as zf:
                    try:
                        self.parse_json(f, zf)
                    except Exception, e:
                        self.error(u'Erreur: %s' % e)

        self.info(u'Job terminé')
        self.update_status('ok', jsonzip_url, jsonzip_lmd)