mirror of
https://codeberg.org/SafeTwitch/safetwitch.git
synced 2024-12-22 13:22:58 -05:00
Remove console.log and add more methods
This commit is contained in:
parent
cb4dd834d0
commit
c9ca366ff9
1 changed files with 67 additions and 4 deletions
|
@ -90,9 +90,6 @@ export class TwitchAPI {
|
|||
})
|
||||
|
||||
const data = await res.json()
|
||||
console.log(data[1].data, data[1].data.user.stream)
|
||||
|
||||
|
||||
const rawStreamerData = data[0].data
|
||||
|
||||
|
||||
|
@ -179,11 +176,77 @@ export class TwitchAPI {
|
|||
})
|
||||
|
||||
const rawData = await res.json()
|
||||
console.log(rawData)
|
||||
|
||||
if(!rawData[0].data.user.stream)
|
||||
return Promise.reject(new Error(`Streamer ${streamerName} is not live`))
|
||||
|
||||
return Promise.resolve(rawData[0].data.user.stream.viewersCount)
|
||||
}
|
||||
|
||||
public isLive = async (streamerName: string) => {
|
||||
const payload = [
|
||||
{
|
||||
"operationName": "UseViewCount",
|
||||
"variables": {
|
||||
"channelLogin": streamerName
|
||||
},
|
||||
"extensions": {
|
||||
"persistedQuery": {
|
||||
"version": 1,
|
||||
"sha256Hash": "00b11c9c428f79ae228f30080a06ffd8226a1f068d6f52fbc057cbde66e994c2"
|
||||
}
|
||||
}
|
||||
},
|
||||
]
|
||||
|
||||
const res = await fetch(this.twitchUrl, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(payload),
|
||||
headers: this.headers
|
||||
})
|
||||
|
||||
const rawData = await res.json()
|
||||
|
||||
if(!rawData[0].data.user.stream)
|
||||
return Promise.resolve(false)
|
||||
|
||||
return Promise.resolve(true)
|
||||
}
|
||||
|
||||
public getStream = async (streamerName: string) => {
|
||||
const isLive = await this.isLive(streamerName)
|
||||
if(!isLive) return Promise.reject(new Error(`Streamer ${streamerName} is not live`))
|
||||
|
||||
// Get token
|
||||
const payload = {
|
||||
"operationName": "PlaybackAccessToken_Template",
|
||||
"query": "query PlaybackAccessToken_Template($login: String!, $isLive: Boolean!, $vodID: ID!, $isVod: Boolean!, $playerType: String!) { streamPlaybackAccessToken(channelName: $login, params: {platform: \"web\", playerBackend: \"mediaplayer\", playerType: $playerType}) @include(if: $isLive) { value signature __typename } videoPlaybackAccessToken(id: $vodID, params: {platform: \"web\", playerBackend: \"mediaplayer\", playerType: $playerType}) @include(if: $isVod) { value signature __typename }}",
|
||||
"variables": {
|
||||
"isLive": true,
|
||||
"login": streamerName,
|
||||
"isVod": false,
|
||||
"vodID": "",
|
||||
"playerType": "site"
|
||||
}
|
||||
}
|
||||
|
||||
var res = await fetch(this.twitchUrl, {
|
||||
headers: this.headers,
|
||||
body: JSON.stringify(payload),
|
||||
method: 'POST'
|
||||
})
|
||||
|
||||
const data = await res.json()
|
||||
|
||||
const token = data.data.streamPlaybackAccessToken.value
|
||||
const signature = data.data.streamPlaybackAccessToken.signature
|
||||
|
||||
const playlistUrl = `https://usher.ttvnw.net/api/channel/hls/${streamerName.toLowerCase()}.m3u8`
|
||||
const params = `?sig=${signature}&token=${token}`
|
||||
var res = await fetch(playlistUrl + params, {
|
||||
headers: this.headers
|
||||
})
|
||||
|
||||
return await res.text()
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue