get_dpclim_period_bounds <- function(start_date, end_date) { start_date <- as.Date(start_date) end_date <- as.Date(end_date) list( start = format(start_date, "%Y-%m-%dT00:00:00Z", tz = "GMT"), end = format(end_date + 1L, "%Y-%m-%dT00:00:00Z", tz = "GMT") ) } getStationData <- function(start_date, end_date, station_id,headers ,dpclim = "public/DPClim/v1/",timesleep=5,base="https://public-api.meteofrance.fr") { url <- httr::modify_url(base,path = paste0(dpclim,"commande-station/infrahoraire-6m")) period_bounds <- get_dpclim_period_bounds(start_date, end_date) parameters <- list( "id-station" = station_id, "date-deb-periode" = period_bounds$start, "date-fin-periode" = period_bounds$end ) # Create the URL with parameters to ask for the csv file url_query_file <- httr::modify_url(url, query = parameters) response <- request_api(url_query_file, headers=headers,code=202) if(!is.null(response) && response$status_code == 202){ url_fichier <- httr::modify_url(base,path = paste0(dpclim,"commande/fichier")) parameters <- list("id-cmde" = httr::content(response)[[1]][[1]] ) print(paste("Waiting for file",parameters[["id-cmde"]],"wait",timesleep,"secs")) Sys.sleep(timesleep) url_get_file <- httr::modify_url(url_fichier, query = parameters) response <- request_api(url_get_file, headers=headers,code=c(204,201,500)) while(!is.null(response) && 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(!is.null(response) && response$status_code == 500){ print("problem with file generation, retry") response <- request_api(url_query_file, headers=headers,code=202) if (is.null(response)) { break } parameters <- list("id-cmde" = httr::content(response)[[1]][[1]] ) print(paste("Waiting for file",parameters[["id-cmde"]],"wait 25 sec")) Sys.sleep(25) url_get_file <- httr::modify_url(url_fichier, query = parameters) response <- request_api(url_get_file, headers=headers,code=c(204,201,500)) } } if(!is.null(response) && response$status_code == 201){ return(read.csv(text=httr::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/DPClim/v1/",timesleep=5,base="https://public-api.meteofrance.fr") { url <- httr::modify_url(base,path = paste0(dpclim,"commande-station/infrahoraire-6m")) period_bounds <- get_dpclim_period_bounds(start_date, end_date) parameters <- list( "id-station" = station_id, "date-deb-periode" = period_bounds$start, "date-fin-periode" = period_bounds$end ) # Create the URL with parameters to ask for the csv file url_query_file <- httr::modify_url(url, query = parameters) response <- request_api(url_query_file, headers=headers,code=202) if(!is.null(response) && response$status_code == 202){ url_fichier <- httr::modify_url(base,path = paste0(dpclim,"commande/fichier")) parameters <- list("id-cmde" = httr::content(response)[[1]][[1]] ) print(paste("Waiting for file",parameters[["id-cmde"]],"wait",timesleep,"secs")) Sys.sleep(timesleep) url_get_file <- httr::modify_url(url_fichier, query = parameters) response <- request_api(url_get_file, headers=headers,code=c(204,201,500)) while(!is.null(response) && 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(!is.null(response) && response$status_code == 500){ print("problem with file generation, retry") response <- request_api(url_query_file, headers=headers,code=202) if (is.null(response)) { break } parameters <- list("id-cmde" = httr::content(response)[[1]][[1]] ) print(paste("Waiting for file",parameters[["id-cmde"]],"wait 25 sec")) Sys.sleep(25) url_get_file <- httr::modify_url(url_fichier, query = parameters) response <- request_api(url_get_file, headers=headers,code=c(204,201,500)) } } if(!is.null(response) && response$status_code == 201){ return(read.csv(text=httr::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") { url <- httr::modify_url(base,path = paste0(dpclim,"commande-station/infrahoraire-6m")) period_bounds <- get_dpclim_period_bounds(start_date, end_date) parameters <- list( "id-station" = station_id, "date-deb-periode" = period_bounds$start, "date-fin-periode" = period_bounds$end ) # Create the URL with parameters to ask for the csv file url_query_file <- httr::modify_url(url, query = parameters) response <- request_api(url_query_file, headers=headers,code=202) if(!is.null(response) && response$status_code == 202){ url_fichier <- httr::modify_url(base,path = paste0(dpclim,"commande/fichier")) parameters <- list("id-cmde" = httr::content(response)[[1]][[1]] ) print(paste("Waiting for file",parameters[["id-cmde"]],"wait",timesleep,"secs")) Sys.sleep(timesleep) url_get_file <- httr::modify_url(url_fichier, query = parameters) response <- request_api(url_get_file, headers=headers,code=c(204,201,500)) while(!is.null(response) && 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(!is.null(response) && response$status_code == 500){ print("problem with file generation, retry") response <- request_api(url_query_file, headers=headers,code=202) if (is.null(response)) { break } parameters <- list("id-cmde" = httr::content(response)[[1]][[1]] ) print(paste("Waiting for file",parameters[["id-cmde"]],"wait 25 sec")) Sys.sleep(25) url_get_file <- httr::modify_url(url_fichier, query = parameters) response <- request_api(url_get_file, headers=headers,code=c(204,201,500)) } } if(!is.null(response) && response$status_code == 201){ return(read.csv(text=httr::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 <- httr::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 <- httr::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) }