From b530e901f28bb63bf12ea27e5d28c78bd34cd3c1 Mon Sep 17 00:00:00 2001 From: MhmmtAliFDN Date: Sat, 9 Dec 2023 20:35:06 +0300 Subject: [PATCH 1/7] =?UTF-8?q?Ozellik=201=20kodland=C4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- coderlog/Labex_Q1.R | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 coderlog/Labex_Q1.R diff --git a/coderlog/Labex_Q1.R b/coderlog/Labex_Q1.R new file mode 100644 index 0000000..8f6e578 --- /dev/null +++ b/coderlog/Labex_Q1.R @@ -0,0 +1,30 @@ +library(httr) + +spotify_token <- function() { + endpoint_uri <- "https://accounts.spotify.com/api/token" + + body <- list( + grant_type = "client_credentials", + client_id = (Sys.getenv("SPOTIFY_ID")), + client_secret = (Sys.getenv("SPOTIFY_SECRET")) + ) + + response <- POST( + url = endpoint_uri, + body = body, + add_headers("Content-Type" = "application/x-www-form-urlencoded"), + encode = "form" + ) + + status_code <- status_code(response) + token <- content(response)$access_token + bearer_token <- paste("Bearer", token) + + result <- list( + status_code <- status_code, + token <- bearer_token + ) + + return(result) +} + From de53c4beb79cf46275e1b7115dd923ee4e88dab3 Mon Sep 17 00:00:00 2001 From: MhmmtAliFDN Date: Sat, 9 Dec 2023 20:45:32 +0300 Subject: [PATCH 2/7] =?UTF-8?q?Ozellik=201,=20testleri=20ile=20tamamland?= =?UTF-8?q?=C4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- coderlog/Labex_Q1_tests.R | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 coderlog/Labex_Q1_tests.R diff --git a/coderlog/Labex_Q1_tests.R b/coderlog/Labex_Q1_tests.R new file mode 100644 index 0000000..30544d1 --- /dev/null +++ b/coderlog/Labex_Q1_tests.R @@ -0,0 +1,22 @@ +library(testthat) + +rm(list = ls()) +file_path <- "Labex_Q1.R" +source(file_path) + +result <- spotify_token() + +#--------------------------------Test 1.1--------------------------------# +test_that("Global Workspace'de spotify_token adlı bir değişken olmalı", { + expect_true(exists("spotify_token")) +}) + +#--------------------------------Test 1.2--------------------------------# +test_that("spotify_token adlı değişken bir 'function' tipinde olmalı", { + expect_true(is.function(spotify_token)) +}) + +#--------------------------------Test 1.3--------------------------------# +test_that("spotify_token() çağrıldığında döndürdüğü çıktı bir liste olmalı", { + expect_true(is.list(result)) +}) \ No newline at end of file From 98186e672c84e213c46898ceeeeba6b5a594e28b Mon Sep 17 00:00:00 2001 From: MhmmtAliFDN Date: Sat, 9 Dec 2023 21:05:05 +0300 Subject: [PATCH 3/7] =?UTF-8?q?Ozellik=202=20kodland=C4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- coderlog/Labex_Q2.R | 68 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 coderlog/Labex_Q2.R diff --git a/coderlog/Labex_Q2.R b/coderlog/Labex_Q2.R new file mode 100644 index 0000000..f543bcf --- /dev/null +++ b/coderlog/Labex_Q2.R @@ -0,0 +1,68 @@ +library(httr) + +#----------------- Spotify Token Alma -----------------# +spotify_token <- function() { + endpoint_uri <- "https://accounts.spotify.com/api/token" + + body <- list( + grant_type = "client_credentials", + client_id = (Sys.getenv("SPOTIFY_ID")), + client_secret = (Sys.getenv("SPOTIFY_SECRET")) + ) + + response <- POST( + url = endpoint_uri, + body = body, + add_headers("Content-Type" = "application/x-www-form-urlencoded"), + encode = "form" + ) + + status_code <- status_code(response) + token <- content(response)$access_token + bearer_token <- paste("Bearer",token) + + result <- list( + status_code = status_code, + token = bearer_token + ) + + return(result) +} +#token <- spotify_token() +#print(token) + + + +#----------------- Sanatçı Adı ile Çıkan Sonuçları Listeleme -----------------# +spotify_search_artist <- function(artist_name) { + if(!is.character(artist_name)) stop("Artist name must be character type."); + + search_url <- paste0( + "https://api.spotify.com/v1/search?q=", URLencode(artist_name), + "&type=artist&limit=",5) + token <- spotify_token() + + response <- GET( + url = search_url, + add_headers("Authorization" = token$token) + ) + + status_code <- status_code(response) + search_result <- content(response, type = "application/json") + artists <- search_result$artists$items[seq_len(5)] + + search_results <- data.frame( + artist = sapply(artists, function(x) x$name), + id = sapply(artists, function(x) x$id) + ) + + result <- list( + status_code = status_code, + search_results = search_results + ) + + return(result) +} +artists <- spotify_search_artist("Eminem") +print(artists) + From 4e894869dc043578bd97f80749de0976b9449836 Mon Sep 17 00:00:00 2001 From: MhmmtAliFDN Date: Sat, 9 Dec 2023 21:07:09 +0300 Subject: [PATCH 4/7] =?UTF-8?q?Ozellik=202=20testleri=20ile=20tamamland?= =?UTF-8?q?=C4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- coderlog/Labex_Q2_tests.R | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 coderlog/Labex_Q2_tests.R diff --git a/coderlog/Labex_Q2_tests.R b/coderlog/Labex_Q2_tests.R new file mode 100644 index 0000000..72bcebe --- /dev/null +++ b/coderlog/Labex_Q2_tests.R @@ -0,0 +1,22 @@ +library(testthat) + +rm(list = ls()) +file_path <- "Labex_Q2.R" +source(file_path) + +result <- spotify_search_artist("The Doors") + +#--------------------------------Test 2.1--------------------------------# +test_that("Global Workspace’de spotify_search_artist adlı bir değişken olmalı.",{ + expect_true(exists("spotify_search_artist")) +}) + +#--------------------------------Test 2.2--------------------------------# +test_that("spotify_search_artist adlı değişkenin tipi “function” olmalı.",{ + expect_is(spotify_search_artist, "function") +}) + +#--------------------------------Test 2.3--------------------------------# +test_that("spotify_search_artist() herhangi bir artist ismi ile çağrıldığında döndürdüğü çıktı bir liste olmalı.",{ + expect_is(result, "list") +}) \ No newline at end of file From 440d1b8e5335f07811d34a10093006cc97b084aa Mon Sep 17 00:00:00 2001 From: MhmmtAliFDN Date: Sat, 9 Dec 2023 21:21:02 +0300 Subject: [PATCH 5/7] =?UTF-8?q?Ozellik=203=20tamamland=C4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- coderlog/Labex_Q3.R | 101 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 coderlog/Labex_Q3.R diff --git a/coderlog/Labex_Q3.R b/coderlog/Labex_Q3.R new file mode 100644 index 0000000..7b5bee9 --- /dev/null +++ b/coderlog/Labex_Q3.R @@ -0,0 +1,101 @@ +library(httr) + +#----------------- Spotify Token Alma -----------------# +spotify_token <- function() { + endpoint_uri <- "https://accounts.spotify.com/api/token" + + body <- list( + grant_type = "client_credentials", + client_id = (Sys.getenv("SPOTIFY_ID")), + client_secret = (Sys.getenv("SPOTIFY_SECRET")) + ) + + response <- POST( + url = endpoint_uri, + body = body, + add_headers("Content-Type" = "application/x-www-form-urlencoded"), + encode = "form" + ) + + status_code <- status_code(response) + token <- content(response)$access_token + bearer_token <- paste("Bearer",token) + + result <- list( + status_code = status_code, + token = bearer_token + ) + + return(result) +} +#token <- spotify_token() +#print(token) + + + +#----------------- Sanatçı Adı ile Çıkan Sonuçları Listeleme -----------------# +spotify_search_artist <- function(artist_name) { + if(!is.character(artist_name)) stop("Artist name must be character type."); + + search_url <- paste0( + "https://api.spotify.com/v1/search?q=", URLencode(artist_name), + "&type=artist&limit=",5) + token <- spotify_token() + + response <- GET( + url = search_url, + add_headers("Authorization" = token$token) + ) + + status_code <- status_code(response) + search_result <- content(response, type = "application/json") + artists <- search_result$artists$items[seq_len(5)] + + search_results <- data.frame( + artist = sapply(artists, function(x) x$name), + id = sapply(artists, function(x) x$id) + ) + + result <- list( + status_code = status_code, + search_results = search_results + ) + + return(result) +} +#artists <- spotify_search_artist("Eminem") +#print(artists) + + + +#----------------- Sanatçı ID ile Popüler Şarkıları Çekme -----------------# +spotify_artist_top_tracks <- function(artist_id) { + endpoint_uri <- paste0("https://api.spotify.com/v1/artists/", artist_id, + "/top-tracks?market=US") + token <- spotify_token() + + response <- GET( + url = endpoint_uri, + add_headers('Authorization' = token$token) + ) + + status_code <- status_code(response) + search_result <- content(response, type = "application/json") + + tracks_list <- lapply(search_result$tracks, function(track) { + list( + id = track$id, + name = track$name, + artist = track$artists[[1]]$name, + album = track$album$name, + year = substr(track$album$release_date, 1, 10) + ) + }) + + tracks <- list( + status_code <- status_code, + resultdf <- as.data.frame(do.call(rbind, tracks_list), stringAsFactors=FALSE) + ) + + return(tracks) +} From 7dfe36adb9042fe1c9249e7747621fbb4b49313e Mon Sep 17 00:00:00 2001 From: MhmmtAliFDN Date: Sat, 9 Dec 2023 21:23:23 +0300 Subject: [PATCH 6/7] =?UTF-8?q?Ozellik=203=20testleri=20ile=20tamamland?= =?UTF-8?q?=C4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- coderlog/Labex_Q3_tests.R | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 coderlog/Labex_Q3_tests.R diff --git a/coderlog/Labex_Q3_tests.R b/coderlog/Labex_Q3_tests.R new file mode 100644 index 0000000..6f22690 --- /dev/null +++ b/coderlog/Labex_Q3_tests.R @@ -0,0 +1,22 @@ +library(testthat) + +rm(list = ls()) +file_path <- "Labex_Q3.R" +source(file_path) + +result <- spotify_artist_top_tracks("0TnOYISbd1XYRBk9myaseg") + +#--------------------------------Test 1.1--------------------------------# +test_that("Global Workspace’de spotify_artist_top_tracks adlı bir değişken olmalı.",{ + expect_true(exists("spotify_artist_top_tracks")) +}) + +#--------------------------------Test 1.2--------------------------------# +test_that("spotify_artist_top_tracks adlı değişkenin tipi “function” olmalı.",{ + expect_is(spotify_artist_top_tracks, "function") +}) + +#--------------------------------Test 1.3--------------------------------# +test_that("spotify_artist_top_tracks() herhangi bir artist_id ile çağrıldığında döndürdüğü çıktı bir list olmalı.",{ + expect_true(is.list(result)) +}) \ No newline at end of file From bb1bcc7097514682830a59734ec47ad4b01e8cf7 Mon Sep 17 00:00:00 2001 From: MhmmtAliFDN Date: Sat, 9 Dec 2023 21:32:43 +0300 Subject: [PATCH 7/7] =?UTF-8?q?Ozellik=204=20tamamland=C4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- coderlog/Labex_Q4.R | 115 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 coderlog/Labex_Q4.R diff --git a/coderlog/Labex_Q4.R b/coderlog/Labex_Q4.R new file mode 100644 index 0000000..c5d54f1 --- /dev/null +++ b/coderlog/Labex_Q4.R @@ -0,0 +1,115 @@ +library(httr) + +#----------------- Spotify Token Alma -----------------# +spotify_token <- function() { + endpoint_uri <- "https://accounts.spotify.com/api/token" + + body <- list( + grant_type = "client_credentials", + client_id = (Sys.getenv("SPOTIFY_ID")), + client_secret = (Sys.getenv("SPOTIFY_SECRET")) + ) + + response <- POST( + url = endpoint_uri, + body = body, + add_headers("Content-Type" = "application/x-www-form-urlencoded"), + encode = "form" + ) + + status_code <- status_code(response) + + if (status_code == 200) { + token <- content(response)$access_token + bearer_token <- paste("Bearer",token) + + result <- list( + status_code = status_code, + token = bearer_token + ) + + return(result) + } else + { + error <- content(response) + return(error) + } +} +token <- spotify_token() +print(token) + + + +#----------------- Sanatçı Adı ile Çıkan Sonuçları Listeleme -----------------# +spotify_search_artist <- function(artist_name) { + if(!is.character(artist_name)) stop("Artist name must be character type."); + + search_url <- paste0( + "https://api.spotify.com/v1/search?q=", URLencode(artist_name), + "&type=artist&limit=",5) + token <- spotify_token() + + response <- GET( + url = search_url, + add_headers("Authorization" = token$token) + ) + + status_code <- status_code(response) + + if (status_code == 200) { + search_result <- content(response, type = "application/json") + artists <- search_result$artists$items[seq_len(5)] + + search_results <- data.frame( + artist = sapply(artists, function(x) x$name), + id = sapply(artists, function(x) x$id) + ) + + result <- list( + status_code = status_code, + search_results = search_results + ) + + return(result) + } + else { + error <- content(response) + return(error) + } +} +artists <- spotify_search_artist("Eminem") +print(artists) + + + +#----------------- Sanatçı ID ile Popüler Şarkıları Çekme -----------------# +spotify_artist_top_tracks <- function(artist_id) { + endpoint_uri <- paste0("https://api.spotify.com/v1/artists/", artist_id, + "/top-tracks?market=US") + token <- spotify_token() + + response <- GET( + url = endpoint_uri, + add_headers('Authorization' = token$token) + ) + + status_code <- status_code(response) + search_result <- content(response, type = "application/json") + + tracks_list <- lapply(search_result$tracks, function(track) { + list( + id = track$id, + name = track$name, + artist = track$artists[[1]]$name, + album = track$album$name, + year = substr(track$album$release_date, 1, 10) + ) + }) + + tracks <- list( + status_code <- status_code, + resultdf <- as.data.frame(do.call(rbind, tracks_list), stringAsFactors=FALSE) + ) + + return(tracks) +}