Skip to content

Commit

Permalink
highlights: fix incorrect highlight save
Browse files Browse the repository at this point in the history
Fixes #1527
  • Loading branch information
sogehige committed Nov 11, 2018
1 parent 6f36714 commit db2a643
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 32 deletions.
9 changes: 5 additions & 4 deletions public/pages/highlights/list.html
Expand Up @@ -15,8 +15,8 @@
<div v-if="filtered.length > 0" class="card" v-for="(item, index) of filtered" v-bind:class="{ 'mt-3': index !== 0 }">
<div class="card-body row p-0">
<div class="col-sm-11 pr-0">
<a class="btn btn-block btn-outline-dark border-0 h-100 text-left p-0" target="_blank" v-bind:href="item.url + '?t=' + timestampToString(item.timestamp)">
<img class="float-left pr-3" v-bind:src="generateThumbnail(item.thumbnail_url)">
<a class="btn btn-block btn-outline-dark border-0 h-100 text-left p-0" target="_blank" v-bind:href="'https://www.twitch.tv/videos/' + item.id + '?t=' + timestampToString(item.timestamp)">
<img class="float-left pr-3" v-bind:src="generateThumbnail(item.game)">
<div style="padding-top:0.8rem !important">
{{ item.title }}
<small class="d-block">
Expand Down Expand Up @@ -96,8 +96,9 @@
(value.seconds ? `${value.seconds}s` : '')
return string
},
generateThumbnail: function (url) {
return url.replace('%{width}', '133').replace('%{height}', '75')
generateThumbnail: function (game) {
const template = 'https://static-cdn.jtvnw.net/ttv-boxart/./%{game}-60x80.jpg'
return template.replace('%{game}', encodeURI(game))
},
deleteItem: function (id) {
this.socket.emit('delete', {_id: id}, () => {
Expand Down
5 changes: 2 additions & 3 deletions src/bot/api.js
Expand Up @@ -1235,8 +1235,7 @@ class API {
}

async createMarker () {
if (cluster.isWorker) throw new Error('API can run only on master')
const token = await global.oauth.settings.bot.accessToken
const token = global.oauth.settings.bot.accessToken
const cid = global.oauth.channelId

const url = 'https://api.twitch.tv/helix/streams/markers'
Expand All @@ -1263,7 +1262,7 @@ class API {
this.calls.bot.limit = request.headers['ratelimit-limit']
global.commons.processAll({ ns: 'api', fnc: 'setRateLimit', args: [ 'bot', request.headers['ratelimit-limit'], request.headers['ratelimit-remaining'], request.headers['ratelimit-reset'] ] })

if (global.panel && global.panel.io) global.panel.io.emit('api.stats', { timestamp: _.now(), call: 'createMarker', api: 'helix', endpoint: url, code: request.status, remaining: this.calls.bot.remaining })
if (global.panel && global.panel.io) global.panel.io.emit('api.stats', { timestamp: _.now(), call: 'createMarker', api: 'helix', endpoint: url, code: request.status, remaining: this.calls.bot.remaining, data: request.data })
} catch (e) {
if (e.errno === 'ECONNRESET' || e.errno === 'ECONNREFUSED' || e.errno === 'ETIMEDOUT') return this.createMarker()
global.log.error(`API: Marker was not created - ${e.message}`)
Expand Down
68 changes: 43 additions & 25 deletions src/bot/systems/highlights.js
Expand Up @@ -5,12 +5,14 @@ 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')
const System = require('./_interface')

const ERROR_STREAM_NOT_ONLINE = '1'
const ERROR_MISSING_TOKEN = '2'

/*
* !highlight <?description> - save highlight with optional description
Expand Down Expand Up @@ -53,33 +55,49 @@ class Highlights extends System {
}

async main (opts) {
if (cluster.isWorker) {
// as we are using API, go through master
if (process.send) process.send({ type: 'highlight', opts })
} else {
const when = await global.cache.when()

try {
if (_.isNil(when.online)) throw Error(ERROR_STREAM_NOT_ONLINE)

const token = await global.oauth.settings.bot.accessToken
if (token === '') return

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: 'title' }), 'value', 'n/a')
const when = await global.cache.when()
const token = global.oauth.settings.bot.accessToken
const cid = global.oauth.channelId
const url = `https://api.twitch.tv/helix/videos?user_id=${cid}&type=archive&first=1`

try {
if (_.isNil(when.online)) throw Error(ERROR_STREAM_NOT_ONLINE)
if (token === '' || cid === '') throw Error(ERROR_MISSING_TOKEN)

// 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 timestamp = moment.preciseDiff(moment().valueOf(), moment(global.api.streamStartedAt).valueOf(), true)
let highlight = {
id: request.data.data[0].id,
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: 'title' }), 'value', 'n/a'),
created_at: Date.now()
}

this.add(highlight, timestamp, opts.sender)
} catch (e) {
switch (e.message) {
case ERROR_STREAM_NOT_ONLINE:
global.commons.sendMessage(global.translate('highlights.offline'), opts.sender)
break
}
global.panel.io.emit('api.stats', { data: request.data, timestamp: _.now(), call: 'highlights', api: 'helix', endpoint: url, code: request.status, remaining: global.api.remainingAPICalls })

this.add(highlight, timestamp, opts.sender)
} catch (e) {
global.panel.io.emit('api.stats', { timestamp: _.now(), call: 'highlights', api: 'helix', endpoint: url, code: e.stack, remaining: global.api.remainingAPICalls })
switch (e.message) {
case ERROR_STREAM_NOT_ONLINE:
global.log.error('Cannot highlight - stream offline')
global.commons.sendMessage(global.translate('highlights.offline'), opts.sender)
break
case ERROR_MISSING_TOKEN:
global.log.error('Cannot highlight - missing token')
break
default:
global.log.error(e.stack)
}
}
}
Expand Down

0 comments on commit db2a643

Please sign in to comment.