-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.R
57 lines (42 loc) · 1.5 KB
/
main.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
library(plumber)
library(RSelenium)
library(jsonlite)
# Function to prepare data
prepareData <- function (elem) {
# Get string
string = elem$getElementText()[[1]]
# Prepare data
string = gsub('\\.', '', string)
# To numeric
data = as.numeric(string)
return(data)
}
# Connect to remote web driver
remDr <- remoteDriver(remoteServerAddr = "seleniumAddress", port = 4444L)
#* Get COVID-19 statistics
#* @serializer unboxedJSON
#* @post /covid_stats
function (res) {
# Open remote web driver
remDr$open()
# Navigate to web
remDr$navigate('https://www.mscbs.gob.es/profesionales/saludPublica/ccayes/alertasActual/nCov/situacionActual.htm')
# Confirmed cases
confirmed = list(
spain = remDr$findElement(value='//p[text()="casos confirmados en España"]//../p[1]'),
europe = remDr$findElement(value='//p[text()="casos confirmados en Europa"]//../p[1]'),
world = remDr$findElement(value='//p[text()="casos confirmados en el mundo"]//../p[1]'))
confirmed = lapply(confirmed, prepareData)
# Administered doses
dataVaccines = remDr$findElements(value='//p[@class="dosis-title"]//../p[@class="cifra"]') %>%
lapply(prepareData) %>% unlist()
vaccines = list(
dosesDelivered = dataVaccines[1],
dosesAdministered = dataVaccines[2],
completeSchedule = dataVaccines[3]
)
# Close remote driver
remDr$close()
# Out data
out = list(confirmedInfections = confirmed, vaccines = vaccines)
}