Example #1
0
 def test_get_bra_xml_fail(self):
     responses.add(
         responses.GET,
         Config.BRA_BASE_URL + f"/BRA.CHABLAIS.20190101142328.xml",
         body="bla bla",
         status=302,
     )
     with pytest.raises(AssertionError) as e:
         get_bra_xml("CHABLAIS",
                     datetime.strptime("20190101142328", "%Y%m%d%H%M%S"))
     assert (
         str(e.value) ==
         "The bra for the massif CHABLAIS at day 2019-01-01 14:23:28 doesn't exist, status: 302"
     )
Example #2
0
def import_all_bra():
    """
    Same as `import_bra` but we request from March 2016 to now.
    """
    start_date = date(year=2016, month=3, day=10)
    date_range = [
        date.today() - timedelta(days=x)
        for x in range(0, (date.today() - start_date).days + 1)
    ]
    for d in date_range:
        massif = ""
        try:
            bra_dates = get_bra_date(d)
            with connection_scope() as con:
                for massif, m_date in bra_dates.items():
                    if not check_bra_record_exist(con, massif, m_date):
                        xml = get_bra_xml(massif, m_date)
                        processed_bra = process_xml(con, xml)
                        persist_bra(con, processed_bra)
                        click.echo(f"Persist {massif.capitalize()}")
        except Exception as e:
            log.debug(e)
            log.critical(
                f"an error occured when processing massif {massif} for date {d}"
            )
Example #3
0
    def test_get_bra_xml_work(self):
        with open(
                os.path.join(
                    CURRENT_DIR,
                    "test_data/BRA.CHABLAIS.20190101142328.xml")) as bra_xml:
            responses.add(
                responses.GET,
                Config.BRA_BASE_URL + f"/BRA.CHABLAIS.20190101142328.xml",
                body=bra_xml.read(),
                content_type="text/xml",
            )

            res = get_bra_xml(
                "CHABLAIS", datetime.strptime("20190101142328",
                                              "%Y%m%d%H%M%S"))
        assert isinstance(res, etree._ElementTree)
Example #4
0
def import_bra(bra_date):
    """
    * setup
    * request https://donneespubliques.meteofrance.fr/donnees_libres/Pdf/BRA/bra.%Y%m%d.json with all the date from december 2016 to today
    * if not 302 (302 means 404 at meteofrance 😭)
    * for all the date in all the json, download the xml of bra
    * process (download + post process)
    * import
    """
    bra_dates = get_bra_date(bra_date)
    with connection_scope() as con:
        for massif, m_date in bra_dates.items():
            try:
                if not check_bra_record_exist(con, massif, m_date):
                    xml = get_bra_xml(massif, m_date)
                    processed_bra = process_xml(con, xml)
                    persist_bra(con, processed_bra)
                    click.echo(f"Persist {massif.capitalize()}")
            except Exception as e:
                log.debug(e)
                log.critical(
                    f"an error occured when processing massif {massif} for date {m_date}"
                )