├── .Rbuildignore ├── .gitignore ├── .travis.yml ├── CONDUCT.md ├── DESCRIPTION ├── LICENSE ├── NAMESPACE ├── NEWS ├── R ├── query.R ├── udapi.R └── utils.R ├── README.md ├── man ├── get_tags.Rd ├── get_term.Rd ├── random_term.Rd └── udapi.Rd ├── tests ├── testthat.R └── testthat │ └── test_udapi.R └── udapi.Rproj /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | ^CONDUCT\.md$ 4 | .travis.yml -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # Sample .travis.yml for R projects 2 | 3 | language: r 4 | warnings_are_errors: false 5 | sudo: required 6 | 7 | env: 8 | global: 9 | - CRAN: http://cran.rstudio.com 10 | 11 | r_packages: 12 | - httr 13 | - curl 14 | - testthat 15 | notifications: 16 | email: 17 | on_success: change 18 | on_failure: change 19 | -------------------------------------------------------------------------------- /CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, we pledge to respect all people who 4 | contribute through reporting issues, posting feature requests, updating documentation, 5 | submitting pull requests or patches, and other activities. 6 | 7 | We are committed to making participation in this project a harassment-free experience for 8 | everyone, regardless of level of experience, gender, gender identity and expression, 9 | sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion. 10 | 11 | Examples of unacceptable behavior by participants include the use of sexual language or 12 | imagery, derogatory comments or personal attacks, trolling, public or private harassment, 13 | insults, or other unprofessional conduct. 14 | 15 | Project maintainers have the right and responsibility to remove, edit, or reject comments, 16 | commits, code, wiki edits, issues, and other contributions that are not aligned to this 17 | Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed 18 | from the project team. 19 | 20 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by 21 | opening an issue or contacting one or more of the project maintainers. 22 | 23 | This Code of Conduct is adapted from the Contributor Covenant 24 | (http:contributor-covenant.org), version 1.0.0, available at 25 | http://contributor-covenant.org/version/1/0/0/ 26 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: udapi 2 | Type: Package 3 | Title: Urban Dictionary API Client 4 | Version: 0.1.3 5 | Date: 2018-08-18 6 | Author: Oliver Keyes [aut, cre] 7 | Maintainer: Oliver Keyes 8 | Description: A client for the Urban Dictionary API. 9 | License: MIT + file LICENSE 10 | LazyData: TRUE 11 | RoxygenNote: 6.0.1 12 | Imports: 13 | httr, 14 | curl 15 | BugReports: https://github.com/Ironholds/udapi/issues 16 | URL: https://github.com/Ironholds/udapi/ 17 | Suggests: testthat 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2016 2 | COPYRIGHT HOLDER: Oliver Keyes -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(get_tags) 4 | export(get_term) 5 | export(random_term) 6 | importFrom(curl,curl_escape) 7 | importFrom(httr,GET) 8 | importFrom(httr,content) 9 | importFrom(httr,stop_for_status) 10 | importFrom(httr,user_agent) 11 | -------------------------------------------------------------------------------- /NEWS: -------------------------------------------------------------------------------- 1 | 0.1.3 2 | ===================== 3 | * Fix broken functionality following an API change 4 | 5 | 0.1.2 6 | ===================== 7 | * Deprecate tag retrieval 8 | 9 | 0.1.1 10 | ===================== 11 | * Test fix 12 | 13 | 0.1.0 14 | ===================== 15 | * Initial release -------------------------------------------------------------------------------- /R/query.R: -------------------------------------------------------------------------------- 1 | #'@title Retrieve Urban Dictionary definitions for a term 2 | #'@description \code{get_term} gets definitions for a specific term from 3 | #'Urban Dictionary, along with associated metadata such as the submitter, 4 | #'up- and down-votes, and a permalink. 5 | #' 6 | #'@param term the term to search for. 7 | #' 8 | #'@param ... further arguments to pass to httr's GET function. 9 | #' 10 | #'@examples 11 | #'# Simple example - retrieve the entry for "friendly fade" 12 | #'fade_definitions <- get_term("friendly fade") 13 | #' 14 | #'@seealso \code{\link{random_term}} for retrieving a set of random definitions 15 | #'for random terms. 16 | #' 17 | #'@importFrom curl curl_escape 18 | #'@export 19 | get_term <- function(term, ...){ 20 | url <- paste0("define?term=", curl::curl_escape(term)) 21 | return(clean_results(ud_query(params = url, ...))) 22 | } 23 | 24 | #'@title Retrieve random Urban Dictionary definitions 25 | #'@description \code{random_term} retrieves a random set of 10 26 | #'Urban Dictionary definitions, along with associated metadata. 27 | #' 28 | #'@param ... arguments to pass to httr's GET function. 29 | #' 30 | #'@examples 31 | #'# Grab 10 random entries 32 | #'entries <- random_term() 33 | #' 34 | #'@seealso \code{\link{get_term}} for retrieving a set of definitions for 35 | #'a specific term. 36 | #'@export 37 | random_term <- function(...){ 38 | return(clean_results(ud_query(params = "random", ...))) 39 | } 40 | 41 | #'@title Get tags associated with a Term 42 | #'@description \code{get_tags} retrieves the tags associated 43 | #'with a particular term on Urban Dictionary. 44 | #' 45 | #'@param term the term to search for. 46 | #' 47 | #'@param ... further arguments to pass to httr's GET function. 48 | #' 49 | #'@return a data.frame containing the term and the top 10 tags associated with it. 50 | #' 51 | #'@seealso \code{\link{get_term}} for retrieving a set of \emph{definitions} for 52 | #'a specific term. 53 | #' 54 | #'@export 55 | get_tags <- function(term, ...){ 56 | .Defunct() 57 | } -------------------------------------------------------------------------------- /R/udapi.R: -------------------------------------------------------------------------------- 1 | #'@title Urban Dictionary API client 2 | #'@description a client library for Urban Dictionary's API 3 | #'@name udapi 4 | #'@docType package 5 | #'@seealso \code{\link{get_term}} for retrieving definitions for a specific term, 6 | #'\code{\link{random_term}} for random definitions of random terms, and \code{\link{get_tags}} for 7 | #'tags associated with a term. 8 | #'@aliases udapi udapi-package 9 | NULL 10 | -------------------------------------------------------------------------------- /R/utils.R: -------------------------------------------------------------------------------- 1 | #'@importFrom httr user_agent stop_for_status GET content 2 | ud_query <- function(params, term = TRUE, ...){ 3 | url <- paste0("http://api.urbandictionary.com/v0/", params) 4 | 5 | result <- httr::GET(url, httr::user_agent("udapi - https://github.com/Ironholds/udapi")) 6 | httr::stop_for_status(result) 7 | output <- httr::content(result) 8 | if("result_type" %in% names(output) && output$result_type == "no_results"){ 9 | stop("No results found") 10 | } 11 | if(term){ 12 | return(output$list) 13 | } 14 | return(output) 15 | } 16 | 17 | clean_results <- function(results){ 18 | results <- lapply(results, function(x){ 19 | if("sound_urls" %in% names(x)){ 20 | x$sound_urls <- NULL 21 | } 22 | return(x) 23 | }) 24 | output_names <- names(results[[1]]) 25 | output <- data.frame(matrix(unlist(results), nrow = length(results), byrow = TRUE), 26 | stringsAsFactors = FALSE) 27 | names(output) <- output_names 28 | output$thumbs_up <- as.numeric(output$thumbs_up) 29 | output$defid <- as.numeric(output$defid) 30 | output$thumbs_down <- as.numeric(output$thumbs_down) 31 | return(output) 32 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Urban Dictionary API client 2 | 3 | __Author:__ Oliver Keyes
4 | __License:__ [MIT](http://opensource.org/licenses/MIT)
5 | __Status:__ Stable 6 | 7 | [![Travis-CI Build Status](https://travis-ci.org/Ironholds/udapi.svg?branch=master)](https://travis-ci.org/Ironholds/udapi) ![downloads](http://cranlogs.r-pkg.org/badges/grand-total/udapi) 8 | 9 | ###Description 10 | 11 | Exactly what it says on the tin, udapi provides a connector to the Urban Dictionary API. It allows you to: 12 | 13 | 1. Retrieve definitions associated with a term, using `get_term`; 14 | 2. Retrieve definitions associated with a *random* term, using `random_term`; 15 | 16 | It's a simple client because it's a simple API. 17 | 18 | Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md). 19 | By participating in this project you agree to abide by its terms. 20 | 21 | ###Installation 22 | 23 | The CRAN version can be retrieved with: 24 | 25 | install.packages("udapi") 26 | 27 | The latest version can be obtained via: 28 | 29 | devtools::install_github("ironholds/udapi") 30 | 31 | ###Dependencies 32 | * R 33 | * httr 34 | -------------------------------------------------------------------------------- /man/get_tags.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/query.R 3 | \name{get_tags} 4 | \alias{get_tags} 5 | \title{Get tags associated with a Term} 6 | \usage{ 7 | get_tags(term, ...) 8 | } 9 | \arguments{ 10 | \item{term}{the term to search for.} 11 | 12 | \item{...}{further arguments to pass to httr's GET function.} 13 | } 14 | \value{ 15 | a data.frame containing the term and the top 10 tags associated with it. 16 | } 17 | \description{ 18 | \code{get_tags} retrieves the tags associated 19 | with a particular term on Urban Dictionary. 20 | } 21 | \seealso{ 22 | \code{\link{get_term}} for retrieving a set of \emph{definitions} for 23 | a specific term. 24 | } 25 | -------------------------------------------------------------------------------- /man/get_term.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/query.R 3 | \name{get_term} 4 | \alias{get_term} 5 | \title{Retrieve Urban Dictionary definitions for a term} 6 | \usage{ 7 | get_term(term, ...) 8 | } 9 | \arguments{ 10 | \item{term}{the term to search for.} 11 | 12 | \item{...}{further arguments to pass to httr's GET function.} 13 | } 14 | \description{ 15 | \code{get_term} gets definitions for a specific term from 16 | Urban Dictionary, along with associated metadata such as the submitter, 17 | up- and down-votes, and a permalink. 18 | } 19 | \examples{ 20 | # Simple example - retrieve the entry for "friendly fade" 21 | fade_definitions <- get_term("friendly fade") 22 | 23 | } 24 | \seealso{ 25 | \code{\link{random_term}} for retrieving a set of random definitions 26 | for random terms. 27 | } 28 | -------------------------------------------------------------------------------- /man/random_term.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/query.R 3 | \name{random_term} 4 | \alias{random_term} 5 | \title{Retrieve random Urban Dictionary definitions} 6 | \usage{ 7 | random_term(...) 8 | } 9 | \arguments{ 10 | \item{...}{arguments to pass to httr's GET function.} 11 | } 12 | \description{ 13 | \code{random_term} retrieves a random set of 10 14 | Urban Dictionary definitions, along with associated metadata. 15 | } 16 | \examples{ 17 | # Grab 10 random entries 18 | entries <- random_term() 19 | 20 | } 21 | \seealso{ 22 | \code{\link{get_term}} for retrieving a set of definitions for 23 | a specific term. 24 | } 25 | -------------------------------------------------------------------------------- /man/udapi.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/udapi.R 3 | \docType{package} 4 | \name{udapi} 5 | \alias{udapi} 6 | \alias{udapi-package} 7 | \alias{udapi-package} 8 | \title{Urban Dictionary API client} 9 | \description{ 10 | a client library for Urban Dictionary's API 11 | } 12 | \seealso{ 13 | \code{\link{get_term}} for retrieving definitions for a specific term, 14 | \code{\link{random_term}} for random definitions of random terms, and \code{\link{get_tags}} for 15 | tags associated with a term. 16 | } 17 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(udapi) 3 | 4 | test_check("udapi") 5 | -------------------------------------------------------------------------------- /tests/testthat/test_udapi.R: -------------------------------------------------------------------------------- 1 | context("Test udapi's internal functions") 2 | 3 | test_that("specific content can be retrieved", { 4 | result <- get_term("friendly fade") 5 | expect_that(ncol(result), equals(10)) 6 | }) 7 | 8 | test_that("random content can be retrieved", { 9 | result <- random_term() 10 | expect_that(ncol(result), equals(10)) 11 | expect_that(nrow(result), equals(10)) 12 | }) -------------------------------------------------------------------------------- /udapi.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | StripTrailingWhitespace: Yes 16 | 17 | BuildType: Package 18 | PackageUseDevtools: Yes 19 | PackageInstallArgs: --no-multiarch --with-keep.source 20 | PackageRoxygenize: rd,collate,namespace,vignette 21 | --------------------------------------------------------------------------------