-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateCurl.js
136 lines (117 loc) · 3.51 KB
/
generateCurl.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
'use strict';
const open = require('open');
const request = require('request');
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const app = express();
let parser = bodyParser.urlencoded({extended: false});
const port = 3000;
const hotkeys = require('node-hotkeys')
let client_id = '8bcb18f826f0438cabccb8538a12bfaa'; // Definitely not a client_id
let client_secret = 'suck my dick lmao'; // Definitely a client_secret
let redirect_uri = 'http:%2F%2Flocalhost:3000%2Fcallback'
let auth = `https://accounts.spotify.com/authorize?client_id=${client_id}&redirect_uri=${redirect_uri}&scope=user-read-playback-state%20user-modify-playback-state&response_type=token`;
let token = "";
function getDeviceStatus(){
let options = {
url: 'https://api.spotify.com/v1/me/player/devices',
headers: {
'Authorization': 'Bearer ' + token
},
json: true
};
return new Promise((resolve, reject) => {
request.get(options, function(error, response, body) {
let allDevices = body["devices"];
allDevices.forEach((el) => {
if(el["is_active"]){
let devId = el["id"];
let currVol = el["volume_percent"];
resolve({"devId": devId, "currVol": currVol});
}
});
reject("No playing device");
});
})
}
function setVolume(newVol, changeVol){
var headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer '+token
};
getDeviceStatus().then((devStatus) => {
let device = devStatus["devId"];
if(changeVol) {
newVol = devStatus["currVol"] + changeVol;
}
var options = {
url: `https://api.spotify.com/v1/me/player/volume?volume_percent=${newVol}&device_id=${device}`,
method: 'PUT',
headers: headers
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
request(options, callback);
}).catch((err) => {
console.log(err);
});
}
function setHotkey(conf){
hotkeys.on({
hotkeys: conf["key"],
callback: function(hotkey) {
if(conf["volumeSet"]) {
console.log(`${conf["key"]} sets volume to ${conf["volumeSet"]}`);
setVolume(conf["volumeSet"], null);
}
if(conf["volumeChange"]) {
console.log(`${conf["key"]} changes volume by ${conf["volumeChange"]}`);
setVolume(null, conf["volumeChange"]);
}
}
})
}
function configureDevice(){
let rawConfig = fs.readFileSync('config.json');
let parsedConfig = JSON.parse(rawConfig);
parsedConfig["keybinds"].forEach((el) => {
setHotkey(el);
});
(async () => {
while (parsedConfig["showKeyDetections"]) {
let hotkeyStr = await hotkeys.getNextHotkey(false);
console.log("Detected:", hotkeyStr);
}
})();
}
let frontendTokenScript =
`
<script>
var token = window.location.hash.match(/.+\=(.+)&t/)[1]
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost:3000/set-token', true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
window.close();
}
}
xhr.send("token="+token);
</script>
`;
app.get('/callback', (req, res) => {
res.send(frontendTokenScript);
});
app.post('/set-token', parser, (req,res) => {
res.send(req.body.token);
token = req.body.token;
configureDevice();
})
app.listen(port, () => {
open(auth);
});