-
Notifications
You must be signed in to change notification settings - Fork 2
/
autoTrader_query.R
150 lines (130 loc) · 4.17 KB
/
autoTrader_query.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Function List -----------------------------------------------------------
# autoTrader_query()
# Take in search arguments and make master search query
# Input: 3 search arguments: 1) Make, 2) Model, 3) Zip
# Output: Master search result xml_document
paginate_search <- function(masterSearchURL,
numRecords=25,
firstRecord = 0,
recursive = FALSE,
pages="all")
{
if(numRecords < 25 && recursive == FALSE){
warning("Minimum number of records per page is 25! Returning 25 listings.")
}
masterSearchURL <-
param_set(masterSearchURL, "firstRecord", firstRecord) %>%
param_set("numRecords", numRecords)
masterSearchURLs <- list(masterSearchURL)
if(numRecords < 25 && recursive == FALSE){
warning("Minimum number of records per page is 25! Returning 25 listings.")
}
if(recursive == FALSE){ # This saves the function a TON of time if using recursion. This needs to be done only once
possiblePages <- xml2::read_html(masterSearchURL) %>%
rvest::html_node(".pull-right > span") %>%
rvest::html_text() %>%
str_split(pattern = " ") %>%
unlist() %>%
tail(n = 1) %>%
as.numeric()
}
if(!missing(pages) && !is.na(pages)){
if(pages == "all" && !is.na(possiblePages)){
range = 1:(possiblePages - 1)
} else if(is.numeric(pages) && pages >= 2){
range = 1:(pages - 1)
}
for(page in range) {
masterSearchURLs[[page + 1]] <- paginate_search(masterSearchURL,
numRecords,
firstRecord = as.integer(numRecords * page),
pages = NA,
recursive = TRUE)
}
}
return(masterSearchURLs)
}
# autoTrader_query() ------------------------------------------------------
autoTrader_query <- function(make=NA_character_,
model=NA_character_,
zip=NA_integer_,
startYear=NA_integer_,
numRecords=25,
firstRecord=0,
endYear=NA_integer_, # The following year
searchRadius=0, # 0 indicates Inf radius
sortBy = "relevance",
sellerTypes = c('p', 'd'),
minPrice,
maxPrice,
maxMileage,
pages,
recursive = FALSE)
{
possibleMilage <- c(0, seq(15000, 75000, by = 15000), 100000, 150000, 200000, 200001)
masterSearchURLs <- list(NULL)
path <- if(is.na(make) && is.na(model)) {
"/all-cars"
} else {
paste0(
ifelse(is.na(make), "", paste0("/", simpleCap(make))),
ifelse(is.na(model), "", paste0("/", simpleCap(model)))
)
}
masterSearchURL <- paste0(
"https://www.autotrader.com/cars-for-sale",
path,
if(!missing(zip) && !is.na(zip)){
paste0(
"?zip=", zip, "&"
)
} else {
"?"
}
,
"startYear=", startYear,
"&sortBy=", sortBy,
"&endYear=", endYear,
"&searchRadius=", searchRadius
,
if(!missing(minPrice)){
paste0(
"&minPrice=", minPrice
)
} else {
""
}
,
if(!missing(maxPrice)){
paste0(
"&maxPrice=", maxPrice
)
} else {
""
}
,
if(!missing(maxMileage)){
maxMileage <- possibleMilage[findInterval(maxMileage, possibleMilage)]
paste0(
"&maxMileage=", maxMileage
)
} else {
""
}
,
if(!missing(sellerTypes)){
paste0(
"&sellerTypes=", reduce(sellerTypes, function(a, b) paste(a, b, sep = ","))
)
} else {
""
}
,
sep = ""
)
paginate_search(masterSearchURL,
pages=pages,
numRecords=numRecords,
firstRecord=firstRecord,
recursive=FALSE)
}