Last active
October 11, 2017 18:37
-
-
Save leeper/9021068 to your computer and use it in GitHub Desktop.
Gender API Example https://gender-api.com/en/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Gets gender by name or email address, optionally by country or IP address. | |
library("httr") | |
library("XML") | |
#' @import httr | |
#' @import rjson | |
#' @param name A character string containing a first name, or a character vector containing first names. One must specify name or email. | |
#' @param email A character string containing an email address with a first name. One must specify name or email. | |
#' @param country An optional character string containing a two-letter country name, as listed here: https://gender-api.com/en/api-docs | |
#' @param ip An optional character string containing an IP address, used in place of country. | |
#' @return A dataframe containing the estimated gender, number of samples that estimate is based upon, an accuracy metric (ranging from 0 to 100), and the length of time for the server to process the request. | |
#' @export | |
#' @examples \dontrun{ | |
#' gender('Andrea', country='US') | |
#' gender('Andrea', country='IT') | |
#' gender(c('george','thomas','ben')) | |
#' } | |
gender <- function(name=NULL, email=NULL, country=NULL, ip=NULL){ | |
args <- list() | |
if(!is.null(name)){ | |
if(length(name)>1) | |
args$name <- paste(head(unique(name),100), collapse=';') | |
else | |
args$name <- name | |
if(length(name)>100) | |
warning("Only first 100 names will be used!") | |
} | |
if(!is.null(email)) | |
args$email <- email | |
if(!is.null(country)) | |
args$country <- country | |
if(!is.null(ip)) | |
args$ip <- ip | |
result <- GET('https://gender-api.com/get', query=args) | |
out <- content(result) | |
if(grepl('errno', content(result,as='text'))) | |
stop(out$errmsg) | |
if((!is.null(name) & length(name)==1) || !is.null(email)) | |
out <- as.data.frame(out) | |
else | |
out <- do.call(rbind.data.frame,out$result) | |
rownames(out) <- 1:nrow(out) | |
return(out) | |
} | |
#' @import httr | |
#' @import XML | |
#' @return A dataframe containing the country name and two-letter shortcut for every country available via the API. | |
#' @export | |
#' @examples \dontrun{ | |
#' genderCountryCodes() | |
#' } | |
genderCountryCodes <- function(){ | |
html <- htmlParse(GET("https://gender-api.com/en/api-docs")) | |
out <- cbind.data.frame(name = xpathSApply(html, "//div[@class='country clearfix']//div[@class='name']", xmlValue), | |
shortcut = xpathSApply(html, "//div[@class='country clearfix']//div[@class='shortcut']", xmlValue)) | |
return(out) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I emailed the developer. Seems like a bug in the API, but your code is a logical workaround (but note the 1000 request/month rate limit, which you might quickly expend doing one name at a time).