mirror of
https://codeberg.org/SafeTwitch/safetwitch-backend.git
synced 2025-03-11 13:41:20 -05:00
Return 400 if streamer is not live
This commit is contained in:
parent
99aaace55c
commit
61f5c330d1
2 changed files with 40 additions and 19 deletions
|
@ -5,30 +5,45 @@ import { TwitchChatServer } from './chatIrcProxy'
|
|||
|
||||
const proxyRouter = Router();
|
||||
const twitch = new TwitchAPI()
|
||||
|
||||
|
||||
proxyRouter.get('/img/:base64Url', async (req: Request, res: Response, next: NextFunction) => {
|
||||
const imageUrl = Buffer.from(req.params.base64Url, 'base64url').toString('utf-8')
|
||||
if(!imageUrl) return;
|
||||
const imageUrl = Buffer.from(req.params.base64Url, 'base64url').toString('utf-8')
|
||||
if (!imageUrl) return;
|
||||
|
||||
const imageRes = await fetch(imageUrl)
|
||||
.catch(next)
|
||||
const imageRes = await fetch(imageUrl)
|
||||
.catch(next)
|
||||
|
||||
if(!imageRes) return;
|
||||
if (!imageRes) return;
|
||||
|
||||
if(imageRes.status !== 200) {
|
||||
res.status(imageRes.status).send()
|
||||
}
|
||||
if (imageRes.status !== 200) {
|
||||
res.status(imageRes.status).send()
|
||||
}
|
||||
|
||||
const arrayBuffer = await imageRes.arrayBuffer()
|
||||
const buf = Buffer.from(arrayBuffer)
|
||||
const arrayBuffer = await imageRes.arrayBuffer()
|
||||
const buf = Buffer.from(arrayBuffer)
|
||||
|
||||
res.send(buf)
|
||||
res.send(buf)
|
||||
})
|
||||
|
||||
|
||||
proxyRouter.get('/stream/:username/hls.m3u8', async (req: Request, res: Response, next: NextFunction) => {
|
||||
let m3u8Data = await twitch.getStream(req.params.username)
|
||||
const urlRegex =/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
|
||||
.catch((reason: Error) => {
|
||||
console.log(reason.message)
|
||||
if (reason.message === `Streamer ${req.params.username} is not live`) {
|
||||
res.status(400).send({
|
||||
status: 'error',
|
||||
message: 'Streamer is not live'
|
||||
})
|
||||
return false
|
||||
} else {
|
||||
next(reason)
|
||||
}
|
||||
})
|
||||
|
||||
if (!m3u8Data) return;
|
||||
|
||||
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'));
|
||||
|
||||
|
@ -37,24 +52,25 @@ proxyRouter.get('/stream/:username/hls.m3u8', async (req: Request, res: Response
|
|||
m3u8Data = m3u8Data.replace(url, `${process.env.URL}/proxy/stream/sub/${base64data}`)
|
||||
}
|
||||
|
||||
res.setHeader('Content-type','application/vnd.apple.mpegurl')
|
||||
|
||||
res.setHeader('Content-type', 'application/vnd.apple.mpegurl')
|
||||
res.send(m3u8Data)
|
||||
})
|
||||
|
||||
proxyRouter.get('/stream/sub/:encodedUrl' , async (req: Request, res: Response, next: NextFunction) => {
|
||||
proxyRouter.get('/stream/sub/:encodedUrl', async (req: Request, res: Response, next: NextFunction) => {
|
||||
const unencodedUrl = Buffer.from(req.params.encodedUrl, 'base64url').toString()
|
||||
const m3u8Fetch = await fetch(unencodedUrl)
|
||||
let m3u8Data = await m3u8Fetch.text()
|
||||
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 matches = m3u8Data.match(urlRegex)
|
||||
if(!matches) return;
|
||||
if (!matches) return;
|
||||
|
||||
for (let url of matches) {
|
||||
const base64data = Buffer.from(url).toString('base64url')
|
||||
m3u8Data = m3u8Data.replace(url, `${process.env.URL}/proxy/stream/segment/${base64data}`)
|
||||
}
|
||||
|
||||
res.setHeader('Content-type','application/vnd.apple.mpegurl')
|
||||
res.setHeader('Content-type', 'application/vnd.apple.mpegurl')
|
||||
res.send(m3u8Data)
|
||||
})
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ import { LooseObject } from "../../../types/looseTypes"
|
|||
import { StreamerData, StreamData, Social, Badge } from "../../../types/scraping/Streamer"
|
||||
import { Category, Tag } from "../../../types/scraping/Category"
|
||||
import { CategoryData, CategoryMinifiedStream } from "../../../types/scraping/CategoryData"
|
||||
import { SearchResult } from "../../../types/scraping/Search"
|
||||
|
||||
const base64 = (data: String) => {
|
||||
return Buffer.from(data).toString('base64url')
|
||||
|
@ -258,6 +259,8 @@ export class TwitchAPI {
|
|||
headers: this.headers
|
||||
})
|
||||
|
||||
console.log(res.status)
|
||||
|
||||
return await res.text()
|
||||
}
|
||||
|
||||
|
@ -577,11 +580,13 @@ export class TwitchAPI {
|
|||
}
|
||||
})
|
||||
|
||||
return {
|
||||
const finalData: SearchResult = {
|
||||
channels: formattedStreamers,
|
||||
categories: foundCategories,
|
||||
relatedChannels: foundRelatedLiveChannels,
|
||||
channelsWithTag: foundChannelsWithTag
|
||||
}
|
||||
|
||||
return finalData
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue