-
Notifications
You must be signed in to change notification settings - Fork 0
/
dailly-update-world.R
70 lines (59 loc) · 3.31 KB
/
dailly-update-world.R
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
# ----------------------------------------------
# load required packages
# ----------------------------------------------
if(!suppressWarnings(require(httr))) install.packages("httr", repos = "http://cran.us.r-project.org")
if(!suppressWarnings(require(dplyr))) install.packages("dplyr", repos = "http://cran.us.r-project.org")
if(!suppressWarnings(require(jsonlite))) install.packages("jsonlite", repos = "http://cran.us.r-project.org")
if(!suppressWarnings(require(raster))) install.packages("raster", repos = "http://cran.us.r-project.org")
# ----------------------------------------------
# load API KEY
# ----------------------------------------------
load('./DATA/environment.Rdata')
# ----------------------------------------------
# OPENAPI GET
# ----------------------------------------------
res <- GET(paste0('http://openapi.data.go.kr/openapi/service/rest/Covid19/getCovid19NatInfStateJson?serviceKey=',api_key_decode,'&pageNo=1&numOfRows=10&startCreateDt=',format(Sys.Date()-7,'%Y%m%d'),'&endCreateDt=',format(Sys.Date(),'%Y%m%d')))
res %>%
content(as ='text', encoding ='UTF-8') %>%
fromJSON() -> json
# str(object = json)
if (json$response$header$resultMsg == "NORMAL SERVICE."){
cat('@ [Success] HTTP Response :',json$response$header$resultMsg,'\n')
}else{
cat('@ [Fail] HTTP Response Error :',json$response$header$resultMsg,'\n')
break()
}
df <- json$response$body$items$item
df$createDt<- as.Date(df$createDt,format='%Y-%m-%d')
df$natDeathRate<- as.numeric(df$natDeathRate)
df <- df[df$createDt==max(df$createDt),]
# ----------------------------------------------
# Create Dataset
# ----------------------------------------------
load('./DATA/World_covid19.Rdata')
if (max(world@data$createDt,na.rm=TRUE)!=max(df$createDt)){
cat('@@ GET world covid19 data\n')
df <- df[,c('areaNmEn','stdDay','createDt','natDeathCnt','natDeathRate','natDefCnt','nationNmEn','nationNm','seq')]
df$nationNmEn[df$nationNmEn =="Russia"] <- 'Russian Federation'
df$nationNmEn[df$nationNmEn =='United States of America'] <- "United States"
df$nationNmEn[df$nationNmEn =='Columbia'] <- "Colombia"
df$nationNmEn[df$nationNmEn =='Republic of South Africa'] <- "South Africa"
df$nationNmEn[df$nationNmEn =='Elsalvador'] <- "El Salvador"
df$nationNmEn[df$nationNmEn =='Commonwealth of Dominica'] <- "Dominica"
df$nationNmEn[df$nationNmEn =='Cabo Verd'] <- "Cape Verde"
df$nationNmEn[df$nationNmEn =='Cote d´Ivoire'] <- "C척te d'Ivoire"
df$nationNmEn[df$nationNmEn =='Guinea Bissau'] <- "Guinea-Bissau"
df$nationNmEn[df$nationNmEn =='Brundi'] <- "Burundi"
df$nationNmEn[df$nationNmEn =='DRCongo'] <- "Congo DRC"
df$nationNmEn[df$nationNmEn =='Swatini'] <- "Eswatini"
df$nationNmEn[df$nationNmEn =='Luxemburg'] <- "Luxembourg"
df$nationNmEn[df$nationNmEn =='Montegro'] <- "Montenegro"
df$nationNmEn[df$nationNmEn =='East Timor'] <- "Timor-Leste"
df$nationNmEn[df$nationNmEn =='Brunei'] <- "Brunei Darussalam"
load('./DATA/World_shp.Rdata')
world@data$id = c(1:dim(world@data)[1])
world@data <- merge(world@data,df,by.x='COUNTRYAFF',by.y='nationNmEn',all.x=TRUE)
world@data <- world@data[order(world@data$id),]
save(world, file='./DATA/World_covid19.Rdata')
# write.csv(df,file='./DATA/World_covid19.csv',row.names = FALSE,fileEncoding = 'UTF-8')
}