-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_helpers.R
115 lines (105 loc) · 3.23 KB
/
api_helpers.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
#' get_total_pages
#'
#' Returns the total number of pages that your song list spans across. Used for determining
#' how many pages to loop the api call over
#'
#' @param username Your `Last.fm` username, as a string
#' @param api_key Your `Last.fm` api_key, as a string
#'
#' @return A single number character string
#' @noRd
get_total_pages <- function(username, api_key, from = 0, max_per_page){
print("Getting page numbers...")
base_url <- paste0(
"http://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&user=",
username,
"&limit=",
max_per_page,
"&api_key=",
api_key,
"&format=json&from=",
from
)
repeat {
response <- httr::GET(base_url)
if (response$status_code == 200) break
}
text_response <- httr::content(response, "text", encoding = "UTF-8")
parsed = jsonlite::fromJSON(text_response, flatten = TRUE)
total_pages = as.integer(parsed[["recenttracks"]][["@attr"]][["totalPages"]])
page_1 = parsed[["recenttracks"]][["track"]]
return(list(total_pages,
page_1))
}
#' construct_urls
#'
#' @param total_pages Total number of pages to build
#' @param username Your username
#' @param api_key Your api_key
#'
#' @return Vector of urls
#' @noRd
#'
construct_urls <- function(total_pages, username, api_key, from = 0, max_per_page){
result <- vector("character")
for (page in seq_along(1:total_pages)){
urls <- paste0(
"http://ws.audioscrobbler.com/2.0/?method=user.getRecentTracks&user=",
username,
"&limit=",
max_per_page,
"&api_key=",
api_key,
"&format=json&page=",
page,
"&from=",
from
)
result[page] <- urls
}
return(stats::na.omit(result))
}
#' rename_api_response
#'
#' Takes the parsed response from the Last.fm API and renames the columns to make them user
#' friendly.
#'
#' @param api_data An api response that has been parsed by the `download_scrobbles` function.
#'
#' @return A renamed dataframe
#' @noRd
rename_api_response <- function(api_data){
names(api_data)[names(api_data) == "name"] <- "song_title"
names(api_data)[names(api_data) == "mbid"] <- "song_mbid"
names(api_data)[names(api_data) == "artist.mbid"] <- "artist_mbid"
names(api_data)[names(api_data) == "artist.#text"] <- "artist"
names(api_data)[names(api_data) == "album.mbid"] <- "album_mbid"
names(api_data)[names(api_data) == "album.#text"] <- "album"
names(api_data)[names(api_data) == "date.#text"] <- "date"
names(api_data)[names(api_data) == "date.uts"] <- "date_unix"
return(api_data)
}
#' get_last_timestamp
#'
#' @param scrobbles_df A dataframe output of `download_scrobbles`
#' @param timestamp_column The column in `scrobbles_df` that corresponds to the `date_unix` column
#'
#' @return Numeric vector
#' @export
#'
#' @examples
#' \dontrun{
#' get_last_timestamp(mydata, 'date_unix')
#' }
get_last_timestamp <- function(scrobbles_df, timestamp_column){
if (is.data.frame(scrobbles_df) == FALSE) {
stop("`scrobbles_df`` must be a dataframe", call. = FALSE)
}
time_vec <- scrobbles_df[,timestamp_column]
# if (!is.numeric(time_vec)){
# stop("timestamp_column column must be numeric", call. = FALSE)
# }
time_vec <- sort(time_vec, decreasing = TRUE)
last_timestamp <- time_vec[1]
return(last_timestamp)
}