Newer
Older
import {
type AlbumResponse,
type AlbumTracksResponse,
type ArtistDetails,
type ArtistMetadataResponse,
type ContentType,
type EpisodeMetadataResponse,
type FileManifestResponse,
type GetLicenseResponse,
type LyricsResponse,
type PlaylistContent,
type PlaylistContentResponse,
type PlaylistResponse,
type PlaylistType,
type ShowMetadataResponse,
type Settings,
type SongMetadataResponse,
type State,
type TrackMetadataResponse,
type Tracks,
type ProfileAttributesResponse,
type ChannelType,
type BrowsePageResponse,
type GenrePlaylistSection,
type ChannelTypeCapabilities,
type SectionItemAlbum,
type SectionItemPlaylist,
type BrowseSectionResponse,
type Section,
type BookChaptersResponse,
type PodcastEpisodesResponse,
type UserPlaylistsResponse,
type DiscographyResponse,
type HomeResponse,
type HomePlaylistSection,
type SectionItemEpisode,
type WhatsNewResponse,
type WhatsNewSection,
type SearchResponse,
type SearchTypes,
type CollectionType,
type LikedEpisodesResponse,
type LikedTracksResponse,
type LibraryResponse,
type FollowingResponse,
type UriType,
type SpotifySource,
type RecentlyPlayedUris,
type RecentlyPlayedDetails,
type RecentlyPlayedSection,
type SectionItemPseudoPlaylist,
} from "./types.js"
const CONTENT_REGEX = /^https:\/\/open\.spotify\.com\/(track|episode)\/([a-zA-Z0-9]*)($|\/)/
const PLAYLIST_REGEX = /^https:\/\/open\.spotify\.com\/(album|playlist|collection)\/([a-zA-Z0-9]*|your-episodes|tracks)($|\/)/
const CHANNEL_REGEX = /^https:\/\/open\.spotify\.com\/(show|artist|user|genre|section|content-feed)\/(section|)([a-zA-Z0-9]*|recently-played)($|\/)/
const SONG_URL_PREFIX = "https://open.spotify.com/track/" as const
const EPISODE_URL_PREFIX = "https://open.spotify.com/episode/" as const
const SHOW_URL_PREFIX = "https://open.spotify.com/show/" as const
const ARTIST_URL_PREFIX = "https://open.spotify.com/artist/" as const
const USER_URL_PREFIX = "https://open.spotify.com/user/" as const
const ALBUM_URL_PREFIX = "https://open.spotify.com/album/" as const
const PAGE_URL_PREFIX = "https://open.spotify.com/genre/" as const
const SECTION_URL_PREFIX = "https://open.spotify.com/section/" as const
const PLAYLIST_URL_PREFIX = "https://open.spotify.com/playlist/" as const
const COLLECTION_UR_PREFIX = "https://open.spotify.com/collection/" as const
const QUERY_URL = "https://api-partner.spotify.com/pathfinder/v1/query" as const
const IMAGE_URL_PREFIX = "https://i.scdn.co/image/" as const
const PLATFORM = "Spotify" as const
// const USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0" as const
const PLATFORM_IDENTIFIER = "web_player linux undefined;firefox 126.0;desktop" as const
const SPOTIFY_CONNECT_NAME = "Web Player (Grayjay)" as const
const CLIENT_VERSION = "harmony:4.42.0-2780565f" as const
const HARDCODED_ZERO = 0 as const
const HARDCODED_EMPTY_STRING = "" as const
const EMPTY_AUTHOR = new PlatformAuthorLink(new PlatformID(PLATFORM, "", plugin.config.id), "", "")
const local_http = http
// const local_utility = utility
// set missing constants
Type.Order.Chronological = "Latest releases"
Type.Order.Views = "Most played"
Type.Order.Favorites = "Most favorited"
Type.Feed.Playlists = "PLAYLISTS"
Type.Feed.Albums = "ALBUMS"
/** State */
let local_state: State
//#region source methods
source.enable = enable
source.disable = disable
source.saveState = saveState
source.getHome = getHome
source.getSearchCapabilities = getSearchCapabilities
source.search = search
source.isChannelUrl = isChannelUrl
source.getChannel = getChannel
source.getChannelCapabilities = getChannelCapabilities
source.getChannelContents = getChannelContents
Kai DeLorenzo
committed
source.getChannelPlaylists = getChannelPlaylists
source.isContentDetailsUrl = isContentDetailsUrl
source.getContentDetails = getContentDetails
source.getUserSubscriptions = getUserSubscriptions
source.getUserPlaylists = getUserPlaylists
source.getPlaybackTracker = getPlaybackTracker
if (IS_TESTING) {
const assert_source: SpotifySource = {
enable,
disable,
saveState,
getHome,
search,
getSearchCapabilities,
isContentDetailsUrl,
getContentDetails,
isChannelUrl,
getChannel,
getChannelContents,
Kai DeLorenzo
committed
getChannelPlaylists,
getChannelCapabilities,
searchChannels,
isPlaylistUrl,
getPlaylist,
searchPlaylists,
getUserPlaylists,
getUserSubscriptions,
getPlaybackTracker
}
if (source.enable === undefined) { assert_never(source.enable) }
if (source.disable === undefined) { assert_never(source.disable) }
if (source.saveState === undefined) { assert_never(source.saveState) }
if (source.getHome === undefined) { assert_never(source.getHome) }
if (source.search === undefined) { assert_never(source.search) }
if (source.getSearchCapabilities === undefined) { assert_never(source.getSearchCapabilities) }
if (source.isContentDetailsUrl === undefined) { assert_never(source.isContentDetailsUrl) }
if (source.getContentDetails === undefined) { assert_never(source.getContentDetails) }
if (source.isChannelUrl === undefined) { assert_never(source.isChannelUrl) }
if (source.getChannel === undefined) { assert_never(source.getChannel) }
if (source.getChannelContents === undefined) { assert_never(source.getChannelContents) }
Kai DeLorenzo
committed
if (source.getChannelPlaylists === undefined) { assert_never(source.getChannelPlaylists) }
if (source.getChannelCapabilities === undefined) { assert_never(source.getChannelCapabilities) }
if (source.searchChannels === undefined) { assert_never(source.searchChannels) }
if (source.isPlaylistUrl === undefined) { assert_never(source.isPlaylistUrl) }
if (source.getPlaylist === undefined) { assert_never(source.getPlaylist) }
if (source.searchPlaylists === undefined) { assert_never(source.searchPlaylists) }
if (source.getUserPlaylists === undefined) { assert_never(source.getUserPlaylists) }
if (source.getUserSubscriptions === undefined) { assert_never(source.getUserSubscriptions) }
if (source.getPlaybackTracker === undefined) { assert_never(source.getPlaybackTracker) }
if (IS_TESTING) {
log(assert_source)
}
}
function enable(conf: SourceConfig, settings: Settings, savedState: string | null) {
if (IS_TESTING) {
log("IS_TESTING true")
log("logging configuration")
log(conf)
log("logging settings")
log(settings)
log("logging savedState")
log(savedState)
}
if (savedState !== null) {
const state: State = JSON.parse(savedState)
local_state = state
// the token stored in state might be old
check_and_update_token()
} else {
const home_page = "https://open.spotify.com"
const token_regex = /<script id="config" data-testid="config" type="application\/json">({.*?})<\/script><script id="session" data-testid="session" type="application\/json">({.*?})<\/script>/
const web_player_js_regex = /https:\/\/open\.spotifycdn\.com\/cdn\/build\/web-player\/web-player\..{8}\.js/
// use the authenticated client to get a logged in bearer token
const html = throw_if_not_200(local_http.GET(home_page, {}, true)).body
const web_player_js_match_result = html.match(web_player_js_regex)
Loading
Loading full blame...