-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
86 lines (69 loc) · 2.61 KB
/
main.ts
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import "jsr:@std/dotenv/load";
import scrapeAlbums from "./scrapeAlbums.ts";
import getClientAuth from "./clientAuth.ts";
import getUserAuth from "./userAuth.ts";
import getAlbumIds from "./getAlbumIds.ts";
import getAllTracks from "./getAlbumTracks.ts";
import clearPlaylist from "./clearPlaylist.ts";
import addTracks from "./addTracks.ts";
import updateDetails from "./updateDetails.ts";
import updateCover from "./updateCover.ts";
import chalk from "npm:chalk";
const doomChartsUrl: string = Deno.args[0];
const playlistId = "6zVF0xeVebktWpbW0wI6JL";
const SPOTIFY_ID = Deno.env.get("SPOTIFY_ID");
const SPOTIFY_SECRET = Deno.env.get("SPOTIFY_SECRET");
let descriptionString = "";
function exit(message: string): never {
console.error(message);
Deno.exit(1);
}
if (!SPOTIFY_ID || !SPOTIFY_SECRET)
exit("Spotify client ID or secret is missing.");
// Get names
const albumTitles = await scrapeAlbums(doomChartsUrl);
if (!albumTitles) exit("Failed to get the album names.");
await confirm(chalk.magenta("Proceed with these albums?"));
// Auth
const clientToken = await getClientAuth(SPOTIFY_ID, SPOTIFY_SECRET);
if (!clientToken) exit("Failed to get a client token.");
// Get Ids
let albumResults = await getAlbumIds(albumTitles, clientToken);
if (!albumResults) exit("Couldn't retrieve album ids");
// Filter bad matches
const excludePrompt = await prompt(
chalk.magenta("Choose matches to exclude: (comma separated #)")
);
if (excludePrompt) {
// Parse input positions
const toExclude: number[] = excludePrompt.split(",").map(Number);
// Format description to "Missing: #1 ... | #2..."
descriptionString = `Missing: ${toExclude
.map((pos, index) => {
const title = albumTitles.filter((album) => album.pos === pos)[0].title;
return `#${pos} ${title} ${index === toExclude.length - 1 ? "" : "| "}`;
})
.join("")}`;
// Filter albums
albumResults = albumResults.filter((a) => !toExclude.includes(a.pos));
}
// Get all tracks
const trackUris = await getAllTracks(
albumResults.map((a) => a.id),
clientToken
);
if (!trackUris) exit("Couldn't get the album tracks");
// Login
const userTokens = await getUserAuth(SPOTIFY_ID, SPOTIFY_SECRET);
if (!userTokens) exit("Failed to get user tokens.");
const { accessToken } = userTokens;
await confirm(chalk.red(`Proceed with nuking playlist ${playlistId} ?`));
await clearPlaylist(playlistId, accessToken);
await addTracks(playlistId, accessToken, trackUris);
await updateDetails(playlistId, accessToken, descriptionString);
await updateCover(
playlistId,
accessToken,
albumResults[albumResults.length - 1].id as string
);
console.log(chalk.green("All done 🤘"));