-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
317 lines (282 loc) · 10.8 KB
/
index.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import "chromedriver"
import webdriver, { Browser } from "selenium-webdriver"
import { Options } from "selenium-webdriver/chrome.js";
import { readFileSync, writeFileSync,existsSync } from "fs";
import express from "express"
const options = new Options()
let globalIterator = 0
const globalState: {
[key: string]: SessionState,
} = {
}
export type Song = {
title: string,
artist: string,
coverImageUrl?: string,
}
export type SessionState = {
username: string,
currentSong: Song,
streams: number,
playbackTime: number, // seconds
target: string,
}
options.excludeSwitches("disable-component-update")
options.addArguments(
"--log-level=3",
"--mute-audio",
"--no-sandbox",
"--disable-dev-shm-usage",
"--window-size=1280x1024"
)
if (process.env.NODE_ENV === 'development') {
require('dotenv').config();
}
const args = {
users: [],
skip: 35,
port: process.env.PORT || 8080,
}
if(existsSync("users.json")){
// json is [{username,password}], convert it to [[username,password]]
args.users = JSON.parse(readFileSync("users.json").toString()).map((user: {
username: string,
password: string,
}) => [user.username, user.password])
}else{
if(process.env.USERS) {
args.users = process.env.USERS.split(";").map((user) => user.split(":"))
}
writeFileSync("users.json", JSON.stringify(args.users.map((user)=> ({
username: user[0],
password: user[1],
}))))
}
const waitForPageLoad = async (driver: webdriver.WebDriver) => {
await driver.wait(async () => {
const readyState = await driver.executeScript('return document.readyState');
return readyState === 'complete';
});
}
const start = async (user: string[], target: string, sessionId: number) => {
globalState[sessionId] = {
username: user[0],
currentSong: {
title: "",
artist: "",
},
streams: 0,
playbackTime: 0,
target
}
const driver = new webdriver.Builder().forBrowser(Browser.CHROME).setChromeOptions(options).build();
driver.get("https://google.com/search?q=spotify")
await waitForPageLoad(driver)
driver.get(`https://accounts.spotify.com/en/login?continue=${encodeURIComponent(target)}`)
await waitForPageLoad(driver)
const email = await driver.findElement(webdriver.By.id("login-username"))
const password = await driver.findElement(webdriver.By.id("login-password"))
const submit = await driver.findElement(webdriver.By.id("login-button"))
await email.sendKeys(user[0])
await password.sendKeys(user[1])
await submit.click()
await waitForPageLoad(driver)
// wait for accept cookies button to appear
await driver.wait(webdriver.until.elementLocated(webdriver.By.id("onetrust-accept-btn-handler")))
await driver.sleep(3000)
// accept cookies
await driver.findElement(webdriver.By.id("onetrust-accept-btn-handler")).click()
await driver.sleep(2000)
// wait for playback button to appear
await driver.wait(webdriver.until.elementLocated(webdriver.By.css('button[data-encore-id="buttonPrimary"]')))
while (1) {
// click unchecked shuffle button
await driver.findElement(webdriver.By.css('button[aria-label="Enable shuffle"][aria-checked="false"]')).then((el) => { el.click() }, () => { })
// click unchecked repeat button
await driver.findElement(webdriver.By.css('button[aria-label="Enable repeat"][aria-checked="false"]')).then((el) => { el.click() }, () => { })
// open now playing widget
await driver.findElement(webdriver.By.css('button[data-testid="control-button-npv"][aria-pressed="false"]')).then((el)=>{el.click()}, ()=>{})
// start playback if not playing button data-testid="play-button" aria-label="Play Ja" data-encore-id="buttonPrimary"
await driver.findElement(webdriver.By.xpath('(.//button[@data-encore-id="buttonPrimary"][@data-testid="play-button"])[2]')).then((el) => {
el.getAttribute("aria-label").then((label) => {
if (label.startsWith("Play")) {
el.click()
}
})
}, () => { })
// unmute if muted
await driver.findElement(webdriver.By.css('button[aria-label="Unmute"]')).then((el) => el.click(), () => { })
if (args["skip"] > 0) {
// check playback progress
const progress = await driver.findElement(webdriver.By.className('playback-bar__progress-time-elapsed')).then(async (el) => { return ((await el.getText()).split(":").map((e, i) => i == 0 ? parseInt(e) * 60 : parseInt(e))).reduce((partialSum, a) => partialSum + a, 0) }, () => { return 0 })
if (progress > args["skip"]) {
// skip to next song
if(globalState[sessionId]) {
globalState[sessionId].streams++
}
await driver.findElement(webdriver.By.css('button[aria-label="Next"]')).then((el) => { el.click() }, () => { })
}
}
// update to globalState
const currentSong = await driver.findElement(webdriver.By.css('div[data-testid="now-playing-widget"]')).then(async (el) => {
// [data-testid="now-playing-widget"]'s aria label is "Now playing: {title} by {artist}"
const ariaLabel = await el.getAttribute("aria-label")
const title = ariaLabel.split(" by ")[0].split(": ")[1]
const artist = ariaLabel.split(" by ")[1]
// coverImageUrl: aside[aria-label="Now Playing View"] img[data-testid="cover-art-image"]
const coverImageUrl = await driver.findElement(webdriver.By.css('aside[aria-label="Now Playing view"] img[data-testid="cover-art-image"]')).then((el) => { return el.getAttribute("src") }, () => { return "" })
return {
title,
artist,
coverImageUrl,
}
}, () => { return { title: "", artist: "" } })
// get playback time from progress bar
const playbackTime = await driver.findElement(webdriver.By.className('playback-bar__progress-time-elapsed')).then(async (el) => {
return ((await el.getText()).split(":").map((e, i) => i == 0 ? parseInt(e) * 60 : parseInt(e))).reduce((partialSum, a) => partialSum + a, 0)
}, () => { return 0 })
if(!globalState[sessionId]) {
driver.quit()
break
}
const username = user[0]
const streams = globalState[sessionId] ? globalState[sessionId].streams : 0
globalState[sessionId] = {
username,
currentSong,
streams,
playbackTime,
target
}
console.log(`[${sessionId}] ${username} - ${currentSong.title} - ${currentSong.artist} - ${playbackTime} - ${streams}`)
// wait 3 seconds
await new Promise((resolve) => setTimeout(resolve, 3000))
}
}
const app = express()
app.get("/", (req, res) => {
res.json(globalState)
})
app.get("/session/:id", (req, res) => {
const id = req.params.id
if (globalState[id]) {
res.json(globalState[id])
} else {
res.status(404).send("Session not found")
}
})
app.get("/start/:type/:id", (req, res) => {
const target = `/${req.params.type}/${req.params.id}`
// check if target is /playlist|album|artist etc/id
if (!target.match(/\/(playlist|album|artist|track)\/[a-zA-Z0-9]+/)) {
res.status(400).send("Invalid target")
return
}
const user = args.users.filter((user) => {
// if used by an session in globalState, return false
if (Object.values(globalState).filter((state) => state.username == user[0]).length > 0) {
return false
}
return true
})[0]
if(!user) {
res.status(400).send("No more users available")
return
}
start(user, `https://open.spotify.com${target}`, globalIterator++)
res.send(`Started session for ${user[0]}`)
})
// start multiple session
app.get("/multistart/:users/:type/:id", (req,res) => {
const target = `/${req.params.type}/${req.params.id}`
// check if target is /playlist|album|artist etc/id
if (!target.match(/\/(playlist|album|artist|track)\/[a-zA-Z0-9]+/)) {
res.status(400).send("Invalid target")
return
}
//users is a count, if set to "available" it will use all available users
const users = args.users.filter((user) => {
// if used by an session in globalState, return false
if (Object.values(globalState).filter((state) => state.username == user[0]).length > 0) {
return false
}
return true
})
let threads: number
if(req.params.users == "available") {
threads = users.length
}else {
threads = parseInt(req.params.users)
}
if(threads > users.length) {
threads = users.length
}
if(threads == 0) {
res.status(400).send("No more users available")
return
}
let i = 0
const interval = setInterval(() => {
if(i >= threads) {
clearInterval(interval)
res.send(`Started ${threads} sessions`)
return
}
start(users[i], `https://open.spotify.com${target}`, globalIterator++)
i++
}, 1500)
})
app.get("/users", (req, res) => {
res.json(args.users.map((user) => {
const sessionId = Object.keys(globalState).filter((key) => globalState[key].username == user[0])[0]
return {
username: user[0],
sessionId,
}
}))
})
app.post("/useradd", (req, res) => {
const username = req.query.username
const password = req.query.password
if (!username || !password) {
res.status(400).send("Missing username or password")
return
}
args.users.push([username, password])
writeFileSync("users.json", JSON.stringify(args.users.map((user)=> ({
username: user[0],
password: user[1],
}))))
res.send("User added")
})
app.get("/users/:username", (req, res) => {
const username = req.params.username
const user = args.users.filter((user) => user[0] == username)[0]
if(!user) {
res.status(404).send("User not found")
return
}
const session = Object.values(globalState).filter((state) => state.username == username)[0]
res.json({
username,
session,
})
})
app.get("/stop/:id", (req, res) => {
const id = req.params.id
if (globalState[id]) {
delete globalState[id]
res.send("Session stopped")
} else {
res.status(404).send("Session not found")
}
})
app.get("/stopall", (req, res) => {
Object.keys(globalState).forEach((key) => {
delete globalState[key]
})
res.send("All sessions stopped")
})
app.listen(args.port, () => {
console.log(`Listening on port ${args.port}`)
})