Skip to content

Commit

Permalink
api: add proxy for calls
Browse files Browse the repository at this point in the history
This will help to debug API calls with new loglevel environment variable
  • Loading branch information
sogehige committed Oct 1, 2018
1 parent 82fd040 commit c1224f1
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 101 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -50,6 +50,7 @@
"sortablejs": "1.7.0",
"source-map-support": "0.5.9",
"spotify-web-api-node": "4.0.0",
"stacktrace-parser": "0.1.4",
"terminal-kit": "1.26.2",
"twitch-emoticons": "http://deps.sogebot.xyz/twitch-emoticons-2.1.0.tgz",
"twitch-js": "2.0.0-beta.19",
Expand Down
176 changes: 103 additions & 73 deletions src/bot/api.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/bot/events.js
Expand Up @@ -198,7 +198,7 @@ class Events {
}

async fireStartCommercial (operation, attributes) {
const cid = await global.oauth.settings._.channelId
const cid = global.oauth.channelId
const url = `https://api.twitch.tv/kraken/channels/${cid}/commercial`

const token = await global.oauth.settings.bot.accessToken
Expand Down
8 changes: 4 additions & 4 deletions src/bot/message.js
Expand Up @@ -448,8 +448,8 @@ class Message {
}
})
// save remaining api calls
global.api.remainingAPICalls = request.headers['ratelimit-remaining']
global.api.refreshAPICalls = request.headers['ratelimit-reset']
global.api.calls.bot.remaining = request.headers['ratelimit-remaining']
global.api.calls.bot.refresh = request.headers['ratelimit-reset']
return request.data.data[0].title
} catch (e) { return 'n/a' } // return nothing on error
},
Expand All @@ -473,8 +473,8 @@ class Message {
}
})
// save remaining api calls
global.api.remainingAPICalls = request.headers['ratelimit-remaining']
global.api.refreshAPICalls = request.headers['ratelimit-reset']
global.api.calls.bot.remaining = request.headers['ratelimit-remaining']
global.api.calls.bot.refresh = request.headers['ratelimit-reset']
return request.data.data[0].viewer_count
} catch (e) { return '0' } // return nothing on error
}
Expand Down
10 changes: 5 additions & 5 deletions src/bot/oauth.js
Expand Up @@ -11,6 +11,7 @@ const constants = require('./constants')
class OAuth extends Core {
timeouts: Object = {}
currentChannel: string = ''
channelId: string = ''

constructor () {
const settings = {
Expand All @@ -19,8 +20,7 @@ class OAuth extends Core {
bot: '',
clientId: '',
botId: '',
broadcasterId: '',
channelId: ''
broadcasterId: ''
},
general: {
channel: '',
Expand Down Expand Up @@ -132,13 +132,13 @@ class OAuth extends Core {
this.currentChannel = channel
const cid = await global.users.getIdFromTwitch(channel)
if (typeof cid !== 'undefined') {
this.settings._.channelId = cid
this.channelId = cid
global.log.info('Channel ID set to ' + cid)
global.tmi.reconnect('bot')
global.tmi.reconnect('broadcaster')
} else {
global.log.error(`Cannot get channel ID of ${channel} - waiting ${Number(global.api.refreshAPICalls - (Date.now() / 1000)).toFixed(2)}s`)
timeout = (global.api.refreshAPICalls - (Date.now() / 1000)) * 1000
global.log.error(`Cannot get channel ID of ${channel} - waiting ${Number(global.api.calls.bot.refresh - (Date.now() / 1000)).toFixed(2)}s`)
timeout = (global.api.calls.bot.refresh - (Date.now() / 1000)) * 1000
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/bot/systems/commercial.js
Expand Up @@ -41,7 +41,7 @@ class Commercial extends System {
return
}

const cid = await global.oauth.settings._.channelId
const cid = global.oauth.channelId
// check if duration is correct (30, 60, 90, 120, 150, 180)
if (_.includes([30, 60, 90, 120, 150, 180], commercial.duration)) {
const url = `https://api.twitch.tv/kraken/channels/${cid}/commercial`
Expand Down
16 changes: 8 additions & 8 deletions src/bot/users.js
Expand Up @@ -304,19 +304,19 @@ class Users extends Core {

// save remaining api calls
// $FlowFixMe error with flow on request.headers
global.api.remainingAPICalls = request.headers['ratelimit-remaining']
global.api.calls.bot.remaining = request.headers['ratelimit-remaining']
// $FlowFixMe error with flow on request.headers
global.api.refreshAPICalls = request.headers['ratelimit-reset']
global.api.calls.bot.refresh = request.headers['ratelimit-reset']

global.panel.io.emit('api.stats', { data: request.data, timestamp: _.now(), call: 'getIdFromTwitch', api: 'helix', endpoint: url, code: request.status, remaining: global.api.remainingAPICalls })
global.panel.io.emit('api.stats', { data: request.data, timestamp: _.now(), call: 'getIdFromTwitch', api: 'helix', endpoint: url, code: request.status, remaining: global.api.calls.bot.remaining })

return request.data.data[0].id
} catch (e) {
if (e.response.status === 429) {
global.api.remainingAPICalls = 0
global.api.refreshAPICalls = e.response.headers['ratelimit-reset']
if (typeof e.response !== 'undefined' && e.response.status === 429) {
global.api.calls.bot.remaining = 0
global.api.calls.bot.refresh = e.response.headers['ratelimit-reset']
}
global.panel.io.emit('api.stats', { data: {}, timestamp: _.now(), call: 'getIdFromTwitch', api: 'helix', endpoint: url, code: e.stack, remaining: global.api.remainingAPICalls })
global.panel.io.emit('api.stats', { data: {}, timestamp: _.now(), call: 'getIdFromTwitch', api: 'helix', endpoint: url, code: e.stack, remaining: global.api.calls.bot.remaining })
}
}

Expand Down Expand Up @@ -467,7 +467,7 @@ class Users extends Core {
})
socket.on('followedAt.viewer', async (id, cb) => {
try {
const cid = await global.oauth.settings._.channelId
const cid = global.oauth.channelId
const url = `https://api.twitch.tv/helix/users/follows?from_id=${id}&to_id=${cid}`

const token = await global.oauth.settings.bot.accessToken
Expand Down
18 changes: 9 additions & 9 deletions src/bot/webhooks.js
Expand Up @@ -39,7 +39,7 @@ class Webhooks {
async subscribe (type) {
clearTimeout(this.timeouts[`subscribe-${type}`])

const cid = await global.oauth.settings._.channelId
const cid = global.oauth.channelId
const clientId = await global.oauth.settings._.clientId
if (cid === '' || clientId === '') {
this.timeouts[`subscribe-${type}`] = setTimeout(() => this.subscribe(type), 1000)
Expand Down Expand Up @@ -97,7 +97,7 @@ class Webhooks {
}

async event (aEvent, res) {
const cid = await global.oauth.settings._.channelId
const cid = global.oauth.channelId

// somehow stream doesn't have a topic
if (_.get(aEvent, 'topic', null) === `https://api.twitch.tv/helix/users/follows?to_id=${cid}`) this.follower(aEvent) // follow
Expand All @@ -107,7 +107,7 @@ class Webhooks {
}

async challenge (req, res) {
const cid = await global.oauth.settings._.channelId
const cid = global.oauth.channelId
// set webhooks enabled
switch (req.query['hub.topic']) {
case `https://api.twitch.tv/helix/users/follows?to_id=${cid}`:
Expand All @@ -123,7 +123,7 @@ class Webhooks {
}

async follower (aEvent) {
const cid = await global.oauth.settings._.channelId
const cid = global.oauth.channelId
if (_.isEmpty(cid)) setTimeout(() => this.follower(aEvent), 10) // wait until channelId is set
if (parseInt(aEvent.data.to_id, 10) !== parseInt(cid, 10)) return
const fid = aEvent.data.from_id
Expand Down Expand Up @@ -151,10 +151,10 @@ class Webhooks {
})

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

global.panel.io.emit('api.stats', { data: request.data, timestamp: _.now(), call: 'webhooks.follower', api: 'helix', endpoint: url, code: request.status, remaining: global.api.remainingAPICalls })
global.panel.io.emit('api.stats', { data: request.data, timestamp: _.now(), call: 'webhooks.follower', api: 'helix', endpoint: url, code: request.status, remaining: global.api.calls.bot.remaining })

if (!await global.commons.isBot(request.data.data[0].login)) {
global.overlays.eventlist.add({
Expand All @@ -169,7 +169,7 @@ class Webhooks {
global.events.fire('follow', { username: request.data.data[0].login, webhooks: true })
}
} catch (e) {
global.panel.io.emit('api.stats', { data: request.data, timestamp: _.now(), call: 'webhooks.follower', api: 'helix', endpoint: url, code: e.stack, remaining: global.api.remainingAPICalls })
global.panel.io.emit('api.stats', { data: request.data, timestamp: _.now(), call: 'webhooks.follower', api: 'helix', endpoint: url, code: e.stack, remaining: global.api.calls.bot.remaining })
}
} else {
if (!_.get(user, 'is.follower', false) && _.now() - _.get(user, 'time.follow', 0) > 60000 * 60) {
Expand Down Expand Up @@ -211,7 +211,7 @@ class Webhooks {
}
*/
async stream (aEvent) {
const cid = await global.oauth.settings._.channelId
const cid = global.oauth.channelId
if (cid === '') setTimeout(() => this.stream(aEvent), 1000) // wait until channelId is set

// stream is online
Expand Down

0 comments on commit c1224f1

Please sign in to comment.