-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIdealista_API.R
99 lines (82 loc) · 3.32 KB
/
Idealista_API.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
### IDEALISTA API ###
# Working Directory
setwd("/Users/YOURNAMEHERE/Desktop")
# Consumer Key & Secret (CONFIDENTIAL)
source("Idealista_Auth.R") # Load variables from external file
#consumer_key <- "XXXXX"
#consumer_secret <- "XXXXX"
# SOURCE: http://developers.idealista.com/access-request
# Secret
secret <- jsonlite::base64_enc(paste(consumer_key, consumer_secret, sep = ":"))
## Search URL parameters ##
operation <- "sale" # sale, rent
order <- "publicationDate" # priceDown, publicationDate, modificationDate, size, distance
center <- "40.4167,-3.70325" # Madrid
distance <- "5000" # meters from the center
maxPrice <- "500000" # Euros
propertyType <- "homes" # homes, offices, premises, garages, bedrooms
typology <- "flat" # flat, chalet, countryHouse, garage, premise, room, office
sorting <- "desc" # asc, desc
language <- "es" # es, it, pt
## FOR LOOP parameters ##
# Amount of loops
x <- 10 # Search pages
# Time between loops
sleeping <- 5 # Seconds
for (i in 1:x) {
## START ##
# Total waiting time
if (i == 1){
y <- x * sleeping
print(paste("Wait for", y, "seconds in total, for", x, "JSON files."))
}
# Request with authorization details
req <- httr::POST("https://api.idealista.com/oauth/token",
httr::add_headers(
"Authorization" = paste("Basic", gsub("\n", "", secret)),
"Content-Type" = "application/x-www-form-urlencoded;charset=UTF-8"
),
body = "grant_type=client_credentials"
)
# Token
token <- paste("Bearer", httr::content(req)$access_token)
# Search URL
url <- paste("https://api.idealista.com/3.5/es/search?operation=", operation,
"&maxItems=50&order=", order,
"¢er=", center,
"&distance=", distance,
"&maxPrice=", maxPrice,
"&propertyType=", propertyType,
"&sort=", sorting,
"&typology=", typology,
"&numPage=",i,
"&language=", language,
sep = "")
# Request with Token
req <- httr::POST(url, httr::add_headers("Authorization" = token))
# Timestamp
newsystime <- format(Sys.time(),"%Y-%m-%d-%H-%M-%S")
# Request JSON file as text
json <- httr::content(req, as = "text")
# Convert JSON
df <- jsonlite::fromJSON(json)
# Save JSON file with timestamp
jsonlite::write_json(df, path = paste("Idealista_", newsystime, ".json", sep = ""))
# Progress
if (i < x){
# Saved confirmation
print(paste("JSON File", i, "has been saved.", sep = " "))
# Wait
Sys.sleep(sleeping) # Seconds
} else {
# Saved confirmation
print(paste("JSON File", i, "has been saved.", sep = " "))
# Done
print("Done!")
print("All JSON files have been saved in your Working Directory.")
}
## END ##
}
## ALTERNATIVE URLs ##
#url <- "https://api.idealista.com/3.5/es/search?operation=sale&maxItems=50&order=priceDown¢er=40.4167,-3.70325&distance=3000&maxPrice=500000&propertyType=homes&sort=desc&numPage=1&language=es"
#url <- "https://api.idealista.com/3.5/es/search?operation=sale&maxItems=50&order=priceDown¢er=40.4167,-3.70325&distance=10000&maxPrice=500000&propertyType=homes&subtypology=flats&sort=desc&numPage=1&language=es"