-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
151 lines (122 loc) · 3.51 KB
/
index.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
// app.js
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const { google } = require('googleapis');
const youtubeSearch = require('youtube-search');
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('./media.db');
console.log(db)
require('dotenv').config();
const port = process.env.PORT;
const API_KEY = process.env.API_KEY;
const googleApiKey = API_KEY; // Ganti dengan API key Google Anda
const youtubeSearchOpts = {
maxResults: 50,
key: googleApiKey,
};
app.use(express.static('public'));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
app.get('/play', (req, res) => {
res.sendFile(__dirname + '/public/player.html');
});
const youtube = google.youtube({
version: 'v3',
auth: googleApiKey,
});
io.on('connection', (socket) => {
socket.on('search', (query) => {
youtubeSearch(query+'', youtubeSearchOpts, (err, results) => {
if (err) return console.log(err);
socket.emit('searchResults', results);
});
});
socket.on('timeline', (query) => {
// console.log(timeline)
io.emit('update_timeline', query);
});
socket.on('set_volume', (vol) => {
// console.log(timeline)
io.emit('volume', vol);
});
socket.on('set_fun', (fun) => {
io.emit('func', fun);
});
socket.on('playVideo', (id,videoId, title,thum) => {
io.emit('loadVideo', videoId, title,thum);
});
socket.on('next', () => {
let playlistArray = [];
db.serialize(() => {
db.each("SELECT * FROM playlist ORDER BY id ASC LIMIT 1", (err, row) => {
if (err) {
console.error(err.message);
} else {
// Menambahkan row ke dalam array
playlistArray.push(row);
// console.log(row)
}
},()=>{
if (playlistArray.length > 0)
{
// console.log(playlistArray[0])
io.emit('loadVideo', playlistArray[0].id_video, playlistArray[0].title,playlistArray[0].thum);
db.serialize(() => {
db.run("DELETE FROM playlist WHERE id = "+playlistArray[0].id);
});
}
else
{
// console.log('Not Song')
}
io.emit('next_song','')
});
});
});
socket.on('selectVideo', (videoId, title,thum) => {
try{
db.serialize(() => {
// Menyimpan data ke dalam tabel users
const stmt = db.prepare("INSERT INTO playlist (id_video, title,thum) VALUES (?, ?,?)");
stmt.run(videoId, title,thum);
stmt.finalize();
});
// return true;
}catch{
return false;
}
});
//get playlist
socket.on('get_playlist',(ee)=>{
let playlistArray = [];
db.serialize(() => {
db.each("SELECT * FROM playlist", (err, row) => {
if (err) {
console.error(err.message);
} else {
// Menambahkan row ke dalam array
playlistArray.push(row);
}
}, () => {
socket.emit('res_playlist', playlistArray);
});
});
});
socket.on('delete_playlist',(id)=>{
db.serialize(() => {
db.run("DELETE FROM playlist WHERE id = "+id);
});
// console.log('del oke');
});
});
server.listen(port, () => {
console.log('Server is running');
});
process.on('exit', () => {
db.close();
});