-
Notifications
You must be signed in to change notification settings - Fork 2
/
global.R
135 lines (117 loc) · 4.55 KB
/
global.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
#### Loading libraries ####
source("dependencies/package-check.R")
## Set API key ##
if (is.null(getOption("trakt.client.id"))) {
get_trakt_credentials()
}
#### Set/find/create/save cache dir ####
cacheDir <- "cache"
if (!file.exists(cacheDir)) {
dir.create(cacheDir)
}
cache_titles <- function(showindex, cache_dir){
titles <- file.path(cache_dir, "showtitles.rds")
if (!file.exists(titles)) {
showindex$requests <- 1
saveRDS(showindex, file = titles)
} else {
temp <- readRDS(file = titles)
if (!(showindex$id %in% temp$id)) {
showindex$requests <- 1
temp <- rbind(temp, showindex)
temp$title <- as.character(temp$title)
temp <- plyr::arrange(temp, title)
saveRDS(temp, file = titles)
} else {
temp$requests[as.character(temp$id) == as.character(showindex$id)] <- temp$requests[as.character(temp$id) == as.character(showindex$id)] + 1
saveRDS(temp, file = titles)
}
}
}
## Fix cache index
fix_cached_index <- function(cacheDir = "cache"){
cached <- sub(".rds", "", dir(cacheDir), ignore.case = T)
for (id in cached) {
if (id == "showtitles") { next }
show <- trakt.show.summary(id)
index <- data.frame(title = show$title, id = show$ids$slug)
cache_titles(index, cacheDir)
}
}
reset_title_cache <- function(cacheDir = "cache"){
temp <- readRDS(file.path(cacheDir, "showtitles.rds"))
temp$title <- as.character(temp$title)
temp <- plyr::arrange(temp, title)
temp$requests <- 0
saveRDS(temp, file.path(cacheDir, "showtitles.rds"))
}
#### Setting some values ####
## Define some HTML characters
bullet <- HTML("•")
mu <- HTML("μ")
sigma <- HTML("σ")
## UI elements ##
btn.scale.x.choices <- c("Total Episode Numbers" = "epnum",
"Episodes per Season" = "episode",
"Airdate" = "first_aired")
btn.scale.y.choices <- c("Rating" = "rating",
"Votes" = "votes")
table.episodes.columns <- c("epnum", "epid", "title", "first_aired.string", "rating", "votes")
table.episodes.names <- c("#", "Episode ID", "Title", "Airdate", "Rating", "Votes")
table.seasons.columns <- c("season", "episode_count", "rating", "votes", "avg.rating.season", "rating.sd", "top.rating.episode", "lowest.rating.episode")
table.seasons.names <- c("Season", "Episodes", "Rating", "Votes", "Average Rating", "Episode sd", "Highest Rating", "Lowest Rating")
#### Helper functions ####
make_tooltip <- function(show.episodes, keyvar = "tooltip"){
strings <- paste0("<strong>", show.episodes$epid, "</strong><br />",
"<strong>Title:</strong> ", show.episodes$title, "<br />",
"<strong>Aired:</strong> ", show.episodes$first_aired.string, "<br />",
"<strong>Rating:</strong> ", show.episodes$rating, "<br />",
"<strong>Votes:</strong> ", show.episodes$votes)
show.episodes[[keyvar]] <- strings
return(show.episodes)
}
## Get season average ratings etc
get_season_ratings <- function(show.episodes = NULL, show.seasons = NULL){
if (is.null(show.episodes) | is.null(show.seasons)){
stop("You need to provide episode and season datasets")
}
seasons <- plyr::join(show.seasons,
plyr::ddply(show.episodes, .(season), plyr::summarize,
avg.rating.season = round(mean(rating), 1),
rating.sd = round(sd(rating), 2),
top.rating.episode = max(rating),
lowest.rating.episode = min(rating)))
seasons$season <- factor(seasons$season, ordered = T)
return(seasons)
}
#### Get posters from fanart.tv ####
# tvdbid <- 353764
get_fanart_poster <- function(tvdbid, api_key = "113407042401248f50123d1c112abf0d") {
query <- paste0("https://webservice.fanart.tv/v3/tv/", tvdbid, "?api_key=", api_key)
ret <- httr::content(httr::GET(query))
ret_url <- ret$tvposter[[1]]$url
if (is.character(ret_url) & nchar(ret_url) > 10) {
return(ret_url)
} else {
message("Possibly broken fanart: ", tvdbid)
return("")
}
}
#### JS for button clicks ####
jscode <- '
$(function() {
var $els = $("[data-proxy-click]");
$.each(
$els,
function(idx, el) {
var $el = $(el);
var $proxy = $("#" + $el.data("proxyClick"));
$el.keydown(function (e) {
if (e.keyCode == 13) {
$proxy.click();
}
});
}
);
});
'