-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_de_paper.R
More file actions
71 lines (51 loc) · 1.66 KB
/
Copy pathfetch_de_paper.R
File metadata and controls
71 lines (51 loc) · 1.66 KB
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
library(httr)
library(jsonlite)
library(tidyverse)
get_paper_avail <- function(id){
# country <- toupper(country)
got <- GET(glue::glue("https://products.dm.de/store-availability/DE/availability?dans=595420,708997,137425,28171,485698,799358,863567,452740,610544,846857,709006,452753,879536,452744,485695,853483,594080,504606,593761,525943,842480,535981,127048,524535&storeNumbers={id}"))
if(got$status_code == 404){
return(NULL)
}
data <- fromJSON(rawToChar(got$content))
item_numbers = names(data$storeAvailabilities)
df <- lapply(1:length(data$storeAvailabilities), function(n){
tibble(
stockLevel = data$storeAvailabilities[[n]]$stockLevel,
storeNumber = data$storeAvailabilities[[n]]$store$storeNumber,
item_id = item_numbers[n]
)
}) %>% do.call(rbind,.)
df %>%
group_by(storeNumber) %>%
summarise(n = sum(stockLevel, na.rm = T)) %>%
pull(n)
}
get_paper_avail_vec <- function(id){
k <- 0
lapply(id, function(i){
k <<- k+1
n <- get_paper_avail(i)
print(k)
if(k %% 10 == 0) Sys.sleep(1)
return(n)
}) %>%
do.call("c",.)
}
de_stores <- read_csv("data/_ALL_DE_STORES.csv")
de_stores <- de_stores %>%
mutate(nPaper = get_paper_avail_vec(storeNumber))
de_stores <- de_stores %>%
select(city, storeNumber, lon, lat, nPaper) %>%
mutate(
bin = cut(nPaper, c(-1, 0, 10, 50, 100, 1000))
)
de_stores <- de_stores %>%
mutate(date = Sys.Date())
time <- Sys.time()
time <- str_split_fixed(time, " ", 3)[,2]
de_stores <- de_stores %>%
mutate(time = time)
time <- str_replace_all(time, ":", "_")
write_csv(de_stores,
glue::glue("data/{Sys.Date()}_{time}_deStoresStatus.csv"))