-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from studiowebux/5-cannot-read-properties-error
fix: added error handling, not tested yet
- Loading branch information
Showing
9 changed files
with
422 additions
and
166 deletions.
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 |
---|---|---|
|
@@ -16,7 +16,7 @@ main.js | |
*.map | ||
|
||
# obsidian | ||
data.json | ||
data.json* | ||
|
||
# Exclude macOS Finder (System Explorer) View States | ||
.DS_Store |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { SpotifyLinkSettings } from "./types"; | ||
|
||
export const DEFAULT_SETTINGS: SpotifyLinkSettings = { | ||
spotifyClientId: "", | ||
spotifyClientSecret: "", | ||
spotifyScopes: "user-read-currently-playing", | ||
spotifyState: "it-can-be-anything", | ||
templates: [ | ||
"**Song Name:** {{ song_name }}\n**Song URL:** {{ song_link }}\n**Album Name:** {{ album }}\n**Album Release Date:** {{ album_release }}\n**Album URL:** {{ album_link }}\n**Cover:** {{ album_cover_medium }}\n**Cover URL:** {{ album_cover_link_medium }}\n**Artists:** {{ artists }}\n**Added at:** *{{ timestamp }}*", | ||
"**Episode Name:** {{ episode_name }}\n**Description:** {{ description }}\n**Added at:** *{{ timestamp }}*", | ||
], | ||
}; |
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,88 @@ | ||
import { CurrentlyPlayingTrack, Episode } from "./types"; | ||
import { millisToMinutesAndSeconds } from "./utils"; | ||
|
||
export function isEpisode(data: CurrentlyPlayingTrack) { | ||
return data.item.type === "episode"; | ||
} | ||
|
||
export function getEpisodeMessageTimestamp(data: CurrentlyPlayingTrack) { | ||
if (!isEpisode(data)) throw new Error("Not an episode."); | ||
const episode = data.item as Episode; | ||
|
||
const episode_name = episode?.name; | ||
const description = episode?.description; | ||
const release_date = episode?.release_date; | ||
const progress = data.progress_ms; | ||
const duration = episode.duration_ms; | ||
const url = episode.external_urls.spotify; | ||
return `['**${episode_name}**': ***${description}***, released ${release_date} | **${millisToMinutesAndSeconds( | ||
progress | ||
)}** (${((progress / duration) * 100).toFixed(0)}%)](${url})`; | ||
} | ||
|
||
export function getEpisodeMessage( | ||
data: CurrentlyPlayingTrack, | ||
template: string | ||
) { | ||
if (!isEpisode(data)) throw new Error("Not an episode."); | ||
const episode = data.item as Episode; | ||
|
||
return template | ||
.replace(/{{ episode_name }}|{{episode_name}}/g, episode.name) | ||
.replace( | ||
/{{ episode_link }}|{{episode_link}}/g, | ||
episode.external_urls.spotify | ||
) | ||
.replace(/{{ description }}|{{description}}/g, episode.description) | ||
.replace( | ||
/{{ duration_ms }}|{{duration_ms}}/g, | ||
episode.duration_ms.toString() | ||
) | ||
.replace( | ||
/{{ audio_preview_url }}|{{audio_preview_url}}/g, | ||
`![Audio preview url](${episode.audio_preview_url})` | ||
) | ||
.replace( | ||
/{{ episode_cover_large }}|{{episode_cover_large}}/g, | ||
`![${episode.name}](${episode.images[0]?.url})` | ||
) | ||
.replace( | ||
/{{ episode_cover_medium }}|{{episode_cover_medium}}/g, | ||
`![${episode.name}](${episode.images[1]?.url})` | ||
) | ||
.replace( | ||
/{{ episode_cover_small }}|{{episode_cover_small}}/g, | ||
`![${episode.name}](${episode.images[2]?.url})` | ||
) | ||
.replace( | ||
/{{ episode_cover_link_large }}|{{episode_cover_link_large}}/g, | ||
episode.images[0].url | ||
) | ||
.replace( | ||
/{{ episode_cover_link_medium }}|{{episode_cover_link_medium}}/g, | ||
episode.images[1]?.url | ||
) | ||
.replace( | ||
/{{ episode_cover_link_small }}|{{episode_cover_link_small}}/g, | ||
episode.images[2]?.url | ||
) | ||
.replace(/{{ release_date }}|{{release_date}}/g, episode.release_date) | ||
.replace(/{{ show_name }}|{{show_name}}/g, episode.show.name) | ||
.replace(/{{ publisher }}|{{publisher}}/g, episode.show.publisher) | ||
.replace( | ||
/{{ show_description }}|{{show_description}}/g, | ||
episode.show.description | ||
) | ||
.replace( | ||
/{{ show_link }}|{{show_link}}/g, | ||
episode.show.external_urls.spotify | ||
) | ||
.replace( | ||
/{{ total_episodes }}|{{total_episodes}}/g, | ||
episode.show.total_episodes.toString() | ||
) | ||
.replace( | ||
/{{ timestamp }}|{{timestamp}}/g, | ||
`${new Date().toDateString()} - ${new Date().toLocaleTimeString()}` | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,44 @@ | ||
import { getEpisodeMessage, getEpisodeMessageTimestamp } from "./episode"; | ||
import { | ||
getTrackMessage, | ||
getTrackMessageTimestamp, | ||
getTrackType, | ||
} from "./track"; | ||
import { CurrentlyPlayingTrack } from "./types"; | ||
import { millisToMinutesAndSeconds } from "./utils"; | ||
|
||
export function processCurrentlyPlayingTrackInput( | ||
data: CurrentlyPlayingTrack | ||
): string { | ||
let message = ""; | ||
if (data && data.is_playing) { | ||
message = `['**${data.item.name}**' by ***${data.item.artists | ||
.map((a) => a.name) | ||
.join(", ")}*** **${millisToMinutesAndSeconds( | ||
data.progress_ms | ||
)}** (${( | ||
(data.progress_ms / parseInt(data.item.duration_ms)) * | ||
100 | ||
).toFixed(0)}%)](${data.item.external_urls.spotify})`; | ||
} else { | ||
message = "No song is playing."; | ||
if (getTrackType(data) === "track") { | ||
return getTrackMessageTimestamp(data); | ||
} | ||
if (getTrackType(data) === "episode") { | ||
return getEpisodeMessageTimestamp(data); | ||
} | ||
|
||
throw new Error( | ||
"The data received is not handle. You can request it by opening a GitHub issue and providing the track URL so that I can adjust the tool accordingly." | ||
); | ||
} | ||
return message; | ||
return "No song is playing."; | ||
} | ||
|
||
export function processCurrentlyPlayingTrack( | ||
data: CurrentlyPlayingTrack, | ||
template = `'{{ song_name }}' by {{ artists }} from {{ album }} released in {{ album_release }}\n{{ timestamp }}` | ||
): string { | ||
let message = ""; | ||
if (data && data.is_playing) { | ||
message = template | ||
.replace(/{{ song_name }}|{{song_name}}/g, data.item.name) | ||
.replace( | ||
/{{ song_link }}|{{song_link}}/g, | ||
data.item.external_urls.spotify | ||
) | ||
.replace( | ||
/{{ artists }}|{{artist}}/g, | ||
data.item.artists.map((a) => a.name).join(", ") | ||
) | ||
.replace( | ||
/{{ album_release }}|{{album_release}}/g, | ||
data.item.album.release_date | ||
) | ||
.replace( | ||
/{{ album_cover_large }}|{{album_cover_large}}/g, | ||
`![${data.item.album.name}](${data.item.album.images[0].url})` | ||
) | ||
.replace( | ||
/{{ album_cover_medium }}|{{album_cover_medium}}/g, | ||
`![${data.item.album.name}](${data.item.album.images[1]?.url})` | ||
) | ||
.replace( | ||
/{{ album_cover_small }}|{{album_cover_small}}/g, | ||
`![${data.item.album.name}](${data.item.album.images[2]?.url})` | ||
) | ||
.replace( | ||
/{{ album_cover_link_large }}|{{album_cover_link_large}}/g, | ||
data.item.album.images[0].url | ||
) | ||
.replace( | ||
/{{ album_cover_link_medium }}|{{album_cover_link_medium}}/g, | ||
data.item.album.images[1]?.url | ||
) | ||
.replace( | ||
/{{ album_cover_link_small }}|{{album_cover_link_small}}/g, | ||
data.item.album.images[2]?.url | ||
) | ||
.replace( | ||
/{{ album_link }}|{{album_link}}/g, | ||
data.item.album.external_urls.spotify | ||
) | ||
.replace(/{{ album }}|{{album}}/g, data.item.album.name) | ||
.replace( | ||
/{{ timestamp }}|{{timestamp}}/g, | ||
`${new Date().toDateString()} - ${new Date().toLocaleTimeString()}` | ||
); | ||
} else { | ||
message = "No song is playing."; | ||
} | ||
if (getTrackType(data) === "track") { | ||
return getTrackMessage(data, template); | ||
} | ||
if (getTrackType(data) === "episode") { | ||
return getEpisodeMessage(data, template); | ||
} | ||
|
||
return message; | ||
throw new Error( | ||
"The data received is not handle. You can request it by opening a GitHub issue and providing the track URL so that I can adjust the tool accordingly." | ||
); | ||
} | ||
return "No song is playing."; | ||
} |
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
Oops, something went wrong.