Skip to content

Commit

Permalink
highlights: Remove not needed helix call
Browse files Browse the repository at this point in the history
Also fix incorrect time on highlights if streams are merged
  • Loading branch information
sogehige committed Oct 1, 2018
1 parent 4dbe22d commit 92a01bb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
10 changes: 9 additions & 1 deletion src/bot/api.js
Expand Up @@ -16,6 +16,8 @@ class API {
this.maxRetries = 3
this.curRetries = 0
this.streamType = 'live'
this.streamId = null
this.streamStartedAt = Date.now()

this.gameOrTitleChangedManually = false

Expand Down Expand Up @@ -501,6 +503,11 @@ class API {
// correct status and we've got a data - stream online
let stream = request.data.data[0]

// Always keep this updated
this.streamStartedAt = stream.started_at
this.streamId = stream.id
this.streamType = stream.type

if (!moment.preciseDiff(moment(stream.started_at), moment((await global.cache.when()).online), true).firstDateWasLater) await global.cache.when({ online: stream.started_at })
if (!await global.cache.isOnline() || this.streamType !== stream.type) {
this.chatMessagesAtStart = global.linesParsed
Expand All @@ -519,7 +526,6 @@ class API {

this.curRetries = 0
this.saveStreamData(stream)
this.streamType = stream.type
await global.cache.isOnline(true)

if (!justStarted) {
Expand Down Expand Up @@ -580,6 +586,8 @@ class API {

await global.db.engine.remove('cache.hosts', {}) // we dont want to have cached hosts on stream start
await global.db.engine.remove('cache.raids', {}) // we dont want to have cached raids on stream start

this.streamId = null
}
}
} catch (e) {
Expand Down
27 changes: 7 additions & 20 deletions src/bot/systems/highlights.js
Expand Up @@ -5,7 +5,6 @@ var _ = require('lodash')
var moment = require('moment')
require('moment-precise-range-plugin')
const cluster = require('cluster')
const axios = require('axios')

// bot libraries
const constants = require('../constants')
Expand Down Expand Up @@ -60,7 +59,6 @@ class Highlights extends System {
} else {
const cid = await global.oauth.settings._.channelId
const when = await global.cache.when()
const url = `https://api.twitch.tv/helix/videos?user_id=${cid}&type=archive&first=1`

const needToWait = _.isNil(cid) || cid === ''
const notEnoughAPICalls = this.remainingAPICalls <= 10 && this.refreshAPICalls > _.now() / 1000
Expand All @@ -75,28 +73,17 @@ class Highlights extends System {
const token = await global.oauth.settings.bot.accessToken
if (token === '') return

// we need to load video id
const request = await axios.get(url, {
headers: {
'Authorization': 'Bearer ' + token
}
})

// save remaining api calls
global.api.remainingAPICalls = request.headers['ratelimit-remaining']
global.api.refreshAPICalls = request.headers['ratelimit-reset']

let highlight = request.data.data[0]
let timestamp = moment.preciseDiff(moment().valueOf(), moment(when.online).valueOf(), true)
highlight.timestamp = { hours: timestamp.hours, minutes: timestamp.minutes, seconds: timestamp.seconds }
highlight.game = _.get(await global.db.engine.findOne('api.current', { key: 'game' }), 'value', 'n/a')
highlight.title = _.get(await global.db.engine.findOne('api.current', { key: 'status' }), 'value', 'n/a')
let timestamp = moment.preciseDiff(moment().valueOf(), moment(global.api.streamStartedAt).valueOf(), true)
let highlight = {
id: global.api.streamId,
timestamp: { hours: timestamp.hours, minutes: timestamp.minutes, seconds: timestamp.seconds },
game: _.get(await global.db.engine.findOne('api.current', { key: 'game' }), 'value', 'n/a'),
title: _.get(await global.db.engine.findOne('api.current', { key: 'status' }), 'value', 'n/a')
}

this.add(highlight, timestamp, opts.sender)
global.panel.io.emit('api.stats', { data: request.data, timestamp: _.now(), call: 'highlights', api: 'helix', endpoint: url, code: request.status, remaining: global.api.remainingAPICalls })
} catch (e) {
if (e.message !== ERROR_STREAM_NOT_ONLINE) {
global.panel.io.emit('api.stats', { timestamp: _.now(), call: 'highlights', api: 'helix', endpoint: url, code: `${e.status} ${_.get(e, 'body.message', e.statusText)}`, remaining: global.api.remainingAPICalls })
global.log.error(e.stack)
}
switch (e.message) {
Expand Down
8 changes: 8 additions & 0 deletions src/bot/webhooks.js
Expand Up @@ -220,10 +220,18 @@ class Webhooks {

if (parseInt(stream.user_id, 10) !== parseInt(cid, 10)) return

// Always keep this updated
this.streamStartedAt = stream.started_at
this.streamId = stream.id
this.streamType = stream.type

await global.db.engine.update('api.current', { key: 'status' }, { value: stream.title })
await global.db.engine.update('api.current', { key: 'game' }, { value: await global.api.getGameFromId(stream.game_id) })

if (!await global.cache.isOnline() || global.twitch.streamType !== stream.type) {
global.log.start(
`id: ${stream.id} | startedAt: ${stream.started_at} | title: ${stream.title} | game: ${await this.getGameFromId(stream.game_id)} | type: ${stream.type}`
)
global.cache.when({ online: stream.started_at })
global.api.chatMessagesAtStart = global.linesParsed

Expand Down

0 comments on commit 92a01bb

Please sign in to comment.