Add new methods and general fixes
This commit is contained in:
parent
42e4503383
commit
07668cc092
@ -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,22 +27,11 @@ 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']
|
|
||||||
})
|
|
||||||
|
|
||||||
streamlink.begin()
|
|
||||||
|
|
||||||
|
|
||||||
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 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)
|
const matches = m3u8Data.match(urlRegex)
|
||||||
if (!matches) return next(new Error('Error proxying HLS'));
|
if (!matches) return next(new Error('Error proxying HLS'));
|
||||||
|
|
||||||
@ -54,33 +43,21 @@ proxyRouter.get('/stream/:username/hls.m3u8', (req: Request, res: Response, next
|
|||||||
res.setHeader('Content-type','application/vnd.apple.mpegurl')
|
res.setHeader('Content-type','application/vnd.apple.mpegurl')
|
||||||
res.send(m3u8Data)
|
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()));
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user