-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
158 lines (133 loc) · 4.4 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
152
153
154
155
156
157
158
const http = require("http");
const gopher = require("gopherserver.js");
const fs = require('fs');
const os = require('os');
const WebSocket = require("ws").Server;
const openradio = require("openradio");
const download = require("./download");
const radio = openradio({
format: "mp3",
bitrate: 128
});
const YouTube = require("youtubei.js");
let client = null;
let curSong = null;
let url = process.argv.slice(2)[0] || fs.readFileSync("yturl.txt", 'utf8');
// Query
let playlist = [];
// Sink management
let wsClient = new Map();
// Server
let gopherServer = gopher();
gopherServer.on('request', soc => {
if (!soc.url || soc.url === "/") {
let page =
soc.send(`!ytradio server
i ${os.hostname()}\t${process.env.GOPHER_PORT || 8081}
iWelcome to this ytradio livestream server. ${os.hostname()}\t${process.env.GOPHER_PORT || 8081}
sStream here (Or /9/stream) /stream ${os.hostname()}\t${process.env.GOPHER_PORT || 8081}
i ${os.hostname()}\t${process.env.GOPHER_PORT || 8081}
iNow Playing: ${curSong.basic_info.author} - ${curSong.basic_info.title} ${os.hostname()}\t${process.env.GOPHER_PORT || 8081}
hThis livestream runs on ytradio project. URL:https://github.com/Yonle/ytradio ${os.hostname()}\t${process.env.GOPHER_PORT || 8081}
`);
} else if (soc.url === "/stream") {
let conn = repeater(soc);
soc.on('error', err => {
conn();
});
}
});
let repeater = openradio.repeater(radio);
let server = http.createServer(function(req, res) {
res.setHeader("content-type", "audio/mpeg");
if (req.method === "HEAD") return res.end();
const conn = repeater(res);
res.on('error', err => {
conn();
});
});
YouTube.Innertube.create({ location: process.env.GEOLOCATION || "US" }).then(a => {
client = a;
server.listen(process.env.PORT || 8080, () => launch());
gopherServer.listen(process.env.GOPHER_PORT || 8081);
gopherServer.on('error', console.error);
server.on('error', console.error);
});
// Websocket, for Song name information
let wss = new WebSocket({ server });
wss.on('connection', (ws, req) => {
let id = Math.random().toString(36).slice(2);
wsClient.set(id, ws);
if (curSong) ws.send(`${curSong.basic_info.author} - ${curSong.basic_info.title}`);
req.on('close', function() {
wsClient.delete(id);
});
});
wss.broadcast = (function(data) {
console.log("-- Now Playing:", data);
wsClient.forEach(function(ws, id) {
ws.send(data, function(error) {
if (error) {
wsClient.delete(id);
}
});
});
});
function getVideoID(url) {
let u = new URL(url);
if (u.hostname === "youtu.be") return u.pathname.slice(1);
if (u.searchParams.has("v")) return u.searchParams.get("v");
}
function getStreamingData(song) {
let streamingData = song.streaming_data.adaptive_formats
.filter(i => !i.has_video && i.has_audio)
.pop();
streamingData.url = streamingData.decipher(client.session.player);
return streamingData;
}
// Player
let launch = function() {
if (!url || url.length < 1) {
console.error("No youtube URL provided. Aborting....");
return process.exit(1);
} else {
console.log('Radio is now listening on port', process.env.PORT || 8080);
console.log('Gopher server is now listening on port', process.env.GOPHER_PORT || 8081, "(Change by setting GOPHER_PORT env variable)");
return play();
}
};
let play = async function() {
try {
if (!curSong && !playlist.length && url) {
let song = await client.music.getInfo(getVideoID(url));
playlist = (await song.getUpNext()).contents;
}
let song = await client.music.getInfo(playlist.shift().video_id);
if (!playlist.length) {
playlist = (await curSong.getUpNext()).contents;
}
if (song.playability_status.status !== "OK") {
console.error(`-! "${song.basic_info.title}" could not be played.`);
console.error(` Reason: ${song.playability_status.reason}`);
console.error(' Skipping....');
return play();
}
radio.play(await download(getStreamingData(song)));
wss.broadcast(`${song.basic_info.author} - ${song.basic_info.title}`);
console.log(" Up next:", `${playlist[0].author} - ${playlist[0].title.text}`);
curSong = song;
fs.writeFileSync("yturl.txt", "https://youtu.be/" + song.basic_info.id, "utf8");
} catch (err) {
console.error(err);
play();
}
};
radio.on('finish', () => {
playing = false;
play();
});
radio.on('error', (e) => {
playing = false;
console.error(e);
play();
});