/
annotations.R
175 lines (143 loc) · 4.51 KB
/
annotations.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
# set custom genius annotation class
as_genius_annotation <- function(content, path, response) {
structure(
list(
content = content,
path = path,
response = response
),
class = "genius_annotation"
)
}
#' Slightly more human-readable output for genius_annotation objects
#'
#' @param x a genius_annotation object
#' @param ... ignored
#' @export
print.genius_annotation <- function(x, ...) {
cat("Genius annotations <", x$path, ">\n", sep = "")
utils::str(x$content, max=1)
invisible(x)
}
#' Retrieve metadata for an annotation
#'
#' The Genius API lets you return data for a specific annotation, given an
#' annotation ID. \code{get_annotation} returns this data in full.
#'
#' A Genius annotation is a piece of content about a part of a document.
#' The document may be a song (hosted on Genius) or a web page (hosted anywhere).
#' The part of a document that an annotation is attached to is called a referent.
#'
#' @family annotation
#'
#' @param annotation_id ID of the annotation
#' @param access_token Genius' client access token, defaults to \code{genius_token}
#'
#' @return a \code{genius_annotation} object that contains the extracted content from the request,
#' the original JSON response object and the request path.
#'
#' @examples
#' \dontrun{
#' get_annotation(annotation_id = 16511101)
#' }
#' @export
get_annotation <- function(annotation_id, access_token = genius_token()) {
check_internet()
path <- sprintf("api.genius.com/annotations/%s", annotation_id)
# request track
req <- genius_get(url = path, access_token)
stop_for_status(req)
res <- content(req)
as_genius_annotation(
res$response, path, req
)
}
# set custom genius referent class
as_genius_referent <- function(content, path, response) {
structure(
list(
content = content,
path = path,
response = response
),
class = "genius_referent"
)
}
#' Slightly more human-readable output for genius_referent objects
#'
#' @param x a genius_referent object
#' @param ... ignored
#' @export
print.genius_referent <- function(x, ...) {
cat("Genius referent <", x$path, ">\n", sep = "")
utils::str(x$content, max=1)
invisible(x)
}
#' Retrieve metadata for a referent
#'
#' The Genius API lets you return data for a specific referent.
#' \code{get_referent} returns this data in full.
#'
#' Referents are the sections of a piece of content to which annotations are attached.
#' Each referent is associated with a web page or a song and may have one or more annotations.
#' Referents can be searched by the document they are attached to or by the user that created them.
#'
#' @family annotation
#'
#' @param created_by_id ID of a user to get referents for
#' @param song_id ID of a song to get referents for (pass only one of \code{song_id} and
#' \code{web_page_id})
#' @param web_page_id ID of a web page to get referents for (pass only one of \code{song_id} and
#' \code{web_page_id})
#' @param access_token Genius' client access token, defaults to \code{genius_token}
#'
#' @return a \code{genius_referent} object that contains the extracted content from the request,
#' the original JSON response object and the request path.
#'
#' @examples
#' \dontrun{
#' get_referent(song_id = 3039923)
#' }
#' @export
get_referent <- function(created_by_id, song_id, web_page_id,
access_token = genius_token()) {
check_internet()
# see if too many arguments passed
if (!missing(song_id) && !missing(web_page_id))
stop("You may pass only one of song_id and web_page_id, not both.")
# construct request url
if (!missing(song_id)) {
path <- sprintf("api.genius.com/referents?song_id=%s", song_id)
} else if (!missing(web_page_id)) {
path <- sprintf("api.genius.com/referents?web_page_id=%s", web_page_id)
}
# add created by clause if arg passed
if (!missing(created_by_id)) {
path <- paste0(path, "&created_by_id=", web_page_id)
}
results <- list()
# initiate page counter
i <- 1
# while loop for retrieving results
while (i > 0) {
# search for term
req <- genius_get(url = paste0(path, "&page=", i),
access_token)
# stop if unexpected request status returned
stop_for_status(req)
# extract request content
res <- content(req)
results[[i]] <- res$response$referents
# check if there are any results
if (length(res$response$referents) > 0) {
i <- i + 1
} else {
break
}
}
# bind result elements
results <- do.call(c, results)
as_genius_referent(
results, path, req
)
}