File size: 1,633 Bytes
da4b152
 
 
 
 
 
 
 
 
 
 
 
9ec431e
221e186
 
da4b152
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import json
import datetime
from pprint import pprint   
from datetime import date
from dotenv import load_dotenv
import os

load_dotenv()


def get_weather(city, date):
    
    agro_api_key='88aaa872e29fedb008a2ffd240ebdf56'
    opencage_api_key='bade196e8eac4d33b72dba6d9e40285a'

    # On récupère les lat et lon de la ville
    geocode_url = f"https://api.opencagedata.com/geocode/v1/json?q={city}&key={opencage_api_key}"
    geocode_response = requests.get(geocode_url)
    geocode_data = geocode_response.json()

    
    latitude = geocode_data['results'][0]['geometry']['lat']
    longitude = geocode_data['results'][0]['geometry']['lng']

    # On demande la météo pour cette ville
    forecast_url = f"http://api.agromonitoring.com/agro/1.0/weather/forecast?lat={latitude}&lon={longitude}&appid={agro_api_key}"
    forecast_response = requests.get(forecast_url)

    if forecast_response.status_code == 200:
        forecast_data = forecast_response.json()
        d={}
        for forecast in forecast_data:
            datew = datetime.datetime.fromtimestamp(forecast['dt'])
            temperature = forecast['main']['temp']
            weather_description = forecast['weather'][0]['description']

            if str(datew).split(' ')[0]==str(date) :
                d[str(datew)]=f"Location : {city}, Temperature: {round(temperature-273.15,2)}°C, Weather: {weather_description}"
        return d, latitude, longitude
    else:
        print(f"Error: {forecast_response.status_code}")

# #exemple
# today = date.today()
# today = today.strftime("%Y-%m-%d")
# a=get_weather('Paris',today)
# print(a)