-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
60 lines (49 loc) · 1.86 KB
/
Copy pathscript.js
File metadata and controls
60 lines (49 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import axios from "axios";
import dotenv from 'dotenv'
import { exec } from "node:child_process";
import fs from 'node:fs'
import yts from "yt-search";
import { promisify } from "node:util";
dotenv.config();
const execPromise = promisify(exec);
//Spotify Auth
const authRes = await axios.post(
'https://accounts.spotify.com/api/token',
new URLSearchParams({
'grant_type': 'client_credentials',
'client_id': process.env.CLIENT_ID,
'client_secret': process.env.CLIENT_SECRET
}),
{
headers: {
'Content-Type': "application/x-www-form-urlencoded"
}
},
);
//Spotify Query String
const authToken = authRes.data.access_token;
const playlistId = process.env.PLAYLIST_LINK.split("playlist/")[1].split("?")[0];
const res = await axios.get(`https://api.spotify.com/v1/playlists/${playlistId}`, {
headers: {
Authorization: `Bearer ${authToken}`
}
})
const tracks = res.data.tracks.items;
for (const trackObj of tracks) {
const artistNameArray = trackObj.track.artists.map(artistObj => artistObj.name)
const queryString = `${trackObj.track.name} - ${artistNameArray.join(', ')}`;
// yt-search for link
const result = await yts(queryString)
const ytURL = await result.all[0].url
await console.log(`${result.all[0].title} - ${ytURL}`);
fs.appendFileSync('logs.txt', `${result.all[0].title} - ${ytURL}\n`)
//yt-dlp
try {
const downloadPath = process.env.DOWNLOAD_PATH.replace(/\\/g, "\\\\") + "//"
await execPromise(`yt-dlp -x --audio-format mp3 --embed-thumbnail --add-metadata -o "${downloadPath}%(title)s.%(ext)s" "${ytURL}"`);
console.log(`Downloaded: ${result.all[0].title}\n`);
fs.appendFileSync('logs.txt', `Downloaded: ${result.all[0].title}\n\n`)
} catch (err) {
console.error(`Error downloading ${ytURL}:`, err.message);
}
};