-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
222 lines (184 loc) · 6.9 KB
/
api.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 path = require('path');
var fs = require('fs');
var async = require('async');
var MusicService = require('./services/music/service/spotify/spotify.js');
var SocialService = require('./services/social/service/mock.js');
var social = new SocialService();
var music = new MusicService();
var less = require('less');
var request = require('request');
var url = require('url');
var utils = require('./utils.js');
var app = utils.createApp();
app.get('/settings.json', function (req, res) {
if (fs.existsSync(path)) {
var settings = JSON.parse(fs.readFileSync('./settings.json'));
res.json(settings);
} else {
var settings = {
'bungalows': {},
'apps': [],
'theme': 'spotify09',
'light': true,
'primaryColor': '#FB8521'
};
fs.writeFileSync('./settings.json', settings);
return settings;
}
});
app.get('/services/:id/authenticate', function (req, res) {
console.log("Got authenticate request");
music.authenticate(req.query.code).then(function (success) {
console.log("success");
res.statusCode = 200;
res.send('success')
res.end();
}, function (error) {
console.log(error);
res.statusCode = 500;
res.end(error);
});
});
app.put('/settings.json', function (req, res) {
var settings = JSON.stringify(req.json);
fs.writeFileSync('settings.json', settings);
var lightTheme = fs.readFileSync(process.env.PWD + '/themes/' + settings.theme + '/css/light.less', {'encoding': 'utf-8'});
console.log(process.env.PWD + '/themes/' + settings.theme + '/css/style.less');
lightTheme = lightTheme.replace(/\@primary-color/, settings.primaryColor);
lightTheme = lightTheme.replace(/\@secondary-color/, settings.secondaryColor);
lightTheme = lightTheme.replace("@islight", true ? '@light' : '@dark');
lightTheme = lightTheme.replace("@isdark", !true ? '@light' : '@dark');
var darkTheme = fs.readFileSync(process.env.PWD + '/themes/' + settings.theme + '/css/dark.less', {'encoding': 'utf-8'});
console.log(process.env.PWD + '/themes/' + settings.theme + '/css/style.less');
darkTheme = darkTheme.replace(/\@primary-color/, settings.primaryColor);
darkTheme = darkTheme.replace(/\@secondary-color/, settings.secondaryColor);
darkTheme = darkTheme.replace("@islight", true ? '@light' : '@dark');
darkTheme = darkTheme.replace("@isdark", !true ? '@light' : '@dark');
//alert(theme);
less.render(lightTheme, {}, function (error, output) {
console.log(error, output);
//alert(output);
fs.writeFileSync(process.env.PWD + '/themes/' + settings.theme + '/css/light.css', output.css);
});
less.render(darkTheme, {}, function (error, output) {
console.log(error, output);
//alert(output);
fs.writeFileSync(process.env.PWD + '/themes/' + settings.theme + '/css/main.scss', output.css);
});
fs.writeFileSync(process.env.PWD + '/themes/' + settings.theme + '/css/main.scss', '@import url("' + (settings.light ? 'light' : 'dark') + '.css")');
var mainCSS = '@import url("' + settings.theme + '/css/style.css")';
fs.writeFileSync(process.env.PWD + '/themes/main.scss', mainCSS);
fs.writeFileSync(process.env.PWD + '/themes/main.scss', '@import url("http://127.0.0.1:9261/themes/' + settings.theme + '/css/main.scss")');
fs.writeFileSync(process.env.PWD + '/themes/light.css', '@import url("http://127.0.0.1:9261/themes/' + settings.theme + '/css/light.css")');
fs.writeFileSync(process.env.PWD + '/themes/main.scss', '@import url("http://127.0.0.1:9261/themes/' + settings.theme + '/css/main.scss")');
})
/*
app.post('/api/login', function (req, res) {
});
app.get('/api/albums/:id', function (req, res) {
var id = req.params.id;
music.getAlbumById(id).then(function (artist) {
var data = JSON.stringify(artist);
res.json(artist);
});
});
app.get('/api/albums/:id/tracks', function (req, res) {
var id = req.params.id;
music.getAlbumTracks(id).then(function (artist) {
var data = JSON.stringify(artist);
res.json(artist);
});
});
app.get('/api/playlists/:id/tracks', function (req, res) {
var id = req.params.id;
music.getPlaylistTracks(id).then(function (artist) {
var data = JSON.stringify(artist);
res.json(artist);
});
});
app.get('/api/search', function (req, res) {
var q = req.query.q;
var type = req.query.type;
var offset = req.query.offset;
var limit = req.query.limit;
music.search(q, type, limit, offset).then(function (data) {
res.json(data);
});
});
app.get('/api/artists/:id', function (req, res) {
var id = req.params.id;
music.getArtistById(id).then(function (artist) {
var data = JSON.stringify(artist);
res.json(artist);
});
});
app.get('/api/albums/:id', function (req, res) {
var id = req.params.id;
music.getAlbumById(id).then(function (artist) {
var data = JSON.stringify(artist);
res.json(data);
});
});
app.get('/api/users/:username/playlists/:id', function (req, res) {
var username = req.params.username;
var id = req.params.id;
music.getPlaylistById(username, id).then(function (playlist) {
res.json(playlist);
});
});
app.get('/api/users/:username/playlists', function (req, res) {
var username = req.params.username;
var id = req.params.id;
music.getPlaylistsForUser(username).then(function (data) {
res.json(data);
});
});
*/
/*
app.get('/api/users/:username', function (req, res) {
var username = req.params.username;
var id = req.params.id;
music.getUserById(username).then(function (data) {
res.json(data);
});
});
app.get('/api/users/:username/playlists/:id/tracks', function (req, res) {
var username = req.params.username;
var id = req.params.id;
music.getPlaylistTracks(username, id).then(function (playlist) {
var data = JSON.stringify(playlist);
res.json(data);
});
});
app.get('/chrome/*', function (req, res) {
var app_path = req.params[0].split('/');
var file = fs.readFileSync('./' + app_path.join('/'));
res.write(file);
res.end();
});*/
app.get('/social/*', function (req, res) {
console.log("A");
console.log(social);
social.request("GET", req.params[0], req.query).then(function (result) {
res.json(result);
}, function (reject) {
res.json(reject);
});
});
app.get('/music/*', function (req, res) {
console.log("A");
console.log(music);
music.request("GET", req.params[0], req.query).then(function (result) {
res.json(result);
}, function (reject) {
res.json(reject);
});
});
app.get('/player/play', function (req, res) {
var id = req.params.id;
music.getAlbumTracks(id).then(function (artist) {
var data = JSON.stringify(artist);
res.json(artist);
});
});
module.exports = app;