File size: 2,126 Bytes
7f4e4c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import re
import requests
import xml.etree.ElementTree as ET
from typing import List, Dict
from meteofrance_api import MeteoFranceClient

METEOFRANCE_API_URL = 'https://public-api.meteofrance.fr/public/DPBRA/v1/'
METEO_FRANCE_TOKEN = os.getenv('METEO_FRANCE_API_TOKEN')

def get_massifs_meteo_france() -> List[Dict]:
    """
    Fetch the list of massifs from Meteo France API.

    Returns:
        List[Dict]: List of massifs with their details.
    """
    url = METEOFRANCE_API_URL + 'liste-massifs'
    headers = {'apikey': METEO_FRANCE_TOKEN, 'accept': '*/*'}
    response = requests.get(url, headers=headers)
    response = response.json()
    liste_massifs = []
    for massif in response['features']:
        liste_massifs.append({
            "id": massif['properties']['code'],
            "nom": massif['properties']['title'],
            "groupe": massif['properties']['Departemen'],
        })
    return liste_massifs

def extraire_texte(element: ET.Element) -> str:
    """
    Extract all text from an XML element recursively.

    Args:
        element (ET.Element): XML element.

    Returns:
        str: Extracted text.
    """
    texte = element.text or ""
    for enfant in element:
        texte += extraire_texte(enfant)
    texte += element.tail or ""
    return texte

def get_massif_conditions(massif_id: str) -> str:
    """
    Fetch the weather conditions for a given massif.

    Args:
        massif_id (str): ID of the massif.

    Returns:
        str: Weather conditions in plain text.
    """
    url = METEOFRANCE_API_URL + 'massif/BRA'
    headers = {'apikey': METEO_FRANCE_TOKEN, 'accept': '*/*'}
    params = {'id-massif': massif_id, "format": "xml"}
    response = requests.get(url, headers=headers, params=params)
    xml_text = response.text
    root = ET.fromstring(xml_text)
    text = extraire_texte(root)
    #remove file names
    text = re.sub(r'\b[\w\-]+\.[a-zA-Z0-9]+\b', '', text).strip()
    return text

def get_forecast(latitude, longitude): 
    
    client = MeteoFranceClient(METEO_FRANCE_TOKEN)
    forecast = client.get_forecast(latitude, longitude)