Skip to content

Commit 4bd1fcf

Browse files
committed
first commit
1 parent be438e8 commit 4bd1fcf

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

MOT.Rproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Version: 1.0
2+
3+
RestoreWorkspace: Default
4+
SaveWorkspace: Default
5+
AlwaysSaveHistory: Default
6+
7+
EnableCodeIndexing: Yes
8+
UseSpacesForTab: Yes
9+
NumSpacesForTab: 2
10+
Encoding: UTF-8
11+
12+
RnwWeave: Sweave
13+
LaTeX: pdfLaTeX

get_data.R

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
library(httr)
2+
key = Sys.getenv("MOT_key") # get API key
3+
npages = 100000 #59310
4+
res <- list()
5+
6+
pb = txtProgressBar(min = 1, max = npages, initial = 1) # Make progress bar
7+
for(i in 1:npages){
8+
setTxtProgressBar(pb,i)
9+
# Request page
10+
req <- GET(
11+
url = "https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests",
12+
query = list(
13+
page = i
14+
),
15+
add_headers(`Content-type` = "application/json", `x-api-key` = key)
16+
)
17+
if(req$status_code == 200){
18+
# convert response content into text
19+
data <- httr::content(req, as = "text", encoding = "UTF-8")
20+
data <- jsonlite::fromJSON(data)
21+
res[[i]] <- data
22+
rm(data)
23+
}else if(req$status_code == 404){
24+
message(paste0("Failed to get page ",i," page not found"))
25+
if(i > 90000){
26+
stop("must have run out of pages")
27+
}
28+
}else{
29+
message(paste0("Failed to get page ",i," error ",req$status_code))
30+
}
31+
32+
if(i %% 100 == 0){
33+
saveRDS(res,"F:/MOT_data/download_data.Rds")
34+
}
35+
36+
}
37+
38+
res <- dplyr::bind_rows(res)
39+
saveRDS(res,"F:/MOT_data/mot_history_all_2019_10_04.Rds")

0 commit comments

Comments
 (0)