-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathultrasignup.R
462 lines (403 loc) · 13.6 KB
/
ultrasignup.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
source('run.R')
library(ggmap)
library(rjson)
library(XML)
library(utils)
library(RCurl)
library(plyr)
snooze <- function(sleepSec) if (sleepSec>0) Sys.sleep(sleepSec)
ultraCookies <- function(){
cookieFile <- file.path(tempdir(),'ultrasignup_cookies.txt')
if (!file.exists(cookieFile))
invisible(getURL('http://ultrasignup.com',.opts=curlOptions(header=TRUE,cookiejar=cookieFile)))
cookieFile
}
ultraOpts <- function() {
extra <- getOption('RUNNING.curloptions')
if (is.null(extra))
extra <- list()
extra$cookiejar <- ultraCookies()
curlOptions(.opts=extra)
}
fromJSONDF <- function(jsonURL){
x <- fromJSON(getURL(jsonURL,.opts=ultraOpts()))
# Treat all NULL values as empty strings. This will allow us to use unlist
# which unfortunately would drops NULL values.
y <- lapply(x,function(i) lapply(i,function(o) if (is.null(o)) "" else o) )
if (length(y) <= 0) return(data.frame())
colNames <- names(y[[1]])
numRows <- length(y)
z <- unlist(y)
dim(z) <- c(length(colNames),numRows)
zz <- as.data.frame(t(z),stringsAsFactors = FALSE)
names(zz) <- colNames
zz
}
queryEvent <- function(q=''){
ultraURL <- sprintf('http://ultrasignup.com/service/events.svc/GetFeaturedEventsSearch/p=0/q=%s',URLencode(q,reserve=TRUE))
fromJSONDF(ultraURL)
}
queryEventByLoc <- function(q=NULL,lat=NULL,lon=NULL,mi=300,mo=12){
if (is.null(q) && is.null(lat)){
ultraURL <- 'http://ultrasignup.com/service/events.svc/closestevents'
} else {
if (is.null(q)){
g <- list(lat=lat,lon=lon)
} else {
g <- geocode(q,messaging=FALSE)
}
lat <- URLencode(format(g$lat))
lng <- URLencode(format(g$lon))
ultraURL <- sprintf('http://ultrasignup.com/service/events.svc/closestevents?lat=%s&lng=%s&mi=%d&mo=%d',lat,lng,mi,mo)
}
x <- fromJSONDF(ultraURL)
x <- x[,names(x)[!names(x) %in% c('EventImages','ScaleColor')]]
unique(x)
}
queryRunner<- function(q=''){
ultraURL <- sprintf('http://ultrasignup.com/service/events.svc/GetParticipantSearch/p=0/q=%s',URLencode(q,reserve=TRUE))
fromJSONDF(ultraURL)
}
queryEntrants <- function(race=''){
if (is.data.frame(race)){
dframe <- race
} else if (!nzchar(race)){
# TODO: vectorize
dframe <- queryEvent(race)
}
eid <- dframe$eventId[1]
eventDistances<-dframe$eventDistances
rootURL <- 'http://ultrasignup.com/'
ultraURL <- sprintf('http://ultrasignup.com/register.aspx?eid=%d',as.integer(eid))
x <- htmlParse(getURL(ultraURL,.opts=ultraOpts()),asText=TRUE)
if (xmlValue(getNodeSet(x,'//title')[[1]])=='Object moved'){
ultraURL <- paste(rootURL,xmlAttrs(getNodeSet(x,'//a')[[1]]),sep='')
x <- htmlParse(getURL(ultraURL,.opts=ultraOpts()),asText=TRUE)
}
x <- getNodeSet(x,'//a[@id="ContentPlaceHolder1_EventInfo1_hlEntrants"]')
if (length(x)>0){
ultraURL <- paste(rootURL,xmlAttrs(x[[1]])['href'],sep='')
x <- htmlParse(getURL(ultraURL,.opts=ultraOpts()),asText=TRUE)
} else {
return(data.frame())
}
dateNode <- xmlValue(getNodeSet(x,'//span[@id="lblDate"]')[[1]])
raceDate <- strptime(strsplit(dateNode,'@')[[1]][1],format="%A, %B %d, %Y")
distances <-
ldply(
getNodeSet(x,'//a[@class="event_selected_link"]|//a[@class="event_link"]'),
function(i)
data.frame(
href=xmlAttrs(i)['href'],
distance=xmlValue(i),
selected=xmlAttrs(i)['class']=='event_selected_link',
stringsAsFactors=FALSE)
)
if (nrow(distances)==0){
# only one distance contested
distances <- data.frame(href=NA,distance=eventDistances,selected=TRUE)
}
thisDistance <- xmlChildren(getNodeSet(x,'//table[@class="ultra_grid"]')[[1]])
trim <- function(x){
gsub('^\\s+|\\s+$|\\xc2+|\\xa0+','',x,perl=TRUE)
}
genderage <- function(x){
strsplit(sub('.*([FM])(\\d\\d?).*$','\\1 \\2',x),' ')[[1]]
}
resultsForDistance <- function(y){
if (length(y)<=1){
return(data.frame())
}
ret <- data.frame()
for (i in 2:length(y)){
dat <- as.character(xmlApply(y[[i]],function(x)trim(xmlValue(x))))
ga <- genderage(dat[5])
if (dat[1]==''){
rank<-NA
} else {
rank<-as.numeric(sub('\\s+?%','',trim(dat[1])))
}
if (dat[2]==''){
agerank<-NA
} else {
agerank<-as.numeric(sub('\\s+?%','',trim(dat[2])))
}
if (dat[4]==''){
formattarget<-NA
targettime<-NA
targettime_hour<-NA
} else {
formattarget<-dat[4]
targettime<-as.integer(str2sec(dat[4]))
targettime_hour<- as.integer(str2sec(dat[4])) / 3600
}
ret <-
rbind(
ret,
data.frame(
date=raceDate,
rank=rank,
agerank=agerank,
results=as.integer(dat[3]),
formattarget=formattarget,
targettime=targettime,
targettime_hour=targettime_hour,
gender=ga[1],
age=ga[2],
firstname=dat[6],
lastname=dat[7],
city=dat[8],
state=dat[9],
bib=dat[11],
stringsAsFactors=FALSE
)
)
}
ret
}
ddplyFun <- function(i){
if (i$selected[1]==TRUE)
return(resultsForDistance(thisDistance))
ultraURL <- paste(rootURL,trim(i$href[1]),sep='')
x <- htmlParse(getURL(ultraURL,.opts=ultraOpts()),asText=TRUE)
nodes <- getNodeSet(x,'//table[@class="ultra_grid"]')
if (length(nodes)==0)
return(data.frame())
x <- xmlChildren(nodes[[1]])
resultsForDistance(x)
}
ddply( distances, .(distance),ddplyFun)
}
runnerResults <- function(lname='',fname=''){
if (is.data.frame(lname)){
dframe <- lname
# TODO: vectorize
lname <- as.character(dframe$LastName)
fname <- as.character(dframe$FirstName)
}
lnames <- lname
fnames <- fname
trim <- function(x){
gsub('^\\s+|\\s+$|\\xc2+|\\xa0+','',x,perl=TRUE)
}
ret <- data.frame()
for (i in 1:length(lnames)){
lname <- lnames[i]
fname <- fnames[i]
ultraURL <- sprintf('http://ultrasignup.com/service/events.svc/history/%s/%s/',URLencode(fname),URLencode(lname))
x <- fromJSON(getURL(ultraURL,.opts=ultraOpts()))
for (j in 1:length(x)){
gender <- x[[j]]$Gender
for (k in 1:length(x[[j]]$Results)){
res <- x[[j]]$Results[[k]]
runner_rank <- as.numeric(sub('%','',res$runner_rank))
place <- as.integer(res$place)
gender_place <- as.integer(res$gender_place)
age <- as.integer(res$age)
formattime <- res$formattime
time <- as.integer(str2sec(formattime))
time_hour <- time / 3600
eventdate <- strptime(res$eventdate,format='%m/%d/%Y')
year <- as.integer(format(eventdate,'%Y'))
race <- strsplit(res$eventname,'-')[[1]]
distance_spec <- trim(race[length(race)]) # can have two or three elem
race <- trim(race[1])
ret <- rbind( ret, data.frame(
lastname=lname,
firstname=fname,
gender=gender,
runner_rank=runner_rank,
place=place,
gender_place=gender_place,
age=age,
formattime=formattime,
time=time,
time_hour=time_hour,
eventdate=eventdate,
year=year,
eventlocation=res$eventlocation,
race=race,
distance_spec=distance_spec,
status=res$status,
stringsAsFactors=FALSE
)
)
}
}
}
ret
}
eventResults <- function(eid=NA,sleep=1){
if (is.data.frame(eid)){
dframe <- eid
eid <- dframe$eventId[1]
} else if (is.na(eid)) return(data.frame())
rootURL <- 'http://ultrasignup.com/'
ultraURL <- sprintf('http://ultrasignup.com/register.aspx?eid=%d',as.integer(eid))
# ultraURL <- "http://ultrasignup.com/register.aspx?did=23884"
# From the registration page, gather all time ID's:
# for
x <- htmlParse(getURL(ultraURL,.opts=ultraOpts()),asText=TRUE); snooze(sleep)
if (xmlValue(getNodeSet(x,'//title')[[1]])=='Object moved'){
ultraURL <- paste('http://ultrasignup.com',xmlAttrs(getNodeSet(x,'//a')[[1]]),sep='')
# Event Id unknown
if (rootURL == ultraURL) return(data.frame())
x <- htmlParse(getURL(ultraURL,.opts=ultraOpts()),asText=TRUE); snooze(sleep)
}
# getNodeSet(x,'//table[@id="ContentPlaceHolder1_Results11_dlResultYears"]//a')
timeId <- ldply(getNodeSet(x,'//table[@id="ContentPlaceHolder1_Results11_dlResultYears"]//a'),function(i) data.frame(url=xmlAttrs(i)[["href"]],year=xmlValue(i)))
if (nrow(timeId) == 0) return(data.frame())
eventName <- getNodeSet(x,'//h1[@class="event-title"]',fun=xmlValue)[[1]]
# Take the first record from the timeId and find all distances contested
x <- htmlParse(paste(rootURL,as.character(timeId$url[1]),sep=''),isURL=TRUE); snooze(sleep)
distance <-
ldply(
getNodeSet(x,'//div[@class="unit-1-2 text-right"]//a'),
function(i) {
data.frame(
url=xmlAttrs(i)[["href"]],
distance=xmlValue(i),
selected=ifelse(xmlAttrs(i)[["class"]]=='event_selected_link',TRUE,FALSE),
stringsAsFactors=FALSE
)
}
)
# This page will also have the ids of all results for a specific distance. Grab that as well
currentDist <-as.character(subset(distance,selected==TRUE)$distance)
distanceIds <- list()
distanceIds[[currentDist]] <-
ldply(
getNodeSet(x,'//table[@id="ContentPlaceHolder1_dlYears"]//a'),
function(i) {
data.frame(
id=sub('GetResults\\((\\d+),\\d+\\)','\\1',xmlAttrs(i)[["onclick"]]),
year=xmlValue(i),
stringsAsFactors=FALSE
)
}
)
# Now get results for what we've got so far
results <-
queryResultsFromUltraSignup(
year=distanceIds[[currentDist]]$year,
eventId=distanceIds[[currentDist]]$id,
distance=currentDist,
sleep=sleep
)
distance <- subset(distance,selected==FALSE)
if (nrow(distance)==0) return(results)
for (i in 1:nrow(distance)){
currentDist <- as.character(distance$distance[i])
x <- htmlParse(paste(rootURL,as.character(distance$url[i]),sep=''),isURL=TRUE); snooze(sleep)
distanceIds[[currentDist]] <-
ldply(
getNodeSet(x,'//table[@id="ContentPlaceHolder1_dlYears"]//a'),
function(i) {
data.frame(
id=sub('GetResults\\((\\d+),\\d+\\)','\\1',xmlAttrs(i)[["onclick"]]),
year=xmlValue(i),
stringsAsFactors=FALSE
)
}
)
results <-
rbind(
results,
queryResultsFromUltraSignup(
year=distanceIds[[currentDist]]$year,
eventId=distanceIds[[currentDist]]$id,
distance=currentDist,
sleep=sleep
)
)
}
results$eventname <- eventName
results$eid <- eid
results
}
# queryResultsFromUltraSignup:
#
# The following information was gleaned from ultrasignup.com on October 7, 2013.
# At the time the site used jqgrid in the browser and results were pulled via
# AJAX in JSON format. Event ids were culled by noting the URL constructed
# for each AJAX call. Example URL:
# http://ultrasignup.com/service/events.svc/results/18998/json?_search=false&nd=1381181620523&rows=1000&page=1&sidx=&sord=asc
queryResultsFromUltraSignup <-
function
(
year= c(2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013),
eventId= c(9308,9307,9306,9305,9304,9303,306,5627,6634,12848,14802,18998),
distance='50km',
sleep=0
)
{
urlTmpl = 'http://ultrasignup.com/service/events.svc/results/%d/json?_search=false'
results <- list()
# Data collection
if (is.character(year)) year <- as.integer(year)
if (is.character(eventId)) eventId <- as.integer(eventId)
for (i in 1:length(year)){
snooze(sleep)
ultraURL <- sprintf(urlTmpl,eventId[i])
x <- fromJSON(getURL(ultraURL,.opts=ultraOpts()))
if (length(x)==0){
results[[i]] <- data.frame()
next
}
# Treat all NULL values as empty strings. This will allow us to use unlist
# which unfortunately would drops NULL values.
y <- lapply(x,function(i) lapply(i,function(o) if (is.null(o)) "" else o) )
# TODO: check if there are really no results for this year, or if a network
# error occurred
if (length(y) <= 0) next
colNames <- names(y[[1]])
numRows <- length(y)
z <- unlist(y)
dim(z) <- c(length(colNames),numRows)
zz <- as.data.frame(t(z),stringsAsFactors = FALSE)
names(zz) <- colNames
zz$year <- year[i]
zz$event_distance_id <- eventId[i]
results[[i]] <- zz
}
res <- results[[1]]
if (length(results)>1){
for (i in 2:length(results)){
res <- rbind(res,results[[i]])
}
}
if (nrow(res)==0)
return(data.frame())
# Data cleaning
res$age <- as.integer(res$age)
res$age[which(res$age==0)] <- NA
res$bib <- as.integer(res$bib)
res$gender_place <- as.integer(res$gender_place)
res$place <- as.integer(res$place)
res$runner_rank <- as.numeric(res$runner_rank)
res$time <- as.numeric(res$time) / 1000
res$gender[which(res$gender=="f")] <- "F"
res$gender <- factor(res$gender,levels=c("M","F"),labels=c("Men","Women"))
# New Data
res$time_hour <- res$time/60/60
for (i in seq(20,75,by=5)){
if(i == 20){
label <- "<20"
idx <- which(res$age < 20)
if (length(idx) > 0)
res[idx,'agegroup'] <- label
} else if (i == 75){
label <- ">=75"
idx <- which(res$age >= 75)
if (length(idx) > 0)
res[idx,'agegroup'] <- label
} else {
label <- paste(i,'-',i+4,sep='')
idx <- which(res$age >= i & res$age <=i+4)
if (length(idx) > 0)
res[idx,'agegroup'] <- label
}
}
res$distance <- distance
res
}