Spaces:
Sleeping
Sleeping
Update app.R
Browse files
app.R
CHANGED
|
@@ -20,7 +20,7 @@ library(gridExtra)
|
|
| 20 |
library(png)
|
| 21 |
library(xgboost)
|
| 22 |
library(httr)
|
| 23 |
-
|
| 24 |
|
| 25 |
pdf(file = NULL)
|
| 26 |
Sys.setenv(TZ='EST')
|
|
@@ -28,12 +28,24 @@ Sys.setenv(TZ='EST')
|
|
| 28 |
# Helper functions
|
| 29 |
download_and_process_image <- function(url) {
|
| 30 |
tryCatch({
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
}, error = function(e) {
|
| 38 |
warning(paste("Error processing image:", e$message))
|
| 39 |
return(NULL)
|
|
@@ -453,23 +465,13 @@ server <- function(input, output, session) {
|
|
| 453 |
mlb_url <- paste0("https://midfield.mlbstatic.com/v1/people/", id, "/mlb/300?circle=false")
|
| 454 |
milb_url <- paste0("https://midfield.mlbstatic.com/v1/people/", id, "/milb/300?circle=false")
|
| 455 |
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
NULL
|
| 460 |
-
})
|
| 461 |
-
|
| 462 |
-
if (is.null(img_file)) {
|
| 463 |
-
img_file <- tryCatch({
|
| 464 |
-
download_and_process_image(milb_url)
|
| 465 |
-
}, error = function(e) {
|
| 466 |
-
NULL
|
| 467 |
-
})
|
| 468 |
}
|
| 469 |
|
| 470 |
-
if (!is.null(
|
| 471 |
-
|
| 472 |
-
img_grob <- rasterGrob(img, interpolate = TRUE)
|
| 473 |
} else {
|
| 474 |
img_grob <- textGrob("Image not available", gp = gpar(col = "red", fontsize = 20))
|
| 475 |
}
|
|
|
|
| 20 |
library(png)
|
| 21 |
library(xgboost)
|
| 22 |
library(httr)
|
| 23 |
+
library(jpeg)
|
| 24 |
|
| 25 |
pdf(file = NULL)
|
| 26 |
Sys.setenv(TZ='EST')
|
|
|
|
| 28 |
# Helper functions
|
| 29 |
download_and_process_image <- function(url) {
|
| 30 |
tryCatch({
|
| 31 |
+
response <- GET(url)
|
| 32 |
+
content_type <- http_type(response)
|
| 33 |
+
|
| 34 |
+
if (content_type %in% c("image/png", "image/jpeg")) {
|
| 35 |
+
temp_file <- tempfile(fileext = ifelse(content_type == "image/png", ".png", ".jpg"))
|
| 36 |
+
writeBin(content(response, "raw"), temp_file)
|
| 37 |
+
|
| 38 |
+
if (content_type == "image/png") {
|
| 39 |
+
img <- readPNG(temp_file)
|
| 40 |
+
} else {
|
| 41 |
+
img <- readJPEG(temp_file)
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
return(list(img = img, type = content_type))
|
| 45 |
+
} else {
|
| 46 |
+
warning(paste("Unsupported image type:", content_type))
|
| 47 |
+
return(NULL)
|
| 48 |
+
}
|
| 49 |
}, error = function(e) {
|
| 50 |
warning(paste("Error processing image:", e$message))
|
| 51 |
return(NULL)
|
|
|
|
| 465 |
mlb_url <- paste0("https://midfield.mlbstatic.com/v1/people/", id, "/mlb/300?circle=false")
|
| 466 |
milb_url <- paste0("https://midfield.mlbstatic.com/v1/people/", id, "/milb/300?circle=false")
|
| 467 |
|
| 468 |
+
img_result <- download_and_process_image(mlb_url)
|
| 469 |
+
if (is.null(img_result)) {
|
| 470 |
+
img_result <- download_and_process_image(milb_url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 471 |
}
|
| 472 |
|
| 473 |
+
if (!is.null(img_result)) {
|
| 474 |
+
img_grob <- rasterGrob(img_result$img, interpolate = TRUE)
|
|
|
|
| 475 |
} else {
|
| 476 |
img_grob <- textGrob("Image not available", gp = gpar(col = "red", fontsize = 20))
|
| 477 |
}
|