Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,45 @@ Package: YTExploreR
Title: What the Package Does (One Line, Title Case)
Version: 0.0.0.9000
Authors@R:
person("Karthiga Sethu", "Sethuramalingam", , "karthigaksk@gmail.com", role = c("aut", "cre"),
c(person("Karthiga Sethu", "Sethuramalingam","karthigaksk@gmail.com", role = c("aut", "cre"),
person("Andrew", "Sarracini", "andrewsarracini1@gmail.com", role = c("aut", "cre"),
person("Nayeli", "Montiel", "naye.montiel.1806@gmail.com", role = c("aut", "cre")))


comment = c(ORCID = "YOUR-ORCID-ID"))
Description: What the package does (one paragraph).
License: GPL (>= 3)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Imports:
tidyverse,
tidyr,
ggrepel,
ggtext,
usethis,
viridis,
ggplot2,
dplyr,
httr,
jsonlite,
stringr
stringr,
lubridate
Collate:
'YTExploreR-package.R'
'YTExploreR_get_uploads.R'
'access_top10_videos.R'
'get_channel_stats.R'
'video_category_util.R'
'video_details_util.R'
'preference_analysis.R'
'rank_and_analyse_top_videos.R'
<<<<<<< Updated upstream
=======
'channel_comparator_plots.R'
'display_individual_channel_stats.R'
'get_video_ids.R'
'visualize_hourly_seasonal.R'
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
>>>>>>> Stashed changes
34 changes: 21 additions & 13 deletions R/YTExploreR_get_uploads.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
library(tidyverse)
library(httr)
library(dplyr)
library(jsonlite)
library(ggplot2)
library(usethis)


#' Date Formatter
Expand Down Expand Up @@ -35,11 +29,13 @@ format_date <- function(month, year) {
#' @import httr
#' @import jsonlite
#' @import dplyr
#' @import tidyr
#'

get_monthly_uploads <- function(chan_id, year) {

api_key <- Sys.getenv("YOUTUBE_API")
# api_key <- Sys.getenv("YOUTUBE_API")
api_key <- "AIzaSyB7eL6rh3LTKBm6rYyCDdWUFraAArhmntQ"

uploads_df <- data.frame()

Expand Down Expand Up @@ -101,7 +97,7 @@ get_monthly_uploads <- function(chan_id, year) {
#'
#' @return A ggplot2 line plot displaying the monthly upload activity OR a single string explaining that the year is out of scope
#'
#' @import ggplot
#' @import ggplot2
#' @import dplyr
#'

Expand All @@ -120,11 +116,16 @@ visualize_monthly_uploads <- function(uploads_df) {

monthly_summary <- all_months %>%
left_join(uploads_df %>% group_by(month, year) %>% summarise(upload_count = n()), by = c("month", "year")) %>%
mutate(upload_count = replace_na(upload_count, 0))
dplyr::mutate(upload_count = tidyr::replace_na(upload_count, 0))

ggplot(monthly_summary, aes(x = month, y = upload_count, group = year)) +
geom_line(linetype = "solid") +
geom_point() +
average_upload <- monthly_summary %>%
group_by(month) %>%
summarise(avg_upload_count = mean(upload_count, na.rm = TRUE))


monthly_line <- ggplot(monthly_summary, aes(x = month, y = upload_count, group = year)) +
geom_line(linetype = "solid", linewidth = 1.5, color="blue") +
geom_point(size=2, color="black") +
labs(title = paste("Monthly Upload Activity for", unique(uploads_df$channelTitle), "in", unique(uploads_df$year)),
x = "",
y = "Channel Upload Activity") +
Expand All @@ -133,8 +134,15 @@ visualize_monthly_uploads <- function(uploads_df) {
labels = month.name) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
ylim(0, max(monthly_summary$upload_count))

combined_plot <- monthly_line +
annotate("segment", x = min(monthly_summary$month), xend = max(monthly_summary$month),
y = mean(monthly_summary$upload_count), yend = mean(monthly_summary$upload_count),
linetype = "dashed", color = "red", size = 1)

combined_plot
}

annual_uploads <- get_monthly_uploads('UCQKnyICqWksz8ygILHS01gQ', 2019)
annual_uploads <- get_monthly_uploads('UCQKnyICqWksz8ygILHS01gQ', 2020)
visualize_monthly_uploads(annual_uploads)

48 changes: 0 additions & 48 deletions R/get_video_ids.R

This file was deleted.

13 changes: 6 additions & 7 deletions R/visualize_hourly_seasonal.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@
#' @import ggtext
#' @import gridExtra
#' @import viridis
#' @import lubridate
#'

visualize_hourly_patterns <- function(uploads_df) {
if (length(uploads_df) == 1) {
return ("No data available for the selected year")
}

uploads_df$publishedAt <- as_datetime(uploads_df$publishedAt)
uploads_df$hour <- hour(uploads_df$publishedAt)
uploads_df$month <- month(uploads_df$publishedAt)
uploads_df$year <- year(uploads_df$publishedAt)
uploads_df$publishedAt <- lubridate::as_datetime(uploads_df$publishedAt)
uploads_df$hour <- lubridate::hour(uploads_df$publishedAt)
uploads_df$month <- lubridate::month(uploads_df$publishedAt)
uploads_df$year <- lubridate::year(uploads_df$publishedAt)
uploads_df$season <- cut(uploads_df$month,
breaks = c(0, 3, 6, 9, 12),
labels = c("Winter", "Spring", "Summer", "Fall"),
Expand All @@ -42,8 +43,6 @@ visualize_hourly_patterns <- function(uploads_df) {
theme(legend.position = "none")
}

visualize_hourly_patterns(get_monthly_uploads("UCyPYQTT20IgzVw92LDvtClw", 2022))


visualize_hourly_patterns(get_monthly_uploads("UCqFMzb-4AUf6WAIbl132QKA", 2023))


47 changes: 47 additions & 0 deletions tests/testthat/test-video_category_util.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
test_that("Extract_Time_in_mins works", {
expect_equal(Extract_Time_in_mins("PT1H30S"), 60.5)
})

test_that("Get_Video_Detail", {
expect_equal(nrow(Get_Video_Detail("Ks-_Mh1QhMc", "AIzaSyBqrBJzAuitb-PpfyPrV7ABbLn8_nIbK3c")), 1)
})

test_that("Get_Video_Category", {
expect_equal(nrow(Get_Video_Category("22,20", "AIzaSyBqrBJzAuitb-PpfyPrV7ABbLn8_nIbK3c")), 2)
})

# Testing get_monthly_uploads
test_that("YTExploreR_get_uploads returns the correct row", {
monthly_uploads_result <- get_monthly_uploads('UCQKnyICqWksz8ygILHS01gQ', 2019)[1,1]
expect_equal(monthly_uploads_result, "2019-01-30T18:11:57+00:00")
})

# Further testing get_monthly_uploads
test_that("get_monthly_uploads returns expected data", {
monthly_uploads_nrow <- nrow(get_monthly_uploads("UCQKnyICqWksz8ygILHS01gQ", 2020))
expect_equal(monthly_uploads_nrow, 454)
})

# Testing format_date
test_that("format_date returns correctly formatted date range", {
expected_start_date <- "2020-1-01T00:00:00Z"
expected_end_date <- "2020-2-01T00:00:00Z"

result <- format_date(1, 2020)

expect_equal(result$start_date, expected_start_date)
expect_equal(result$end_date, expected_end_date)
})

# Testing visualize_hourly_seasonal
test_that("visualize_hourly_patterns generates a ggplot object", {
visualize_hourly_result <- visualize_hourly_patterns(get_monthly_uploads("UCqFMzb-4AUf6WAIbl132QKA", 2022))
expect_true(inherits(visualize_hourly_result, "gg"))
})

# Testing visualize_monthly_uploads return inherit object
test_that("visualize_monthly_uploads generates a ggplot object", {
visualize_monthly_result <- visualize_monthly_uploads(get_monthly_uploads("UCqFMzb-4AUf6WAIbl132QKA", 2022))
expect_true(inherits(visualize_monthly_result, "gg"))
})