Skip to content

Commit

Permalink
Merge pull request #2 from mwood77/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
mwood77 authored Aug 19, 2022
2 parents 1c55b29 + fc5d5be commit f917a29
Show file tree
Hide file tree
Showing 11 changed files with 312 additions and 660 deletions.
10 changes: 8 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
{
"version": "0.2.0",
"version": "0.3.0",
"configurations": [
{
"command": "npm run start-chat-logger",
"name": "run logger",
"request": "launch",
"type": "node-terminal"
}
},
{
"command": "npm run start-input-mapper",
"name": "run mapper",
"request": "launch",
"type": "node-terminal"
}
]
}
23 changes: 19 additions & 4 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,23 @@
* make sure your `LIVE_VIDEO_ID` and `API_KEY` are set in your `.env` file.
2. If prompted to generate new Auth token, do so. By default, this is stored at `Users/{YOUR-USER}/.super-secrets/`
<!--
https://developers.google.com/youtube/v3/quickstart/nodejs
## Generate random test data
```javascript
const cannedArray = ['LEFT', 'RIGHT', 'ENTER', 'UP', 'DOWN'];
const cannedAuthorArray = ['Aida Oksana 🔺️☆', 'K_A_N_G____ ', 'vaishali', '🌾🎶🎶', 'если не указано кому обращение значит этот вопрос ко всем'];
const inputStack = new Set();
const now = Date.now();
https://nodejs.org/en/docs/guides/backpressuring-in-streams/
-->
function randomPosition(maxPosition) {
return Math.floor(Math.random() * maxPosition * Math.random());
}
for (let i = 0; i <= 100; i++) {
const hashAuthor = 1491733810630004;
const hashMessage = 4646774944775204;
const time = new Date;
inputStack.add(hashAuthor * Math.random(100) + ':'+ hashMessage * Math.random(100) + ':' + time.getMilliseconds() + '=|=' + cannedArray[randomPosition(5)] + '=|='+ cannedAuthorArray[randomPosition(5)]);
}
console.log([...inputStack]);
```
84 changes: 51 additions & 33 deletions chat-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,33 @@
require('dotenv').config();
const fs = require('fs');
const readline = require('readline');
const robot = require("robotjs");
const { google } = require('googleapis');
const child_process = require('child_process');
const OAuth2 = google.auth.OAuth2;
const service = google.youtube('v3');
const { BehaviorSubject } = require('rxjs');
const { translateInput } = require('./input-mapper');

const SCOPES = ['https://www.googleapis.com/auth/youtube.readonly'];
let TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
process.env.USERPROFILE) + '/.super-secrets/';
const TOKEN_PATH = TOKEN_DIR + 'youtube-nodejs-quickstart.json';
const compiledSystemController = './build/system-controller.js';
const VIDEO_ID = process.env.LIVE_VIDEO_ID;
const API_KEY = process.env.API_KEY;

let authed;
const maximumDailyRequests = 10000;
const logFile = './resources/unfiltered.txt';
const stream = fs.createWriteStream(logFile);
const inputStack = new Set();
const chatInput$ = new BehaviorSubject();

let videoInformation = {
channel: undefined,
chatId: undefined,
videoTitle: undefined,
pollingInterval: 2000,
}

let lastElement = {
id: undefined,
previousLastPosition: 0,
currentLastPosition: 0,
};
Expand Down Expand Up @@ -153,18 +157,18 @@ function getVideoDetails(auth) {
return;
}
const videoDetails = response.data.items[0];
const channel = videoDetails.snippet.channelTitle
const chatId = videoDetails.liveStreamingDetails.activeLiveChatId;
const videoTitle = videoDetails.snippet.title
videoInformation.channel = videoDetails.snippet.channelTitle
videoInformation.chatId = videoDetails.liveStreamingDetails.activeLiveChatId;
videoInformation.videoTitle = videoDetails.snippet.title;

if (videoDetails === 0) {
console.error(`No video with id ${VIDEO_ID} was found.`)
} else {
console.log('\n\n======= Source =======\n Channel = %s\n Video = %s\n Live Chat Id = %s\n======================\n\n',
channel,
videoTitle,
chatId);
getLiveChat(chatId);
videoInformation.channel,
videoInformation.videoTitle,
videoInformation.chatId);
getLiveChat(videoInformation.chatId);
}
});
};
Expand All @@ -176,7 +180,7 @@ function getVideoDetails(auth) {
* @param liveChat the live chat session id of a live video.
*
*/
function getLiveChat(liveChat) {
function getLiveChat(liveChat, updateDelayInterval) {
service.liveChatMessages.list({
auth: authed,
key: API_KEY,
Expand All @@ -188,12 +192,19 @@ function getLiveChat(liveChat) {
return;
}
const liveChatDetails = response.data;
const delayInterval = liveChatDetails.pollingIntervalMillis <= 2100 ? liveChatDetails.pollingIntervalMillis + 700 : liveChatDetails.pollingIntervalMillis;
const nextPageToken = liveChatDetails.nextPageToken;
videoInformation.pollingInterval = delayInterval;

if (liveChatDetails === 0) {
console.error(`No chat with id ${liveChat} was found.`);
} else {
child_process.exec('run node ./build/system-controller.js');
beginRecursionLogging(liveChat, null, liveChatDetails.nextPageToken);
console.info('-> Current interval: ' + videoInformation.pollingInterval);

if (!updateDelayInterval) { // ensure recursion only begins on first call
if (liveChatDetails === 0) {
console.error(`No chat with id ${liveChat} was found.`);
} else {
child_process.exec('run node ./build/system-controller.js');
beginRecursionLogging(liveChat, delayInterval, nextPageToken);
}
}
});
};
Expand All @@ -210,7 +221,7 @@ function getPaginatedLiveChatAndAddChatsToInputStack(liveChat, nextPageToken) {
auth: authed,
key: API_KEY,
liveChatId: liveChat,
part: 'snippet',
part: 'snippet, authorDetails',
pageToken: nextPageToken
}, function(err, response) {
if (err) {
Expand All @@ -225,31 +236,37 @@ function getPaginatedLiveChatAndAddChatsToInputStack(liveChat, nextPageToken) {
} else {
repeat.items.forEach((element) => {
const snippet = element.snippet;
const hashAuthor = cyrb53(snippet.authorChannelId)
const hashMessage = cyrb53(snippet.textMessageDetails.messageText + snippet.authorChannelId)
inputStack.add(hashAuthor + ':'+ hashMessage + '=|=' + snippet.textMessageDetails.messageText, 'utf8');
const author = element.authorDetails ? element.authorDetails.displayName : null;
const time = snippet.publishedAt;
inputStack.add(JSON.stringify({
author: author,
time: time,
message: snippet.textMessageDetails.messageText,
}));
});
lastElement.previousLastPosition = lastElement.currentLastPosition;
lastElement.id = [...inputStack][[...inputStack].length - 1].split(':')[1].split('=|=')[0]; // isolate hashMessage
lastElement.currentLastPosition = [...inputStack].length - 1;

lastElement.previousLastPosition = lastElement.currentLastPosition <= 0 ? 0 : lastElement.currentLastPosition;
lastElement.currentLastPosition = [...inputStack].length - 1 >= 0 ? [...inputStack].length : 0;

chatInput$.next([...inputStack].slice(lastElement.previousLastPosition, lastElement.currentLastPosition)); // emit latest chats
}
});
};



function beginRecursionLogging(liveChatID, interval, nextPageToken) {
function beginRecursionLogging(liveChatID, delay, nextPageToken) {
(async function loop() {
for (let i = 0; i < maximumDailyRequests; i++) {
if (interval) {
await new Promise(resolve => setTimeout(resolve, interval));
if (delay) {
await new Promise(resolve => setTimeout(resolve, videoInformation.pollingInterval));
} else {
await new Promise(resolve => setTimeout(resolve, 9000));
}
console.info('======== refreshing data @ request number: %s ========', i + 1);
if (i & 100 === 0 ) console.info('==== chunk: %s ====', i + 1);
getPaginatedLiveChatAndAddChatsToInputStack(liveChatID, nextPageToken);

// Update polling interval
if (i % 20 === 0) getLiveChat(videoInformation.chatId, true);
}
})();
};
Expand All @@ -260,10 +277,11 @@ function beginRecursionLogging(liveChatID, interval, nextPageToken) {
* @param {Array} input BehaviourSubject's value
*/
function actionAvatar(input) {
console.log(input)
input.forEach(el => {
robot.typeString(el.split('=|=')[1])
robot.keyTap('enter');
const content = JSON.parse(el)
const key = content.message;
const author = content.author;
translateInput(key, author);
});
};

Expand Down
Loading

0 comments on commit f917a29

Please sign in to comment.