From 07668cc092aa1a0714e5cb031b8e9b0f7158f851 Mon Sep 17 00:00:00 2001 From: dragongoose <19649813+dragongoose@users.noreply.github.com> Date: Fri, 17 Mar 2023 21:19:48 -0400 Subject: [PATCH] Add new methods and general fixes --- server/routes/proxyRoute.ts | 65 ++++++++++++------------------------- 1 file changed, 21 insertions(+), 44 deletions(-) diff --git a/server/routes/proxyRoute.ts b/server/routes/proxyRoute.ts index fbed9b2..baa24d4 100644 --- a/server/routes/proxyRoute.ts +++ b/server/routes/proxyRoute.ts @@ -1,9 +1,9 @@ -import { Streamlink } from '@dragongoose/streamlink'; -import { Socket } from 'dgram'; import { Router, Response, Request, NextFunction } from 'express' +import { TwitchAPI } from '../util/scraping/extractor'; import ws from 'ws'; const proxyRouter = Router(); +const twitch = new TwitchAPI() proxyRouter.get('/img', async (req: Request, res: Response, next: NextFunction) => { const imageUrl = req.query.imageUrl?.toString() @@ -27,60 +27,37 @@ proxyRouter.get('/img', async (req: Request, res: Response, next: NextFunction) .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) - const streamlink = new Streamlink(`https://twitch.tv/${req.params.username}`, { - otherArgs: ['--stream-url'] - }) + let m3u8Data = await twitch.getStream(req.params.username) + 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}`) + } - - streamlink.on('log', async (data) => { - // 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) - }) + 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() - fetch(unencodedUrl).then((response) => { - response.body!.pipeTo( - new WritableStream({ - start() { - response.headers.forEach((v, n) => res.setHeader(n, v)); - }, - write(chunk) { - res.write(chunk); - }, - close() { - res.end(); - }, - }) - ); - }) - .catch((err) => next(err)) + const m3u8Fetch = await fetch(unencodedUrl) + var m3u8Data = await m3u8Fetch.text() + + res.send(m3u8Data) }) // IRC PROXY 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.on('message', message => console.log(message.toString())); });