/
server.js
174 lines (159 loc) · 4.45 KB
/
server.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
var express = require('express')
var app = express()
var path = require('path')
var http = require('http').Server(app)
var io = require('socket.io').listen(http)
var clk = require('chalk')
var bodyParser = require('body-parser')
var cors = require('cors')
var readline = require('readline')
var getVideoId = require('get-video-id')
var fetchVideoInfo = require('youtube-info')
app.use(bodyParser.json()) // support json encoded bodies
app.use(bodyParser.urlencoded({extended: true})) // support encoded bodies
app.use(cors())
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
app.options('*', cors())
var current = null
var playing = false
var playlistMode = false
var shuffle = false
var playlist = []
var volume = 100
app.get('/player', function (req, res) {
res.sendFile(__dirname + '/player.html')
})
Array.prototype.insert = function (index, item) {
this.splice(index, 0, item)
}
io.on('connection', function (socket) {
socket.on('add', function (url) {
var video = getVideoId(url)
if (video !== undefined && video.id !== undefined) {
console.log(clk.blue('Adding video ') + clk.blue.bold(video.id))
if (video.service === 'youtube') {
playlist.push(video.id)
if (current === null) {
next(io)
}
}
}
updateInfo(io)
})
socket.on('remove', function (index) {
console.log(clk.blue('Removing video ') + clk.blue.bold(playlist[index]))
playlist.splice(index, 1)
updateInfo(io)
})
socket.on('next', function (ignored) {
console.log(clk.blue('Skipping current'))
next(io)
updateInfo(io)
})
socket.on('finished', function (ignored) {
console.log(clk.yellow('Finished current'))
next(io)
updateInfo(io)
})
socket.on('get', function (ignored) {
updateInfo(socket)
socket.emit('volume', volume)
socket.emit('playing', playing)
})
socket.on('playing', function (bool) {
if (current !== null) {
playing = bool
io.emit('playing', bool)
}
})
socket.on('volume', function (value) {
console.log(clk.blue('Set volume to ') + clk.blue.bold(value))
volume = value
io.emit('volume', volume)
})
socket.on('playlistMode', function (value) {
console.log(clk.blue('Set playlist mode to ') + clk.blue.bold(value))
playlistMode = value
io.emit('playlistMode', playlistMode)
})
socket.on('shuffle', function (value) {
console.log(clk.blue('Set shuffle to ') + clk.blue.bold(value))
shuffle = value
io.emit('shuffle', shuffle)
})
})
function next (io) {
console.log(clk.blue('Finished video ') + clk.blue.bold(current))
if (playlist.length !== 0) {
if (shuffle) {
playlist = shuffleArray(playlist)
}
if (playlistMode) {
playlist.push(playlist[0])
}
setVideo(playlist.shift(), io)
} else {
current = null
playing = false
io.emit('playing', playing)
console.log(clk.yellow('List empty, waiting for queue'))
}
}
function shuffleArray (array) {
var currentIndex = array.length, temporaryValue, randomIndex
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex)
currentIndex -= 1
// And swap it with the current element.
temporaryValue = array[currentIndex]
array[currentIndex] = array[randomIndex]
array[randomIndex] = temporaryValue
}
return array
}
function setVideo (id, target) {
current = id
console.log(clk.green('Playing video ') + clk.green.bold(id))
target.emit('play', id)
}
function updateInfo (target) {
if (current === null) {
target.emit('current', null)
} else {
fetchVideoInfo(current, function (err, info) {
target.emit('current', {
'id': current,
'info': info
})
})
}
var prettyPlaylist = playlist.concat()
var counter = 0
if (playlist.length > 0) {
prettyPlaylist.forEach(function (item, index, array) {
fetchVideoInfo(item, function (err, res) {
array[index] = ({
id: item,
info: res
})
counter++
if (counter === playlist.length) {
target.emit('list', prettyPlaylist)
}
})
})
} else {
target.emit('list', [])
}
target.emit('shuffle', shuffle)
target.emit('playlistMode', playlistMode)
}
app.use('/client', express.static(path.join(__dirname + '/client')))
http.listen(1337, function () {
console.log(clk.green.bold('listening on *:' + 1337))
})