-
Notifications
You must be signed in to change notification settings - Fork 0
/
taxonomy.R
59 lines (42 loc) · 1.47 KB
/
taxonomy.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
library(jsonlite)
library(plyr)
library(httr)
library(lubridate)
save_taxonomy <- function(etsyAPIkey, filename = ""){
url <- paste("https://openapi.etsy.com/v2/taxonomy/buyer/get?api_key=", etsyAPIkey, sep = "")
taxonomy <- GET(url)
taxonomy_json <- content(taxonomy, as = "text")
if(str_length(filename) == 0 ) {
filename <- paste("taxonomy", format(today(), format="%d%b%Y"), sep = "_")
filename <- paste(filename, "json", sep=".")
}
write(taxonomy_json, filename)
}
load_taxonomy <- function(file){
rawdata <- fromJSON(file)
taxonomy <- flatten_taxonomy(NULL, rawdata[[2]])
return(taxonomy)
}
flatten_taxonomy <- function(flattened, nested) {
if(is.list(nested) & length(nested) == 0 ){
return(flattened)
}
tmp <- nested
#flatten ID lists
tmp$children_ids <- unlist(lapply(nested$children_ids, paste0, collapse = ','))
tmp$full_path_taxonomy_ids <- unlist(lapply(nested$full_path_taxonomy_ids, paste0, collapse = ','))
tmp$children <- NULL
tmp$nav <- NULL
tmp$is_supplies_top_level <- NULL
#remove factors
tmp[] <- lapply(tmp, as.character)
if(is.null(flattened)){
flattened <- tmp
} else {
flattened <- rbind(flattened, tmp)
}
children <- adply(nested$children, c(1), function(x) flatten_taxonomy(NULL, x))
children$X1 <- NULL #additional column generated by adply
flattened <- rbind(flattened, children)
return(flattened)
}