2023-03-24 07:41:33 -04:00
|
|
|
import { Router } from 'express'
|
|
|
|
import { TwitchAPI } from '../util/scraping/extractor/index'
|
|
|
|
|
|
|
|
const profileRouter = Router()
|
|
|
|
const twitch = new TwitchAPI()
|
|
|
|
|
|
|
|
profileRouter.get('/users/:username', async (req, res, next) => {
|
|
|
|
const username = req.params.username
|
2023-04-08 18:15:41 -04:00
|
|
|
console.log(username)
|
2023-03-24 07:41:33 -04:00
|
|
|
|
|
|
|
let streamerData = await twitch.getStreamerInfo(username)
|
|
|
|
.catch(next)
|
|
|
|
|
|
|
|
if (streamerData)
|
2023-04-10 12:15:05 -04:00
|
|
|
res.send({
|
|
|
|
status: 'ok',
|
|
|
|
data: streamerData
|
|
|
|
})
|
2023-03-24 07:41:33 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
profileRouter.get('/discover', async (req, res, next) => {
|
2023-04-11 14:28:39 -04:00
|
|
|
let cursor = req.query.cursor?.toString()
|
2023-03-29 10:03:16 -04:00
|
|
|
let discoveryData;
|
|
|
|
if (cursor) {
|
|
|
|
discoveryData = await twitch.getDirectory(15, cursor)
|
2023-04-16 12:38:34 -04:00
|
|
|
.catch(next)
|
2023-03-29 10:03:16 -04:00
|
|
|
} else {
|
|
|
|
discoveryData = await twitch.getDirectory(15)
|
2023-04-16 12:38:34 -04:00
|
|
|
.catch(next)
|
2023-03-29 10:03:16 -04:00
|
|
|
}
|
|
|
|
|
2023-04-10 12:15:05 -04:00
|
|
|
res.send({
|
|
|
|
status: 'ok',
|
|
|
|
data: discoveryData
|
|
|
|
})
|
2023-03-24 07:41:33 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
profileRouter.get('/discover/:game', async (req, res, next) => {
|
2023-04-11 14:28:39 -04:00
|
|
|
let discoveryData;
|
|
|
|
let cursor = req.query.cursor?.toString()
|
|
|
|
if(cursor) {
|
|
|
|
discoveryData = await twitch.getDirectoryGame(req.params.game, 50, cursor)
|
|
|
|
.catch(next)
|
|
|
|
} else {
|
|
|
|
discoveryData = await twitch.getDirectoryGame(req.params.game, 50)
|
|
|
|
.catch(next)
|
|
|
|
}
|
2023-03-31 07:56:38 -04:00
|
|
|
|
|
|
|
if(discoveryData)
|
2023-04-10 12:15:05 -04:00
|
|
|
res.send({
|
|
|
|
status: 'ok',
|
|
|
|
data: discoveryData
|
|
|
|
})
|
2023-03-31 07:56:38 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
profileRouter.get('/badges', async (req, res, next) => {
|
|
|
|
let badges = await twitch.getTwitchBadges()
|
|
|
|
.catch(next)
|
|
|
|
|
|
|
|
if(req.query.streamerName) {
|
|
|
|
let broadcastBadges = await twitch.getStreamerBadges(req.query.streamerName.toString())
|
|
|
|
.catch(next)
|
|
|
|
if(broadcastBadges && badges)
|
|
|
|
badges = [...badges, ...broadcastBadges]
|
|
|
|
}
|
|
|
|
|
|
|
|
if(badges)
|
2023-04-10 12:15:05 -04:00
|
|
|
res.send({
|
|
|
|
status: 'ok',
|
|
|
|
data: badges
|
|
|
|
})
|
2023-03-24 07:41:33 -04:00
|
|
|
})
|
|
|
|
|
2023-04-16 12:38:34 -04:00
|
|
|
profileRouter.get('/search', async (req, res, next) => {
|
|
|
|
const query = req.query.query
|
|
|
|
if(!query)
|
|
|
|
return res.status(400).send({
|
|
|
|
status: 'error',
|
|
|
|
message: 'No query provided'
|
|
|
|
})
|
|
|
|
|
|
|
|
const result = await twitch.getSearchResult(query.toString())
|
|
|
|
.catch(next)
|
|
|
|
res.send({
|
|
|
|
status: 'ok',
|
|
|
|
data: result
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-03-24 07:41:33 -04:00
|
|
|
export default profileRouter
|