-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rajit Khatri
authored and
Rajit Khatri
committed
Nov 5, 2024
1 parent
fba5297
commit 1e38147
Showing
3 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import type { GetRecentlyPlayedTracksResponse, GetRecentlyPlayedTracksInput, AudioFeaturesResponse } from "../types/spotify-web-api"; | ||
import getMultiTrackAudioFeatures from "./multiTrackAudioFeaturesService"; | ||
import type { HistoricalMetrics } from "../types/enhancify"; | ||
|
||
async function getRecentlyPlayedTracksMetrics(apiOptions: GetRecentlyPlayedTracksInput) : Promise<HistoricalMetrics | {}> { | ||
let url = "https://api.spotify.com/v1/me/player/recently-played"; | ||
|
||
// Extract the query parameters from the API options input | ||
// to create a request string | ||
let queryParams: string[] = []; | ||
for (const [key, value] of Object.entries(apiOptions.data)) { | ||
if (!value) { | ||
continue; | ||
} | ||
queryParams.push(key + "=" + value); | ||
} | ||
url += queryParams.join("&"); | ||
|
||
var accessToken = Spicetify.Platform.Session.accessToken; | ||
|
||
// Make the API request to get the recently | ||
// played songs | ||
let RecentlyPlayedTracksResponse = await fetch(url, | ||
{ | ||
headers: { | ||
Authorization: "Bearer " + accessToken, | ||
}, | ||
|
||
} | ||
); | ||
|
||
// If the respone was not successfully retrieved, | ||
// then return an empty object | ||
if (RecentlyPlayedTracksResponse.status != 200) { | ||
return {}; | ||
} | ||
|
||
// Get the musical characteristics of each song | ||
// Get all the song ids | ||
let songIDs: string[] = []; | ||
let RecentlyPlayedTracksResponseJSON = await RecentlyPlayedTracksResponse.json(); | ||
for (let item of RecentlyPlayedTracksResponseJSON.items) { | ||
songIDs.push(item.track.id); | ||
} | ||
let audioFeatures = await getMultiTrackAudioFeatures(songIDs); | ||
|
||
// Process the audio features | ||
let relevantMetrics = ["acousticness", "danceability", "energy", "instrumentalness", | ||
"liveness", "speechiness", "valence"]; | ||
let metricsData: { [metric: string]: {total: number, count: number} } = {}; | ||
for (let metric of relevantMetrics) { | ||
metricsData[metric] = {total: 0, count: 0}; | ||
} | ||
for (let audioFeature of audioFeatures) { | ||
for (let feature of Object.keys(audioFeature)) { | ||
if (feature in relevantMetrics) { | ||
if (audioFeature[feature as keyof AudioFeaturesResponse]) { | ||
metricsData[feature].total += parseInt(audioFeature[feature as keyof AudioFeaturesResponse]); | ||
metricsData[feature].count += 1; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Average out the song metrics | ||
let result: HistoricalMetrics = { | ||
acousticness: { | ||
average: 0, | ||
count: 0 | ||
}, | ||
danceability: { | ||
average: 0, | ||
count: 0 | ||
}, | ||
energy: { | ||
average: 0, | ||
count: 0 | ||
}, | ||
instrumentalness: { | ||
average: 0, | ||
count: 0 | ||
}, | ||
liveness: { | ||
average: 0, | ||
count: 0 | ||
}, | ||
speechiness: { | ||
average: 0, | ||
count: 0 | ||
}, | ||
valence: { | ||
average: 0, | ||
count: 0 | ||
} | ||
}; | ||
for (let metric in Object.keys(metricsData)) { | ||
result[metric as keyof HistoricalMetrics].average = metricsData[metric].total / metricsData[metric].count; | ||
result[metric as keyof HistoricalMetrics].count = metricsData[metric].count; | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters