added getNumViewers, not implemented for twitch yet

master
vampirefrog 2 years ago
parent 63e7cfbce8
commit 2f0aad90de

@ -12,6 +12,7 @@ class Channel extends EventEmitter {
sendChat() { throw new Error('Please implement '+arguments.callee.name+'()!'); }
isJoined() { throw new Error('Please implement '+arguments.callee.name+'()!'); }
getNumViewers() { throw new Error('Please implement '+arguments.callee.name+'()!'); }
}
if(typeof process === 'object') {

@ -223,6 +223,9 @@ class DLiveApi extends EventEmitter {
avatar
partnerStatus
myChatBadges
livestream {
watchingCount
}
}
}
`

@ -368,10 +368,33 @@ class DLiveChannel extends Channel {
}
}
isJoined() {
return this.state == 'joined';
}
getNumViewers() {
return new Promise((resolve, reject) => {
this.chat.api.post({
operationName: 'LivestreamPageRefetch',
variables: {
displayname: this.username
},
query: `
query LivestreamPageRefetch(
$displayname: String!
) {
userByDisplayName(displayname: $displayname) {
livestream {
watchingCount
}
}
}
`
}).then((data) => {
resolve({ viewers: data.userByDisplayName.livestream.watchingCount, platform: this.chat.getPlatformName() });
}, reject);
});
}
}
if(typeof process === 'object') {

@ -15,6 +15,8 @@ class TrovoChannel extends Channel {
this.channelName = channelData.channelName;
this.channelID = channelData.channelID;
this.viewerCount = null;
this._loadGiftData();
this._createChannelWS();
}
@ -339,6 +341,56 @@ class TrovoChannel extends Channel {
});
this.ws.connect();
this.pubWs = new TrovoPubSubSocket(this.channelID);
this.pubWs.on('MESSAGE', (data) => {
if(data && data.data && data.data.message) {
let parsed = JSON.parse(data.data.message);
// {
// type: 'viewcount',
// viewers: 7,
// users: [
// {
// uid: 101488110,
// nickName: 'comfyfren',
// avatar: '52kqybqaaaaaasvqr4tka4c7cy.jpeg?t=7',
// spellPoint: 1000
// }
// ],
// online_amount: { '1': 7, '2': 1, '3': 0 },
// effects: {}
// }
// {
// type: 'channel_console',
// explicit_module: {
// baseInfo: {
// category: 1,
// name: 'Treasure Box',
// icon: 'https://static.trovo.live/imgupload/interact_console/20201120_9xnoxkwvv63x.png',
// explicitIcon: 'https://static.trovo.live/imgupload/interact_console/20201120_gyx86oz4xaj3x.png',
// consoleID: 9
// },
// extInfo: {
// mask: 2,
// extData: '{"unleashed":false,"winners":null,"status_names":{"0":"In Progress","1":"Miss it","2":"Miss it","3":"Win!","4":"Drawing"},"
// loop_type":0}'
// }
// }
// }
// { type: 'livestop', time: 1620853599, rand: 0 }
if(parsed.type == 'chat_deletion') {
this.emit('message', {
type: 'delete',
ids: parsed.msgIDs
});
} else if(parsed.type == 'viewcount') {
this.viewerCount = parsed.viewers;
}
}
});
this.pubWs.connect();
}
_parseEmotes(str) {
@ -406,7 +458,7 @@ class TrovoChannel extends Channel {
_handleChatMessage(message) {
this.emit('message', {
type: 'chat',
id: message.id,
id: message.identifier,
original: message,
text: message.content,
spans: this._parseEmotes(message.content),
@ -434,7 +486,7 @@ class TrovoChannel extends Channel {
} else {
console.log('unrecognized gift id', giftId);
}
amount = data.content.num;
amount = parseInt(data.content.num)||1;
} else {
message = data.content;
giftId = data['magicChat.magicChatGiftID'];
@ -443,7 +495,6 @@ class TrovoChannel extends Channel {
if(giftData) {
currency = giftData.currency;
value = giftData.value;
amount = giftData.amount;
giftName = giftData.giftName;
icon = giftData.icon;
}
@ -536,6 +587,12 @@ class TrovoChannel extends Channel {
sendChat(message) {
return this.chat.client.SendMessage(message, this.channelID);
}
getNumViewers() {
return new Promise((resolve, reject) => {
resolve({ viewers: this.viewerCount, platform: this.chat.getPlatformName() });
});
}
}
if(typeof process === 'object') {

@ -0,0 +1,100 @@
if(typeof process === 'object') {
var WebSocket = require('isomorphic-ws');
var EventEmitter = require('events');
var TrovoApollo = require('./trovo.js/lib/api/TrovoApollo');
}
class TrovoPubSubSocket extends EventEmitter {
constructor(channelID, settings) {
super();
this.channelID = channelID;
settings = settings || {};
this.wsUrl = settings && settings.wsUrl || 'wss://pubsub.trovo.live/sub';
this.verbose = settings.verbose || false;
this.reconnectDelay = settings.reconnectDelay || 2000;
this.state = 'none';
this.trovoApollo = new TrovoApollo();
this.nonces = {};
this.pingTimeout = 45000;
}
async connect() {
if(this.webSocket) {
this.webSocket.removeAllListeners('message');
this.state = 'none';
this.webSocket = null;
}
this.emit('verbose', 'Opening WebSocket ' + this.wsUrl);
this.webSocket = new WebSocket(this.wsUrl);
this.webSocket.once('open', () => {
this.emit('verbose', 'websocket open');
this.trovoApollo.GetPubSubToken({}).then((token) => {
this.send('AUTH', { token: token.getPubSubToken.token }).then((response) => {
this.emit('open');
this.send('LISTEN', {"topics":["emote-event-notify"]}).then((response) => { console.log('listen emote event notify', response); });
this.send('LISTEN', {"topics":["channel-event-by-id."+this.channelID]});
this.send('LISTEN', {"topics":["channel-change-notify-by-id."+this.channelID]});
this.sendPing();
});
});
});
this.webSocket.once('error', (err) => {
this.emit('verbose', 'websocket error: ' + err.toString());
});
this.webSocket.on('close', (err, message) => {
this.emit('verbose', 'websocket close: ' + err + ' ' + message);
setTimeout(() => { this.connect(); }, this.reconnectDelay);
});
this.webSocket.on('message', (text) => {
var data = JSON.parse(text);
if(data.type == 'RESPONSE' && data.nonce) {
if(this.nonces && this.nonces[data.nonce]) {
this.nonces[data.nonce].resolve(data.data);
delete this.nonces[data.nonce];
}
} else if(data.type == 'PONG') {
setTimeout(() => { this.sendPing(); }, this.pingTimeout)
}
this.emit(data.type, data);
});
}
sendPing() {
this.send('PING');
}
send(type, data) {
if(this.webSocket && this.webSocket.readyState == 1) {
var nonce = type + '_' + Date.now();
var message = {
type: type,
nonce: nonce,
data: data
};
var str = JSON.stringify(message);
this.webSocket.send(str);
this.emit('verbose', 'SEND ' + str);
return new Promise((resolve, reject) => {
this.nonces[nonce] = { resolve: resolve, reject: reject };
});
}
}
isConnected() {
return this.state === 'connected';
}
}
if(typeof process === 'object') {
module.exports = TrovoPubSubSocket;
}

@ -206,6 +206,12 @@ class TwitchChannel extends Channel {
this.emit('verbose', "unhandled command " + JSON.stringify(msg));
}
}
getNumViewers() {
return new Promise((resolve, reject) => {
resolve(null);
});
}
}
if(typeof process === 'object') {

Loading…
Cancel
Save