Skip to content

Commit

Permalink
systems/highlights - new highlight system #195 #196
Browse files Browse the repository at this point in the history
  • Loading branch information
sogehige committed May 24, 2017
1 parent 8344ca0 commit 7bfd5b6
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 9 deletions.
5 changes: 3 additions & 2 deletions config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
username = soge__bot
; get oauth password http://oauth.sogehige.tv
; owners = owner1,owner2
owners = sogehige
channel = sogehige
owners = soge__
channel = soge__
password =
clientId = 1wjn1i3792t71tl90fmyvd0zl6ri2vg
; set your bot color - Blue, BlueViolet, CadetBlue, Chocolate, Coral, DodgerBlue, Firebrick, GoldenRod, Green, HotPink, OrangeRed, Red, SeaGreen, SpringGreen, YellowGreen
Expand All @@ -30,6 +30,7 @@ raffles = true
queue = true
cooldown = true
gambling = true
highlights = true

[bot]
debug = false
138 changes: 138 additions & 0 deletions libs/systems/highlights.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
'use strict'

// 3rdparty libraries
var _ = require('lodash')
var moment = require('moment')
require('moment-precise-range-plugin')
// bot libraries
var constants = require('../constants')
var log = global.log

const ERROR_STREAM_NOT_ONLINE = '1'

/*
* !highlight <?description> - save highlight with optional description
* !highlight list - get list of highlights in current running or latest stream
*/

function Highlights () {
this.highlights = []
this.cached = {
id: null,
created_at: null
}

if (global.commons.isSystemEnabled(this)) {
global.parser.register(this, '!highlight list', this.list, constants.OWNER_ONLY)
global.parser.register(this, '!highlight', this.highlight, constants.OWNER_ONLY)

global.panel.addMenu({category: 'manage', name: 'highlights', id: 'highlights'})
global.panel.socketListening(this, 'highlight.save', this.saveHighlight)
global.panel.socketListening(this, 'highlight.get', this.sendHighlight)

global.watcher.watch(this, 'highlights', this._save)
this._update(this)
}
}

Highlights.prototype._update = function (self) {
global.botDB.findOne({ _id: 'highlights' }, function (err, item) {
if (err) return log.error(err, { fnc: 'Highlights.prototype._update' })
if (_.isNull(item)) return

self.highlights = item.highlights
})
}

Highlights.prototype._save = function (self) {
var highlights = { highlights: self.highlights }
global.botDB.update({ _id: 'highlights' }, { $set: highlights }, { upsert: true })
}

Highlights.prototype.highlight = function (self, sender, description) {
description = description.trim().length > 0 ? description : null

let highlight = {}

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

let timestamp = moment.preciseDiff(global.twitch.when.online, moment(), true)
timestamp = { hours: timestamp.hours, minutes: timestamp.minutes, seconds: timestamp.seconds }
highlight.stream_id = moment(global.twitch.when.online).format('X')
highlight.stream = global.twitch.when.online
highlight.timestamp = timestamp
highlight.description = description
highlight.title = global.twitch.currentStatus
highlight.game = global.twitch.currentGame
highlight.created_at = moment().format('X')

if (self.cached.created_at === global.twitch.when.online && !_.isNil(self.cached.id) && !_.isNil(self.cached.created_at)) {
highlight.video_id = self.cached.id
self.add(self, highlight, timestamp, sender)
} else {
// we need to load video id
let options = {
url: 'https://api.twitch.tv/kraken/channels/' + global.channelId + '/videos?broadcast_type=archive&limit=1',
headers: {
Accept: 'application/vnd.twitchtv.v5+json',
'Client-ID': global.configuration.get().twitch.clientId
}
}
global.client.api(options, function (err, res, body) {
if (err) {
global.log.error(err, { fnc: 'Highlights#1' })
return
}
const video = body.videos[0]
self.cached.id = video._id
highlight.video_id = self.cached.id
self.add(self, highlight, timestamp, sender)
})
}
} catch (e) {
switch (e.message) {
case ERROR_STREAM_NOT_ONLINE:
global.commons.sendMessage(global.translate('highlights.offline'), sender)
break
}
}
}

Highlights.prototype.saveHighlight = function (self, socket) {
self.highlight(self, null, '')
}
Highlights.prototype.sendHighlight = function (self, socket) {
socket.emit('highlight.list', _.orderBy(self.highlights, 'created_at', 'desc'))
}

Highlights.prototype.add = function (self, highlight, timestamp, sender) {
global.commons.sendMessage(global.translate(_.isNil(highlight.description) ? 'highlights.saved.no-description' : 'highlights.saved.description')
.replace('(description)', highlight.description)
.replace('(hours)', (timestamp.hours < 10) ? '0' + timestamp.hours : timestamp.hours)
.replace('(minutes)', (timestamp.minutes < 10) ? '0' + timestamp.minutes : timestamp.minutes)
.replace('(seconds)', (timestamp.seconds < 10) ? '0' + timestamp.seconds : timestamp.seconds), sender)

self.highlights.push(highlight)
}

Highlights.prototype.list = function (self, sender) {
const sortedHighlights = _.orderBy(self.highlights, 'id', 'desc')
const latestStreamId = sortedHighlights.length > 0 ? sortedHighlights[0].id : null

if (_.isNull(latestStreamId)) {
global.commons.sendMessage(global.translate('highlights.list.empty'), sender)
return
}
let highlights = _.filter(self.highlights, function (o) { return o.id === latestStreamId })
let list = []

for (let highlight of highlights) {
list.push(highlight.timestamp.hours + 'h' +
highlight.timestamp.minutes + 'm' +
highlight.timestamp.seconds + 's')
}
global.commons.sendMessage(global.translate(list.length > 0 ? 'highlights.list.items' : 'highlights.list.empty')
.replace('(items)', list.join(', ')), sender)
}
module.exports = new Highlights()
16 changes: 15 additions & 1 deletion locales/cs.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
}
},
"webpanel": {
"video_id": "ID Videa",
"highlights": "Highlighty",
"cooldown-quiet-header": "Zobrazí cooldown zprávu",
"cooldown-quiet-toggle-no": "Upozornit",
"cooldown-quiet-toggle-yes": "Neupozornit",
Expand Down Expand Up @@ -273,7 +275,8 @@
"dark": "tmavý theme",
"gambling": "Gambling",
"seppukuTimeout": "Timeout pro !sepukku",
"rouletteTimeout": "Timeout pro !roulette"
"rouletteTimeout": "Timeout pro !roulette",
"click-to-highlight": "highlight"
},
"raffle": {
"announceInterval": "Otevrene raffle budou oznamovany kazdych (value) minut",
Expand Down Expand Up @@ -789,5 +792,16 @@
"success": "(sender), (username) ma nyni data (merged-username)",
"noID": "(sender), pro toto uzivatelske jmeno neexistuje ID, proto nelze zmenit",
"noUsernameToMerge": "(sender), nebylo nalezeno jine uzivatelske jmeno"
},
"highlights": {
"saved": {
"description": "(sender), highlight byl ulozen pro (hours)h(minutes)m(seconds)s jako `(description)`",
"no-description": "(sender), highlight byl ulozen pro (hours)h(minutes)m(seconds)s"
},
"list": {
"items": "(sender), seznam ulozenych highlightu pro posledni stream: (items)",
"empty": "(sender), zande highlighty nebyly ulozeny"
},
"offline": "(sender), nelze ulozit highlight, stream je offline"
}
}
16 changes: 15 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
}
},
"webpanel": {
"video_id": "Video ID",
"highlights": "Highlights",
"cooldown-quiet-header": "Show cooldown message",
"cooldown-quiet-toggle-no": "Notify",
"cooldown-quiet-toggle-yes": "Won't notify",
Expand Down Expand Up @@ -273,7 +275,8 @@
"dark": "dark theme",
"gambling": "Gambling",
"seppukuTimeout": "Timeout for !sepukku",
"rouletteTimeout": "Timeout for !roulette"
"rouletteTimeout": "Timeout for !roulette",
"click-to-highlight": "highlight"
},
"raffle": {
"announceInterval": "Opened raffles will be announced every (value) minute",
Expand Down Expand Up @@ -789,5 +792,16 @@
"success": "(sender), (username) have now data of (merged-username)",
"noID": "(sender), specified username doesn't have ID and cannot be merged",
"noUsernameToMerge": "(sender), no other username was found to merge"
},
"highlights": {
"saved": {
"description": "(sender), highlight was saved for (hours)h(minutes)m(seconds)s as `(description)`",
"no-description": "(sender), highlight was saved for (hours)h(minutes)m(seconds)s"
},
"list": {
"items": "(sender), list of saved highlights for latest stream: (items)",
"empty": "(sender), no highlights were saved"
},
"offline": "(sender), cannot save highlight, stream is offline"
}
}
4 changes: 4 additions & 0 deletions public/dist/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -695,3 +695,7 @@ a.soundboard-list-group-item span {
.action-danger > a {
color: #a94442 !important;
}

th:first-letter {
text-transform: uppercase;
}
10 changes: 7 additions & 3 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ <h4 class="modal-title"><i data-lang="change-title"></i> <small id="titleForGame
<h2><i data-lang="game"></i> <small data-lang="click-to-change"></small></h2>
<span class="data" id="game" data-lang="not-available"></span>
</div>
<div class="stream-info col-xs-6 col-sm-4 col-md-2">
<h2 data-lang="uptime"></h2>
<div class="stream-info col-xs-6 col-sm-4 col-md-2" onclick="saveHighlight()">
<h2><i data-lang="uptime"></i> <small data-lang="click-to-highlight"></small></h2>
<span class="data" id="uptime">--:--:--</span>
<span class="stats">&nbsp;</span>
</div>
Expand Down Expand Up @@ -271,7 +271,6 @@ <h2 data-lang="status"></h2>
})
})


console.debug('EMIT [getSystems]')
socket.emit('getSystems')
socket.on('systems', function(data) {
Expand Down Expand Up @@ -299,6 +298,11 @@ <h2 data-lang="status"></h2>
switchPercentageShow(false)
})

var saveHighlight = function () {
console.debug('EMIT [highlight.save]')
socket.emit('highlight.save')

}
var switchTheme = function () {
var btn = $('#theme-btn')
if (btn.text() === translations['dark']) {
Expand Down
47 changes: 47 additions & 0 deletions public/pages/highlights.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<div class="widget">
<h2 class="title" data-lang="highlights"></h2>
<table class="table table-striped table-responsive table-condensed">
<thead>
<tr>
<th data-lang="video_id"></th>
<th data-lang="game"></th>
<th data-lang="stream-title"></th>
<th data-lang="description"></th>
<th data-lang="time"></th>
<th></th>
</tr>
</thead>
<tbody id="highlights"></tbody>
</table>
</div>
</div>

<script>
var highlights = {
update: function (list) {
$("#highlights").empty()
_.each(list, function(item, index) {
let time = '(hours)h(minutes)m(seconds)s'
.replace('(hours)', (item.timestamp.hours < 10) ? '0' + item.timestamp.hours : item.timestamp.hours)
.replace('(minutes)', (item.timestamp.minutes < 10) ? '0' + item.timestamp.minutes : item.timestamp.minutes)
.replace('(seconds)', (item.timestamp.seconds < 10) ? '0' + item.timestamp.seconds : item.timestamp.seconds)
.replace('00h', '').replace('00m', '') // also remove unnecessary hours and minutes if they are 0
$("#highlights").append('<tr>' +
'<td><a href="http://twitch.tv/videos/' + item.video_id + '?t=' + time + '">' + item.video_id + '</a></td>' +
'<td>' + item.game + '</td>' +
'<td>' + item.title + '</td>' +
'<td>' + (_.isNil(item.description) ? '' : item.description) + '</td>' +
'<td>' + time + '</td>' +
'<td class="tableMenu">' +
'</td>' +
'</tr>');
})
}
}

socket.emit('highlight.get');
socket.once('highlight.list', function(list) {
highlights.update(list)
});
</script>
2 changes: 1 addition & 1 deletion test/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ global.timeouts = []
global.log.exitOnError = false

/* users */
global.ownerUser = {username: 'sogehige'}
global.ownerUser = {username: 'soge__'}
2 changes: 1 addition & 1 deletion test/moderation.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global describe it after before */

var expect = require('chai').expect
var testUser = {username: 'sogehige'}
var testUser = ownerUser
var testUser2 = {username: 'soge'}

require('./general')
Expand Down

0 comments on commit 7bfd5b6

Please sign in to comment.