fix: better converter

This commit is contained in:
Mateusz Pstrucha committed 2022-04-19 20:25:30 +02:00
1 parent 79140b5a51
commit d3d043c6fd
2 files changed
+27 -2

No files matched your search

+2 -2
View File
@@ -1,6 +1,6 @@
class Track {
constructor(tid, { url, info: [{ id, title, duration }] }) {
this.url = url
constructor(tid, [{ id, title, duration }]) {
this.url = `https://www.youtube.com/watch?v=${id}`
this.tid = tid
this.ytid = id
this.title = title
+25
View File
@@ -1,4 +1,5 @@
import spotifyToYt from 'spotify-to-yt'
import { YouTube } from 'youtube-sr'
import {
createWriteStream,
existsSync,
@@ -11,12 +12,36 @@ import ytdl from 'ytdl-core'
import { logAction, logError } from './log.js'
import Track from '../classes/Track.js'
import { getTrack } from './calls.js'
const cache = './cache'
function convert(tids) {
logAction(`Running converter`, tids.length)
return Promise.all(
tids.map(async (tid) => {
let { title, artists } = await getTrack(tid)
return await YouTube.search(
`${title} ${artists.join(' ')} official audio`,
{ limit: 10, type: 'video' }
).then((videos) => {
if (videos.length < 1) {
logError('No videos found, retrying', tid)
return YouTube.search(`${title} ${artists.join(' ')}`, {
limit: 10,
type: 'video',
}).then((videos) => {
if (videos.length < 1) {
logError(`Conversion failed twice, I give up`, tid)
} else return videos
})
} else return videos
})
})
).then((ytd) => tids.map((tid, i) => new Track(tid, ytd[i])))
return Promise.all(
tids.map((tid) =>
spotifyToYt.trackGet(`spotify:track:${tid}`).catch((err) => {