import {URL} from 'url'; import {inject, injectable} from 'inversify'; import {toSeconds, parse} from 'iso8601-duration'; import got from 'got'; import ytsr, {Video} from 'ytsr'; import spotifyURI from 'spotify-uri'; import Spotify from 'spotify-web-api-node'; import YouTube, {YoutubePlaylistItem} from 'youtube.ts'; import PQueue from 'p-queue'; import shuffle from 'array-shuffle'; import {Except} from 'type-fest'; import {QueuedSong, QueuedPlaylist} from '../services/player.js'; import {TYPES} from '../types.js'; import {cleanUrl} from '../utils/url.js'; import ThirdParty from './third-party.js'; import Config from './config.js'; import KeyValueCacheProvider from './key-value-cache.js'; import {ONE_HOUR_IN_SECONDS, ONE_MINUTE_IN_SECONDS} from '../utils/constants.js'; type SongMetadata = Except; @injectable() export default class { private readonly youtube: YouTube; private readonly youtubeKey: string; private readonly spotify: Spotify; private readonly cache: KeyValueCacheProvider; private readonly ytsrQueue: PQueue; constructor( @inject(TYPES.ThirdParty) thirdParty: ThirdParty, @inject(TYPES.Config) config: Config, @inject(TYPES.KeyValueCache) cache: KeyValueCacheProvider) { this.youtube = thirdParty.youtube; this.youtubeKey = config.YOUTUBE_API_KEY; this.spotify = thirdParty.spotify; this.cache = cache; this.ytsrQueue = new PQueue({concurrency: 4}); } async youtubeVideoSearch(query: string): Promise { const {items} = await this.ytsrQueue.add(async () => this.cache.wrap( ytsr, query, { limit: 10, }, { expiresIn: ONE_HOUR_IN_SECONDS, }, )); let firstVideo: Video | undefined; for (const item of items) { if (item.type === 'video') { firstVideo = item; break; } } if (!firstVideo) { throw new Error('No video found.'); } return this.youtubeVideo(firstVideo.id); } async youtubeVideo(url: string): Promise { const videoDetails = await this.cache.wrap( this.youtube.videos.get, cleanUrl(url), { expiresIn: ONE_HOUR_IN_SECONDS, }, ); return { title: videoDetails.snippet.title, artist: videoDetails.snippet.channelTitle, length: toSeconds(parse(videoDetails.contentDetails.duration)), url: videoDetails.id, playlist: null, isLive: videoDetails.snippet.liveBroadcastContent === 'live', thumbnailUrl: videoDetails.snippet.thumbnails.medium.url, }; } async youtubePlaylist(listId: string): Promise { // YouTube playlist const playlist = await this.cache.wrap( this.youtube.playlists.get, listId, { expiresIn: ONE_MINUTE_IN_SECONDS, }, ); interface VideoDetailsResponse { id: string; contentDetails: { videoId: string; duration: string; }; } const playlistVideos: YoutubePlaylistItem[] = []; const videoDetailsPromises: Array> = []; const videoDetails: VideoDetailsResponse[] = []; let nextToken: string | undefined; while (playlistVideos.length !== playlist.contentDetails.itemCount) { // eslint-disable-next-line no-await-in-loop const {items, nextPageToken} = await this.cache.wrap( this.youtube.playlists.items, listId, {maxResults: '50', pageToken: nextToken}, { expiresIn: ONE_MINUTE_IN_SECONDS, }, ); nextToken = nextPageToken; playlistVideos.push(...items); // Start fetching extra details about videos videoDetailsPromises.push((async () => { // Unfortunately, package doesn't provide a method for this const p = { searchParams: { part: 'contentDetails', id: items.map(item => item.contentDetails.videoId).join(','), key: this.youtubeKey, responseType: 'json', }, }; const {items: videoDetailItems} = await this.cache.wrap( async () => got( 'https://www.googleapis.com/youtube/v3/videos', p, ).json() as Promise<{items: VideoDetailsResponse[]}>, p, { expiresIn: ONE_MINUTE_IN_SECONDS, }, ); videoDetails.push(...videoDetailItems); })()); } await Promise.all(videoDetailsPromises); const queuedPlaylist = {title: playlist.snippet.title, source: playlist.id}; const songsToReturn: SongMetadata[] = []; for (const video of playlistVideos) { try { const length = toSeconds(parse(videoDetails.find((i: {id: string}) => i.id === video.contentDetails.videoId)!.contentDetails.duration)); songsToReturn.push({ title: video.snippet.title, artist: video.snippet.channelTitle, length, url: video.contentDetails.videoId, playlist: queuedPlaylist, isLive: false, thumbnailUrl: video.snippet.thumbnails.medium.url, }); } catch (_: unknown) { // Private and deleted videos are sometimes in playlists, duration of these is not returned and they should not be added to the queue. } } return songsToReturn; } async spotifySource(url: string, playlistLimit: number): Promise<[SongMetadata[], number, number]> { const parsed = spotifyURI.parse(url); let tracks: SpotifyApi.TrackObjectSimplified[] = []; let playlist: QueuedPlaylist | null = null; switch (parsed.type) { case 'album': { const uri = parsed as spotifyURI.Album; const [{body: album}, {body: {items}}] = await Promise.all([this.spotify.getAlbum(uri.id), this.spotify.getAlbumTracks(uri.id, {limit: 50})]); tracks.push(...items); playlist = {title: album.name, source: album.href}; break; } case 'playlist': { const uri = parsed as spotifyURI.Playlist; let [{body: playlistResponse}, {body: tracksResponse}] = await Promise.all([this.spotify.getPlaylist(uri.id), this.spotify.getPlaylistTracks(uri.id, {limit: 50})]); playlist = {title: playlistResponse.name, source: playlistResponse.href}; tracks.push(...tracksResponse.items.map(playlistItem => playlistItem.track)); while (tracksResponse.next) { // eslint-disable-next-line no-await-in-loop ({body: tracksResponse} = await this.spotify.getPlaylistTracks(uri.id, { limit: parseInt(new URL(tracksResponse.next).searchParams.get('limit') ?? '50', 10), offset: parseInt(new URL(tracksResponse.next).searchParams.get('offset') ?? '0', 10), })); tracks.push(...tracksResponse.items.map(playlistItem => playlistItem.track)); } break; } case 'track': { const uri = parsed as spotifyURI.Track; const {body} = await this.spotify.getTrack(uri.id); tracks.push(body); break; } case 'artist': { const uri = parsed as spotifyURI.Artist; const {body} = await this.spotify.getArtistTopTracks(uri.id, 'US'); tracks.push(...body.tracks); break; } default: { return [[], 0, 0]; } } // Get random songs if the playlist is larger than limit const originalNSongs = tracks.length; if (tracks.length > playlistLimit) { const shuffled = shuffle(tracks); tracks = shuffled.slice(0, playlistLimit); } const searchResults = await Promise.allSettled(tracks.map(async track => this.spotifyToYouTube(track))); let nSongsNotFound = 0; // Count songs that couldn't be found const songs: SongMetadata[] = searchResults.reduce((accum: SongMetadata[], result) => { if (result.status === 'fulfilled') { accum.push({ ...result.value, ...(playlist ? {playlist} : {}), }); } else { nSongsNotFound++; } return accum; }, []); return [songs, nSongsNotFound, originalNSongs]; } private async spotifyToYouTube(track: SpotifyApi.TrackObjectSimplified): Promise { return this.youtubeVideoSearch(`"${track.name}" "${track.artists[0].name}"`); } }