-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
133 lines (112 loc) · 3.4 KB
/
index.js
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const puppeteer = require("puppeteer");
const axios = require("axios");
const SpotifyWebApi = require("spotify-web-api-node");
const dotenv = require("dotenv");
dotenv.config();
async function getAccessToken() {
try {
const response = await axios({
method: "post",
url: "https://accounts.spotify.com/api/token",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
params: {
grant_type: "client_credentials",
},
auth: {
username: process.env.SPOTIFY_CLIENT_ID,
password: process.env.SPOTIFY_CLIENT_SECRET,
},
});
return response.data.access_token;
} catch (error) {
console.error("Error getting access token:", error);
throw error;
}
}
async function countGenres(playlistId) {
try {
const accessToken = await getAccessToken();
const spotifyApi = new SpotifyWebApi({
clientId: process.env.SPOTIFY_CLIENT_ID,
clientSecret: process.env.SPOTIFY_CLIENT_SECRET,
redirectUri: process.env.SPOTIFY_REDIRECT_URI,
accessToken,
});
const getGenres = async (browser, artist) => {
const page = await browser.newPage();
const url = `https://everynoise.com/lookup.cgi?who=${encodeURIComponent(
artist
)}`;
await page.goto(url);
const genres = await page.evaluate(() => {
const anchorTags = document.querySelectorAll("div>a:not([title])");
return Array.from(anchorTags).map((a) => a.textContent);
});
await page.close();
return genres;
};
const getPlaylistArtists = async (playlistId) => {
const { body } = await spotifyApi.getPlaylist(playlistId);
return [
...new Set(
body.tracks.items.map((track) => track.track.artists[0].name)
),
];
};
const artists = await getPlaylistArtists(playlistId);
if (!artists || artists.length === 0) {
console.log("No artists found in the playlist.");
return;
}
const browser = await puppeteer.launch();
let genresCount = {};
for (const artist of artists) {
const genres = await getGenres(browser, artist);
console.log(`Genres for ${artist}: ${genres}`);
for (const genre of genres) {
genresCount[genre] = (genresCount[genre] || 0) + 1;
}
}
await browser.close();
const sortedGenresCount = Object.entries(genresCount).sort(
(a, b) => b[1] - a[1]
);
console.log(sortedGenresCount);
return sortedGenresCount;
} catch (error) {
console.error("Error counting genres:", error);
throw error;
}
}
function extractPlaylistId(input) {
const spotifyUrlRegex =
/https:\/\/open\.spotify\.com\/playlist\/([\w\d]+)(\?si=.+)?/;
const matches = input.match(spotifyUrlRegex);
return matches ? matches[1] : null;
}
function getPlaylistIdFromArgs(args) {
if (args.length === 0) {
return null;
}
const input = args[0];
if (input.startsWith("https://")) {
return extractPlaylistId(input);
}
return input;
}
async function main() {
const args = process.argv.slice(2);
const playlistId =
getPlaylistIdFromArgs(args) || process.env.SPOTIFY_PLAYLIST_ID;
if (!playlistId) {
console.error(
"Please provide the playlist ID or URL as a command line argument or set SPOTIFY_PLAYLIST_ID environment variable."
);
process.exit(1);
}
const result = await countGenres(playlistId);
console.log(result);
}
main();