From ff044f42167543daf584f362f9638b5b48e1d545 Mon Sep 17 00:00:00 2001 From: LuanRT Date: Fri, 7 Jan 2022 18:45:30 -0300 Subject: [PATCH] fix: error polling livechat due to dislikes --- lib/Livechat.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/Livechat.js b/lib/Livechat.js index d362a360..d23af2b4 100644 --- a/lib/Livechat.js +++ b/lib/Livechat.js @@ -25,15 +25,17 @@ class Livechat extends EventEmitter { async #poll() { if (!this.running) return; - const livechat = await Actions.livechat(this, 'livechat/get_live_chat', { ctoken: this.ctoken }); - if (!livechat.success) throw new Error(`Error polling livechat: ${livechat.message}`); + const livechat = await Actions.livechat(this.session, 'live_chat/get_live_chat', { ctoken: this.ctoken }); + if (!livechat.success) { + this.emit('error', { message: `Failed polling livechat: ${livechat.message}. Retrying...` }); + return await this.#poll(); + } const continuation_contents = livechat.data.continuationContents; const action_group = continuation_contents.liveChatContinuation.actions; this.#enqueueActionGroup(action_group); - // Why don't we just emit the message directly? Well, enqueueing the messages is necessary so they are not emitted in a “messy” way, funny enough that's exactly how YouTube does it in its live chat js script. - this.message_queue.forEach((message, index) => { + this.message_queue.forEach((message) => { if (this.id_cache.includes(message.id)) return; setTimeout(() => this.emit('chat-update', message), message.timestamp / 1000 - new Date().getTime()); this.id_cache.push(message.id); @@ -44,25 +46,28 @@ class Livechat extends EventEmitter { const data = { video_id: this.video_id }; if (this.metadata_ctoken) data.continuation = this.metadata_ctoken; - const updated_metadata = await Actions.livechat(this, 'updated_metadata', data); - if (!updated_metadata.success) throw new Error(`Error polling livestream metadata: ${updated_metadata.message}`); + const updated_metadata = await Actions.livechat(this.session, 'updated_metadata', data); + if (!updated_metadata.success) { + this.emit('error', { message: `Failed polling livechat metadata: ${livechat.message}.` }); + } + this.metadata_ctoken = updated_metadata.data.continuation.timedContinuationData.continuation; const metadata = updated_metadata.data.actions; this.emit('update-metadata', { likes: metadata[1].updateToggleButtonTextAction.defaultText.simpleText, - dislikes: metadata[2].updateToggleButtonTextAction.defaultText.simpleText, view_count: { simple_text: metadata[0].updateViewershipAction.viewCount.videoViewCountRenderer.viewCount.simpleText, short_view_count: metadata[0].updateViewershipAction.viewCount.videoViewCountRenderer.extraShortViewCount.simpleText } }); - this.livechat_poller = setTimeout(async () => await this.#poll(), this.poll_intervals_ms); + this.livechat_poller = setTimeout(async () => await this.#poll()); } #enqueueActionGroup(group) { group.forEach((action) => { + console.log(action); if (!action.addChatItemAction) return; //TODO: handle different action types const message_content = action.addChatItemAction.item.liveChatTextMessageRenderer; if (!message_content) return;