-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinfo.R
executable file
·73 lines (63 loc) · 1.8 KB
/
info.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
#' tk_info
#' @description Function to get information on a given user/hashtag/piece of music
#' @param scope Character indicating the endpoint to scrape (must be "hashtag", "user"
#' or "music")
#' @param query Character indicating the username/hashtag/music_id to scrape
#' @export
#' @examples
#'
#' \dontrun{
#' # Get info about a user:
#' tk_info(scope = "user", query = "willsmith")
#' # Get info about a hashtag
#' tk_info(scope = "hashtag", query = "cosplay")
#' }
tk_info <- function(scope, query, ...){
res <- switch(
scope,
"user" = {
url <- get_url("username", query_1 = query)
tmp <- get_data(url, ...)
if("found" %in% names(tmp)) return(tibble::tibble(query = query, found = F))
tmp$userInfo
},
"hashtag" = {
url <- get_url("hashtag", query_1 = query)
tmp <- get_data(url, ...)
if("found" %in% names(tmp)) return(tibble::tibble(query = query, found = F))
tmp
},
"music" = {
tmp <- tk_posts(scope = "music", query = query, n = 1, ...)
if("found" %in% names(tmp)){
return(tibble::tibble(query = query, found = F))
}
tmp
},
"post" = {
url <- get_url("post", query_1 = query)
tmp <- get_data(url, ...)
if("found" %in% names(tmp)){
return(tibble::tibble(query = query, found = F))
}
tmp
}
)
if("found" %in% names(res)){
return(tibble::tibble(query = query, found = F))
}
if(is.null(res)){
return(tibble::tibble(query = query, found = F))
}
out <- res %>%
rlist::list.flatten() %>%
purrr::imap_dfc(~{
if(length(.x) == 1){
return(tibble::tibble(.x) %>% purrr::set_names(.y))
} else {
return(tibble::tibble(list(.x)) %>% purrr::set_names(.y))
}
}) %>%
dplyr::mutate(query = query)
return(out)
}