Add new methods and general fixes

This commit is contained in:
dragongoose 2023-03-17 21:19:48 -04:00
parent 42e4503383
commit 07668cc092

View File

@ -1,9 +1,9 @@
import { Streamlink } from '@dragongoose/streamlink';
import { Socket } from 'dgram';
import { Router, Response, Request, NextFunction } from 'express' import { Router, Response, Request, NextFunction } from 'express'
import { TwitchAPI } from '../util/scraping/extractor';
import ws from 'ws'; import ws from 'ws';
const proxyRouter = Router(); const proxyRouter = Router();
const twitch = new TwitchAPI()
proxyRouter.get('/img', async (req: Request, res: Response, next: NextFunction) => { proxyRouter.get('/img', async (req: Request, res: Response, next: NextFunction) => {
const imageUrl = req.query.imageUrl?.toString() const imageUrl = req.query.imageUrl?.toString()
@ -27,60 +27,37 @@ proxyRouter.get('/img', async (req: Request, res: Response, next: NextFunction)
.catch((err) => next(err)) .catch((err) => next(err))
}) })
proxyRouter.get('/stream/:username/hls.m3u8', (req: Request, res: Response, next: NextFunction) => {
proxyRouter.get('/stream/:username/hls.m3u8', async (req: Request, res: Response, next: NextFunction) => {
console.log(req.params.username) console.log(req.params.username)
const streamlink = new Streamlink(`https://twitch.tv/${req.params.username}`, { let m3u8Data = await twitch.getStream(req.params.username)
otherArgs: ['--stream-url'] const urlRegex =/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
}) const matches = m3u8Data.match(urlRegex)
if (!matches) return next(new Error('Error proxying HLS'));
streamlink.begin() for (let url of matches) {
const base64data = Buffer.from(url).toString('base64url')
m3u8Data = m3u8Data.replace(url, `${process.env.URL}/proxy/hls/${base64data}`)
}
res.setHeader('Content-type','application/vnd.apple.mpegurl')
streamlink.on('log', async (data) => { res.send(m3u8Data)
// m3u8 url
let twitchM3u8url = data.toString()
const urlRegex =/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
const twitchRes = await fetch(twitchM3u8url)
let m3u8Data = await twitchRes.text()
const matches = m3u8Data.match(urlRegex)
if (!matches) return next(new Error('Error proxying HLS'));
for (let url of matches) {
const base64data = Buffer.from(url).toString('base64url')
m3u8Data = m3u8Data.replace(url, `${process.env.URL}/proxy/hls/${base64data}`)
}
res.setHeader('Content-type','application/vnd.apple.mpegurl')
res.send(m3u8Data)
})
}) })
proxyRouter.get('/hls/:encodedUrl' , (req: Request, res: Response, next: NextFunction) => { proxyRouter.get('/hls/:encodedUrl' , async (req: Request, res: Response, next: NextFunction) => {
console.log('hi')
const unencodedUrl = Buffer.from(req.params.encodedUrl, 'base64url').toString() const unencodedUrl = Buffer.from(req.params.encodedUrl, 'base64url').toString()
fetch(unencodedUrl).then((response) => { const m3u8Fetch = await fetch(unencodedUrl)
response.body!.pipeTo( var m3u8Data = await m3u8Fetch.text()
new WritableStream({
start() { res.send(m3u8Data)
response.headers.forEach((v, n) => res.setHeader(n, v));
},
write(chunk) {
res.write(chunk);
},
close() {
res.end();
},
})
);
})
.catch((err) => next(err))
}) })
// IRC PROXY // IRC PROXY
export const wsServer = new ws.Server({ noServer: true }); export const wsServer = new ws.Server({ noServer: true });
wsServer.on('connection', (socket: Socket) => { wsServer.on('connection', (socket: ws.WebSocket) => {
socket.send('Welcome! Send a comma seperated list to get the IRC') socket.send('Welcome! Send a comma seperated list to get the IRC')
socket.on('message', message => console.log(message.toString())); socket.on('message', message => console.log(message.toString()));
}); });