Skip to content

Commit

Permalink
Merge pull request #48 from benubah/patch-1
Browse files Browse the repository at this point in the history
added  `fields` to `find_groups()` to retrieve optional fields from M…
  • Loading branch information
ledell authored Oct 17, 2020
2 parents 06bb7ad + 61cd2b1 commit e5344f4
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 17 deletions.
8 changes: 6 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ Authors@R:
person(given = "Rick",
family = "Pack",
role = "ctb",
email = "[email protected]"))
email = "[email protected]"),
person(given = "Ben",
family = "Ubah",
role = "ctb",
email = "[email protected]"))
Description: Provides access to data from 'meetup.com' (see
https://www.meetup.com/meetup_api/ for more information).
License: MIT + file LICENSE
Expand All @@ -55,4 +59,4 @@ VignetteBuilder:
knitr
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.0
RoxygenNote: 7.1.1
18 changes: 17 additions & 1 deletion R/find_groups.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#' @param topic_id Integer. Meetup.com topic ID.
#' @param radius can be either "global" (default) or distance in miles in the
#' range 0-100.
#' @param fields Character. Optional fields that are not returned by default.
#' @template api_key
#'
#' @return A tibble with the following columns:
Expand Down Expand Up @@ -37,14 +38,29 @@
#' api_key <- Sys.getenv("MEETUP_KEY")
#' groups <- find_groups(text = "r-ladies", api_key = api_key)
#' groups <- find_groups(topic_id = 1513883, api_key = api_key)
#' groups <- find_groups(text = "r-ladies", fields = "past_event_count,
#' upcoming_event_count", api_key = api_key)
#' past_event_counts <- purrr::map_dbl(groups$resource, "past_event_count",
#' .default = 0)
#' upcoming_event_counts <- purrr::map_dbl(groups$resource, "upcoming_event_count",
#' .default = 0)
#'}
#' @export
find_groups <- function(text = NULL, topic_id = NULL, radius = "global", api_key = NULL) {
find_groups <- function(text = NULL, topic_id = NULL, radius = "global", fields = NULL, api_key = NULL) {
api_method <- "find/groups"
# If topic_id is a vector, change it to single string of comma separated values
if(length(topic_id) > 1){
topic_id <- paste(topic_id, collapse = ",")
}
# If fields is a vector, change it to single string of comma separated values
if(length(fields) > 1){
fields <- paste(fields, collapse = ",")
}
res <- .fetch_results(api_method = api_method,
api_key = api_key,
text = text,
topic_id = topic_id,
fields = fields,
radius = radius)
tibble::tibble(
id = purrr::map_int(res, "id"),
Expand Down
20 changes: 17 additions & 3 deletions R/get_events.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#' * proposed
#' * suggested
#' * upcoming
#'
#' @param fields Character, character vector or characters separated by comma (e.g "event_hosts" or c("event_hosts","attendance_count") or "event_hosts, group_past_event_count").
#' @template api_key
#'
#' @return A tibble with the following columns:
Expand Down Expand Up @@ -44,9 +44,19 @@
#' event_status = "past")
#' upcoming_events <- get_events(urlname = urlname,
#' event_status = "upcoming")
#' past_meetings <- get_events(urlname = urlname,
#' event_status = "past",
#' fields = "event_hosts", api_key = api_key)
#' # get events hosts (co-organizers) of single past meeting
#' single_event <- past_meetings$resource[[1]]$event_hosts
#'
#' # get all event hosts names (2) and host_counts (6) for that single event
#' # host_counts represents how events the person has co-organized or hosted.
#' do.call("rbind", lapply(single_event, '[', c(2,6)))

#'}
#' @export
get_events <- function(urlname, event_status = "upcoming", api_key = NULL) {
get_events <- function(urlname, event_status = "upcoming", fields = NULL, api_key = NULL) {
if (!is.null(event_status) &&
!event_status %in% c("cancelled", "draft", "past", "proposed", "suggested", "upcoming")) {
stop(sprintf("Event status %s not allowed", event_status))
Expand All @@ -55,8 +65,12 @@ get_events <- function(urlname, event_status = "upcoming", api_key = NULL) {
if (length(event_status) > 1) {
event_status <- paste(event_status, collapse = ",")
}
# If fields is a vector, change it to single string of comma separated values
if(length(fields) > 1){
fields <- paste(fields, collapse = ",")
}
api_method <- paste0(urlname, "/events")
res <- .fetch_results(api_method, api_key, event_status)
res <- .fetch_results(api_method, api_key, event_status, fields = fields)
tibble::tibble(
id = purrr::map_chr(res, "id"), #this is returned as chr (not int)
name = purrr::map_chr(res, "name"),
Expand Down
32 changes: 23 additions & 9 deletions R/internals.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ spf <- function(...) stop(sprintf(...), call. = FALSE)
.quick_fetch <- function(api_url,
api_key = NULL, # deprecated, unused, can't swallow this in `...`
event_status = NULL,
offset = 0,
...) {

# 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
)

Expand Down Expand Up @@ -54,30 +56,42 @@ spf <- function(...) stop(sprintf(...), call. = FALSE)
api_url <- paste0(meetup_api_prefix, api_method)

# Fetch first set of results (limited to 200 records each call)

res <- .quick_fetch(api_url = api_url,
api_key = api_key,
event_status = event_status,
offset = 0,
...)

res <- .quick_fetch(api_url, event_status = event_status, ...)


# 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
cat(paste("Downloading", total_records, "record(s)..."))

# If you have not yet retrieved all records, calculate the # of remaining calls required
extra_calls <- ifelse(
(length(records) < total_records) & !is.null(res$headers$link),
floor(total_records/length(records)),
0)
if (extra_calls > 0) {
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 seq(extra_calls)) {
# Keep making API requests with an increasing offset value until you get all the records
# TO DO: clean this strsplit up or replace with regex

for(i in 1:(offsetn - 1)) {
res <- .quick_fetch(api_url = api_url,
api_key = api_key,
event_status = event_status,
offset = i,
...)

next_url <- strsplit(strsplit(res$headers$link, split = "<")[[1]][2], split = ">")[[1]][1]
res <- .quick_fetch(next_url, event_status)

all_records[[i + 1]] <- res$result
}
records <- unlist(all_records, recursive = FALSE)

}

return(records)
Expand Down
1 change: 1 addition & 0 deletions man/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
get_boards.Rd
16 changes: 15 additions & 1 deletion man/find_groups.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion man/get_events.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/meetupr-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e5344f4

Please sign in to comment.