deplacer check dans la bonne function

This commit is contained in:
Simon 2025-10-02 10:31:55 +01:00
parent f06b59e278
commit 1631c7aa20

View file

@ -1,4 +1,4 @@
getStationData <- function(start_date, end_date, station_id,headers = headers,dpclim = "public/DPClim/v1/",timesleep=5,base="https://public-api.meteofrance.fr") { getStationData <- function(start_date, end_date, station_id,headers ,dpclim = "public/DPClim/v1/",timesleep=5,base="https://public-api.meteofrance.fr") {
start_date <- as.Date(start_date) start_date <- as.Date(start_date)
end_date <- as.Date(end_date) end_date <- as.Date(end_date)
url <- modify_url(base,path = paste0(dpclim,"commande-station/infrahoraire-6m")) url <- modify_url(base,path = paste0(dpclim,"commande-station/infrahoraire-6m"))
@ -8,21 +8,29 @@ getStationData <- function(start_date, end_date, station_id,headers = headers,dp
parameters <- list("id-station" = station_id, "date-deb-periode" = formatted_start_date, "date-fin-periode" = formatted_end_date) parameters <- list("id-station" = station_id, "date-deb-periode" = formatted_start_date, "date-fin-periode" = formatted_end_date)
# Create the complete URL with parameters # Create the URL with parameters to ask for the csv file
url_with_params <- modify_url(url, query = parameters) url_query_file <- modify_url(url, query = parameters)
response <- request_api(url_query_file, headers=headers,code=202)
# Add headers
# Make the GET request
response <- request_api(url_with_params, headers,code=202)
if(response$status_code == 202){ if(response$status_code == 202){
url <- modify_url(base,path = paste0(dpclim,"commande/fichier")) url_fichier <- modify_url(base,path = paste0(dpclim,"commande/fichier"))
parameters <- list("id-cmde" = content(response)[[1]][[1]] ) parameters <- list("id-cmde" = content(response)[[1]][[1]] )
url_with_params <- modify_url(url, query = parameters) print(paste("Waiting for file",parameters[["id-cmde"]],"wait",timesleep,"secs"))
response <- request_api(url_with_params, headers,code=c(204,201)) Sys.sleep(timesleep)
while(response$status_code == 204){ url_get_file <- modify_url(url_fichier, query = parameters)
print("file not finished yet, wait 5s") response <- request_api(url_get_file, headers=headers,code=c(204,201,500))
while(response$status_code %in% c(204,500)){
print("file not ready yet, wait a minute more")
Sys.sleep(60) Sys.sleep(60)
response <- request_api(url_with_params, headers,code=c(204,201)) response <- request_api(url_get_file, headers=headers,code=c(204,201,500))
if(response$status_code == 500){
print("problem with file generation, retry")
response <- request_api(url_query_file, headers=headers,code=202)
parameters <- list("id-cmde" = content(response)[[1]][[1]] )
print(paste("Waiting for file",parameters[["id-cmde"]],"wait 25 sec"))
Sys.sleep(25)
url_get_file <- modify_url(url_fichier, query = parameters)
response <- request_api(url_get_file, headers=headers,code=c(204,201,500))
}
} }
if(response$status_code == 201){ if(response$status_code == 201){
return(read.csv(text=content(response,as="text")[[1]],sep=";",header=T,dec = ",")) return(read.csv(text=content(response,as="text")[[1]],sep=";",header=T,dec = ","))
@ -33,3 +41,131 @@ getStationData <- function(start_date, end_date, station_id,headers = headers,dp
} }
getStationDataDPObs <- function(start_date, end_date, station_id,headers ,dpclim = "public/DPClim/v1/",timesleep=5,base="https://public-api.meteofrance.fr") {
start_date <- as.Date(start_date)
end_date <- as.Date(end_date)
url <- modify_url(base,path = paste0(dpclim,"commande-station/infrahoraire-6m"))
# Format dates in ISO8601 format
formatted_start_date <- format(start_date, "%Y-%m-%dT00:00:00Z", tz = "GMT")
formatted_end_date <- format(end_date, "%Y-%m-%dT00:00:00Z", tz = "GMT")
parameters <- list("id-station" = station_id, "date-deb-periode" = formatted_start_date, "date-fin-periode" = formatted_end_date)
# Create the URL with parameters to ask for the csv file
url_query_file <- modify_url(url, query = parameters)
response <- request_api(url_query_file, headers=headers,code=202)
if(response$status_code == 202){
url_fichier <- modify_url(base,path = paste0(dpclim,"commande/fichier"))
parameters <- list("id-cmde" = content(response)[[1]][[1]] )
print(paste("Waiting for file",parameters[["id-cmde"]],"wait",timesleep,"secs"))
Sys.sleep(timesleep)
url_get_file <- modify_url(url_fichier, query = parameters)
response <- request_api(url_get_file, headers=headers,code=c(204,201,500))
while(response$status_code %in% c(204,500)){
print("file not ready yet, wait a minute more")
Sys.sleep(60)
response <- request_api(url_get_file, headers=headers,code=c(204,201,500))
if(response$status_code == 500){
print("problem with file generation, retry")
response <- request_api(url_query_file, headers=headers,code=202)
parameters <- list("id-cmde" = content(response)[[1]][[1]] )
print(paste("Waiting for file",parameters[["id-cmde"]],"wait 25 sec"))
Sys.sleep(25)
url_get_file <- modify_url(url_fichier, query = parameters)
response <- request_api(url_get_file, headers=headers,code=c(204,201,500))
}
}
if(response$status_code == 201){
return(read.csv(text=content(response,as="text")[[1]],sep=";",header=T,dec = ","))
}
}
print("Failed to retrieve data from the API. Why? not sure..")
return(NULL)
}
getStationDataTemp <- function(start_date, end_date, station_id,headers ,dpclim = "public/DPClim/v1/",timesleep=5,base="https://public-api.meteofrance.fr") {
start_date <- as.Date(start_date)
end_date <- as.Date(end_date)
url <- modify_url(base,path = paste0(dpclim,"commande-station/infrahoraire-6m"))
# Format dates in ISO8601 format
formatted_start_date <- format(start_date, "%Y-%m-%dT00:00:00Z", tz = "GMT")
formatted_end_date <- format(end_date, "%Y-%m-%dT00:00:00Z", tz = "GMT")
parameters <- list("id-station" = station_id, "date-deb-periode" = formatted_start_date, "date-fin-periode" = formatted_end_date)
# Create the URL with parameters to ask for the csv file
url_query_file <- modify_url(url, query = parameters)
response <- request_api(url_query_file, headers=headers,code=202)
if(response$status_code == 202){
url_fichier <- modify_url(base,path = paste0(dpclim,"commande/fichier"))
parameters <- list("id-cmde" = content(response)[[1]][[1]] )
print(paste("Waiting for file",parameters[["id-cmde"]],"wait",timesleep,"secs"))
Sys.sleep(timesleep)
url_get_file <- modify_url(url_fichier, query = parameters)
response <- request_api(url_get_file, headers=headers,code=c(204,201,500))
while(response$status_code %in% c(204,500)){
print("file not ready yet, wait a minute more")
Sys.sleep(60)
response <- request_api(url_get_file, headers=headers,code=c(204,201,500))
if(response$status_code == 500){
print("problem with file generation, retry")
response <- request_api(url_query_file, headers=headers,code=202)
parameters <- list("id-cmde" = content(response)[[1]][[1]] )
print(paste("Waiting for file",parameters[["id-cmde"]],"wait 25 sec"))
Sys.sleep(25)
url_get_file <- modify_url(url_fichier, query = parameters)
response <- request_api(url_get_file, headers=headers,code=c(204,201,500))
}
}
if(response$status_code == 201){
return(read.csv(text=content(response,as="text")[[1]],sep=";",header=T,dec = ","))
}
}
print("Failed to retrieve data from the API. Why? not sure..")
return(NULL)
}
getStationDataDPObs <- function(start_date, end_date, station_id,headers ,dpclim = "public/DPObs/v1/",timesleep=5,base="https://public-api.meteofrance.fr") {
start_date <- as.Date(start_date)
end_date <- as.Date(end_date)
url <- modify_url(base,path = paste0(dpclim,"station/infrahoraire-6m"))
# Format dates in ISO8601 format
formatted_start_date <- format(start_date, "%Y-%m-%dT12:00:00Z", tz = "GMT")
formatted_end_date <- format(end_date, "%Y-%m-%dT00:00:00Z", tz = "GMT")
parameters <- list("id_station" = station_id, "date" = formatted_start_date,'format'='csv')
# Create the URL with parameters to ask for the csv file
url_query_file <- modify_url(url, query = parameters)
response <- request_api(url_query_file, headers=headers,code=200)
print(response)
#if(response$status_code == 202){
# url_fichier <- modify_url(base,path = paste0(dpclim,"commande/fichier"))
# parameters <- list("id-cmde" = content(response)[[1]][[1]] )
# print(paste("Waiting for file",parameters[["id-cmde"]],"wait",timesleep,"secs"))
# Sys.sleep(timesleep)
# url_get_file <- modify_url(url_fichier, query = parameters)
# response <- request_api(url_get_file, headers=headers,code=c(204,201,500))
# while(response$status_code %in% c(204,500)){
# print("file not ready yet, wait a minute more")
# Sys.sleep(60)
# response <- request_api(url_get_file, headers=headers,code=c(204,201,500))
# if(response$status_code == 500){
# print("problem with file generation, retry")
# response <- request_api(url_query_file, headers=headers,code=202)
# parameters <- list("id-cmde" = content(response)[[1]][[1]] )
# print(paste("Waiting for file",parameters[["id-cmde"]],"wait 25 sec"))
# Sys.sleep(25)
# url_get_file <- modify_url(url_fichier, query = parameters)
# response <- request_api(url_get_file, headers=headers,code=c(204,201,500))
# }
# }
# if(response$status_code == 201){
# return(read.csv(text=content(response,as="text")[[1]],sep=";",header=T,dec = ","))
# }
#}
#print("Failed to retrieve data from the API. Why? not sure..")
#return(NULL)
}