-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
222 lines (186 loc) · 4.6 KB
/
content.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
var currentPlayingSong = "";
var lastPlayingSong = "";
var justSkippedSong = ".";
var songPlayTime = 0.0;
var token;
var refreshToken;
var clientId = "6b71231841e2457480410c0c5a90d3b6";
var isSkippingSongs = false;
//localStorage.setItem("token", ""your token)
//localStorage.setItem("refreshToken", "Your refresh token")
refreshToken = localStorage.getItem("refreshToken");
token = localStorage.getItem("token");
var weights = localStorage.getItem("weights");
//weights = {};
if (weights == null) {
weights = {};
localStorage.setItem("weights", JSON.stringify(weights));
}
setInterval(async () => {
songPlayTime += 0.2;
if (isSkippingSongs) {
return;
}
const anchorElement = document.querySelector(
'a[data-testid="context-item-link"]'
);
if (anchorElement) {
const songTitle = anchorElement.textContent.trim();
await updateCurrentSong(songTitle);
} else {
}
}, 200);
async function updateCurrentSong(songTitle) {
if (songTitle == currentPlayingSong) {
return;
}
if (isSkippingSongs) {
return;
}
if (currentPlayingSong == justSkippedSong) {
currentPlayingSong = songTitle;
return;
}
currentPlayingSong = songTitle;
await listeningToNewSong();
}
async function listeningToNewSong() {
if (currentPlayingSong == lastPlayingSong) {
songPlayTime = 0.0;
return;
}
console.log(lastPlayingSong)
calcNewWieght(lastPlayingSong);
lastPlayingSong = currentPlayingSong;
songPlayTime = 0.0;
var result = calcSongWeight(currentPlayingSong);
if (result) {
return;
}
lastPlayingSong = currentPlayingSong;
var queueTracks = await getUseQue();
await getNextSong(queueTracks);
}
async function getNextSong(queue) {
var songToPlay;
var songPicked = false;
var songIndex = 0;
queue.queue.every((element, v) => {
if (element == false || element == true) {
return true;
}
var isSongPicked = calcSongWeight(element);
if (!isSongPicked) {
return true;
}
songToPlay = element.name;
songPicked = true;
songIndex = v + 1;
return false;
});
if (!songPicked) {
noSongPicked();
}
justSkippedSong = currentPlayingSong;
currentPlayingSong = songToPlay;
makeSkips(songIndex).then(() => {
isSkippingSongs = false;
});
}
async function makeSkips(numberOfSkips) {
isSkippingSongs = true;
for (let i = 0; i < numberOfSkips; i++) {
await makeSkipCalls();
await delay(5);
}
}
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function makeSkipCalls() {
await fetch("https://api.spotify.com/v1/me/player/next", {
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
},
});
}
function noSongPicked() {
return;
//cry
}
function calcSongWeight(songName) {
if (!weights.hasOwnProperty(songName)) {
weights[songName] = 8.0;
}
const songWeight = weights[songName];
var randomNumber = Math.floor(Math.random() * 10) + 1;
if (randomNumber >= songWeight) {
return false;
}
return true;
}
async function getUseQue() {
var queue;
try {
var response = await fetch("https://api.spotify.com/v1/me/player/queue", {
headers: { Authorization: `Bearer ${token}` },
});
if (response.status == "401") {
console.log("throwing new error");
throw new Error(response.status);
}
queue = await response.json();
} catch (Error) {
if (Error.message == 401) {
await refreshAccessToken();
return await getUseQue();
}
}
return queue;
}
async function refreshAccessToken() {
var response = await fetch("http://localhost:5107/api/refreshtoken", {
method: "POST",
body: JSON.stringify({ refreshToken: refreshToken }),
});
var result = await response.json();
localStorage.setItem("token", result["access_token"]);
token = result["access_token"];
return;
}
function calcNewWieght(songName) {
if (isSkippingSongs || songName == "") {
return;
}
if (!weights.hasOwnProperty(songName)) {
weights[songName] = 8;
localStorage.setItem("weights", weights);
return;
}
if (songPlayTime > 30.0) {
weights[songName] = weights[songName] + 2;
checkIf0or10(songName);
return;
}
if (songPlayTime < 5.0) {
weights[songName] = weights[songName] - 2;
checkIf0or10(songName);
return;
}
if (songPlayTime < 30.0) {
weights[songName] = weights[songName] - 1;
checkIf0or10(songName);
return;
}
}
function checkIf0or10(songName) {
if (weights[songName] < 0) {
weights[songName] = 0;
}
if (weights[songName] > 10) {
weights[songName] = 10;
}
localStorage.setItem("weights", JSON.stringify(weights));
return;
}