mirror of
https://codeberg.org/dragongoose/safetwitch.git
synced 2024-11-08 01:11:42 +00:00
Add new methods and general fixes
This commit is contained in:
parent
42e4503383
commit
07668cc092
1 changed files with 21 additions and 44 deletions
|
@ -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()));
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue