From e103282cd7c283ba1c7fe9c92b2bd8feaed5b120 Mon Sep 17 00:00:00 2001 From: Max Isom Date: Sat, 24 Oct 2020 13:11:29 -0400 Subject: [PATCH] Fix auto translate search function --- src/services/get-songs.ts | 19 +++++-------------- src/services/player.ts | 2 +- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/src/services/get-songs.ts b/src/services/get-songs.ts index 48a1dc7..8041d0b 100644 --- a/src/services/get-songs.ts +++ b/src/services/get-songs.ts @@ -4,13 +4,11 @@ import {toSeconds, parse} from 'iso8601-duration'; import got from 'got'; import spotifyURI from 'spotify-uri'; import Spotify from 'spotify-web-api-node'; -import ytsr from 'ytsr'; import YouTube from 'youtube.ts'; import pLimit from 'p-limit'; import uniqueRandomArray from 'unique-random-array'; import {QueuedSong, QueuedPlaylist} from '../services/player'; import {TYPES} from '../types'; -import {parseTime} from '../utils/time'; @injectable() export default class { @@ -183,23 +181,16 @@ export default class { return [songs as QueuedSong[], nSongsNotFound, originalNSongs]; } - private async spotifyToYouTube(track: SpotifyApi.TrackObjectSimplified, playlist: QueuedPlaylist | null): Promise { + private async spotifyToYouTube(track: SpotifyApi.TrackObjectSimplified, _: QueuedPlaylist | null): Promise { try { - const {items} = await ytsr(`"${track.name}" "${track.artists[0].name}" offical`, {limit: 5}); - const video = items.find(item => item.type === 'video'); + const {items} = await this.youtube.videos.search({q: `"${track.name}" "${track.artists[0].name}"`, maxResults: 10}); + const videoResult = items[0]; // Items.find(item => item.type === 'video'); - if (!video || video.type !== 'video') { + if (!videoResult) { throw new Error('No video found for query.'); } - return { - title: video.title, - artist: track.artists[0].name, - length: video.duration ? parseTime(video.duration) : 0, - url: video.link, - playlist, - isLive: video.live - }; + return await this.youtubeVideo(videoResult.id.videoId); } catch (_: unknown) { return null; } diff --git a/src/services/player.ts b/src/services/player.ts index 76ff73f..d149826 100644 --- a/src/services/player.ts +++ b/src/services/player.ts @@ -83,7 +83,7 @@ export default class { } const stream = await this.getStream(currentSong.url, {seek: positionSeconds}); - this.dispatcher = this.voiceConnection.play(stream, {type: 'webm/opus'}); + this.dispatcher = this.voiceConnection.play(stream, {type: 'webm/opus', bitrate: 'auto'}); this.attachListeners(); this.startTrackingPosition(positionSeconds);