-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathinternals.R
151 lines (120 loc) · 4.63 KB
/
internals.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
# Wrapper for messages, spotted in googlesheets3
spf <- function(...) stop(sprintf(...), call. = FALSE)
# This helper function makes a single call, given the full API endpoint URL
# Used as the workhorse function inside .fetch_results() below
.meetup_call <- function(api_path,
event_status = NULL,
offset = 0,
verbose = NULL,
...) {
# list of parameters
parameters <- list(status = event_status, # you need to add the status
# otherwise it will get only the upcoming event
offset = offset,
... # other parameters
)
req <- httr::GET(url = meetup_api_prefix(), # the host
path = api_path, # path to append
query = parameters,
config = meetup_token()
)
if (req$status_code == 400) {
stop("HTTP 400 Bad Request error encountered for: ",
api_path,".\n As of June 30, 2020, this may be ",
"because a presumed bug with the Meetup API ",
"causes this error for a future event. Please ",
"confirm the event has ended.",
call. = FALSE)
}
httr::stop_for_status(req)
headers <- httr::headers(req)
assign(
"meetupr_rate",
c(headers$`x-ratelimit-limit`, headers$`x-ratelimit-reset`),
envir = .meetupr_env
)
reslist <- httr::content(req, "parsed")
if (length(reslist) == 0) {
if(verbose) {
cat("Zero records match your filter. Nothing to return.\n")
}
return(NULL)
}
return(list(result = reslist, headers = req$headers))
}
# from https://stackoverflow.com/questions/34254716/how-to-define-hidden-global-variables-inside-r-packages
.meetupr_env <- new.env(parent=emptyenv())
assign("meetupr_rate", c(30, 10), envir=.meetupr_env)
meetup_call <- ratelimitr::limit_rate(
.meetup_call,
rate = ratelimitr::rate(
.meetupr_env[["meetupr_rate"]][1],
.meetupr_env[["meetupr_rate"]][2]
)
)
meetup_api_prefix <- function() {
Sys.getenv("MEETUP_API_URL", "https://api.meetup.com/")
}
# Fetch all the results of a query given an API Method
# Will make multiple calls to the API if needed
# API Methods listed here: https://www.meetup.com/meetup_api/docs/
.fetch_results <- function(api_path, event_status = NULL, verbose = TRUE, ...) {
# Fetch first set of results (limited to 200 records each call)
res <- meetup_call(api_path = api_path,
event_status = event_status,
offset = 0,
verbose = verbose,
...)
# Total number of records matching the query
total_records <- as.integer(res$headers$`x-total-count`)
if (length(total_records) == 0) total_records <- 1L
records <- res$result
if (total_records == 0) {
return(res$result)
}
if(verbose) cat("Downloading", total_records, "record(s)...\n", sep = " ")
if((length(records) < total_records) & !is.null(res$headers$link)){
# calculate number of offsets for records above 200
offsetn <- ceiling(total_records/length(records))
all_records <- list(records)
for(i in 1:(offsetn - 1)) {
res <- meetup_call(api_path = api_path,
event_status = event_status,
offset = i,
...)
next_url <- strsplit(strsplit(res$headers$link, split = "<")[[1]][2], split = ">")[[1]][1]
next_url <- gsub(meetup_api_prefix(), "", next_url)
res <- meetup_call(next_url, event_status)
all_records[[i + 1]] <- res$result
}
records <- unlist(all_records, recursive = FALSE)
}
return(records)
}
# helper function to convert a vector of milliseconds since epoch into POSIXct
.date_helper <- function(time) {
if (is.character(time)) {
# if date is character string, try to convert to numeric
time <- tryCatch(expr = as.numeric(time),
error = warning("One or more dates could not be converted properly"))
}
if (is.numeric(time)) {
# divide milliseconds by 1000 to get seconds; convert to POSIXct
seconds <- time / 1000
out <- as.POSIXct(seconds, origin = "1970-01-01")
} else {
# if no conversion can be done, then return NA
warning("One or more dates could not be converted properly")
out <- rep(NA, length(time))
}
return(out)
}
# Helper to check event status
.check_event_status <- function(event_status){
match.arg(event_status,
c("cancelled", "draft", "past", "proposed", "suggested", "upcoming"),
several.ok = TRUE)
}
.collapse = function(x){
paste(x, collapse = ",")
}