Skip to content
Permalink
Browse files

Rewriting almost from scratch

  • Loading branch information...
lithiumjs committed Apr 2, 2019
1 parent 6678175 commit 0ce41b3d4e5e716f882eb6fab3cc8b95a963b069
Showing with 270 additions and 243 deletions.
  1. +34 −8 README.md
  2. +24 −0 config.json
  3. +161 −137 index.js
  4. +0 −98 json.js
  5. +51 −0 util.js
@@ -1,11 +1,37 @@
# bfb-votes-collector

~~**WARNING**: Very slow! Will fix in not-so-distant future.~~ **Fixed!**
# BFB Votes Collector

## How to run:

* Install Node.js (if haven't done so)
* Run cmd (or linux terminal) in this folder
* ~~Type `npm install`~~ It now relies on my comments getter so no need to do that.
* Type `node index.js`
* Just wait
1. Install Node.js (if haven't done so)
2. Run cmd (or linux terminal) in this folder
3. ~~Type `npm install`~~ It now relies on my comments getter so no need to do that.
4. Type `node index.js`
5. Just wait

## Changeable things:

* `config.json`
* `videoID`: YouTube video ID (goes after `https://www.youtube.com/watch?v=`)
* `names`: Names of contestants
* `colors`: Color codes for corresponding contestants
* `deadlineAfter`: Number of seconds since the video has been uploaded, when the votes are valid

## List of colors

* `black`
* `red`
* `green`
* `yellow`
* `blue`
* `magenta`
* `cyan`
* `light_gray`
* `dark_gray`
* `light_red`
* `light_green`
* `light_yellow`
* `light_blue`
* `light_magenta`
* `light_cyan`
* `white`
* `default` (Terminal's default foreground/background color)
@@ -0,0 +1,24 @@
{
"videoID": "2EMeuBFr0_Y",
"names": [
"Black Square",
"Organic Fruit and Grain Bar",
"Incense Holder",
"Loren",
"Corny",
"Paintball",
"Graffiti",
"Microphone"
],
"colors": [
"black",
"magenta",
"green",
"white",
"yellow",
"red",
"light_yellow",
"light_gray"
],
"deadlineAfter": 172800
}
298 index.js
@@ -1,149 +1,173 @@
var Getter = require("./getter.js");
var VIDEO_ID = 'SDaS5VNbVOo';
var getter = Getter(VIDEO_ID);
var comments = 0;
var votes = {
a: 0,
b: 0,
c: 0,
d: 0,
e: 0,
f: 0,
g: 0,
h: 0
};
var totalvotes = 0;
var shinycowards = 0;
var shinline = 0;
var deadlinevotes = 0;
var commentors = {};
var thecowards = {};
var contestants = {
a: "Tennis Ball",
b: "Golf Ball",
c: "Basketball",
d: "Grassy",
e: "TV",
f: "8-ball",
g: "Blocky",
h: "Robot Flower"
const alphabet = "abcdefghijklmnopqrstuvwxyz".split(""); // change this string if you need a different alphabet

console.log("Loading...");

const config = require("./config.json");
const Getter = require("./getter.js");
const util = require("./util.js");

const fg = util.colors.fg;
const bg = util.colors.bg;

const stats = {
commentCount: 0,
votes: {},
shinyCowards: 0,
votesAfterDeadline: 0,
commentors: {},
cowardVotes: {},
videoStats: {},
startedAt: Date.now()
};
var colors = {
a: ["\033[43m", "\033[33m"],
b: ["\033[47m", "\033[37m"],
c: ["\033[41m", "\033[31m"],
d: ["\033[102m", "\033[92m"],
e: ["\033[46m", "\033[36m"],
f: ["\033[40m", "\033[30m"],
g: ["\033[101m", "\033[91m"],
h: ["\033[105m", "\033[95m"]
}

var checker = /\[([a-h])\]/gi;

function allMatches(str, checker) {
var matches = []
var result;
while (result = checker.exec(str)) {
matches.push(result[1]);
}
return matches;
}

console.log("Getting comments...");
var start = Date.now();
const usedLetters = alphabet.slice(0, config.names.length);

getter.on('stats', function(s) {
stats = s;
});
usedLetters.forEach(l => stats.votes[l] = 0);

const getter = Getter(config.videoID);

const checker = new RegExp(`\\[([${usedLetters.join("")}])\\]`, "gi");

getter.on("stats", s => stats.videoStats = s);

getter.on("error", e => console.error("Error: " + e));

getter.on("data", comment => {
++stats.commentCount;

getter.on('data', function (comment) {
comments++;
var secondsAfter = (new Date(comment.publishedAt).getTime() - new Date(stats.published).getTime()) / 1000;
if (!comment || !comment.authorChannelId) return;
var hasVoted = false;
if (commentors[comment.authorChannelId.value]) {
hasVoted = true;
shinycowards++;

let hasAlreadyVoted = false;

if (stats.commentors[comment.authorChannelId.value]) {
hasAlreadyVoted = true;
++stats.shinyCowards;
}
var c = (comment.textDisplay + "").toLowerCase();
if (comments % 100 == 0) {
process.stdout.write("\033c");
console.log(`${comments}/${stats.commentCount} comments, \x1b[92m${totalvotes} valid votes\x1b[0m, \x1b[31m${deadlinevotes} deadlined \x1b[0m, \x1b[33m${shinline} shiny cowards\x1b[0m, working for ${(Date.now() - start) / 1000}s`);
}
var width = process.stdout.columns;
var multiplier = width / comments;
var voteline = Math.floor(totalvotes * multiplier);
var deadlineline = Math.floor(deadlinevotes * multiplier);
var cowardline = Math.floor(shinycowards * multiplier);
var filler = width - voteline - deadlineline - cowardline;
if (comments % 100 == 0) console.log(`\x1b[102m${" ".repeat(voteline)}\x1b[43m${" ".repeat(cowardline)}\x1b[41m${" ".repeat(deadlineline)}\x1b[100m${" ".repeat(filler)}\x1b[0m`);
allMatches(c, checker).forEach(function(l) {
if (secondsAfter <= 172800 && !hasVoted) {
votes[l]++;
totalvotes++;
hasVoted = true;
commentors[comment.authorChannelId.value] = 1;
} else if (secondsAfter > 172800) {
deadlinevotes++;
} else if (hasVoted) {
shinline++;
commentors[comment.authorChannelId.value]++;
thecowards[comment.authorDisplayName] = (thecowards[comment.authorDisplayName] || 0) + 1

const c = (comment.textDisplay + "").toLowerCase();
const timeDifference = (new Date(comment.publishedAt).getTime() - new Date(stats.videoStats.published).getTime()) / 1000;

// console.log(util.allMatches(c, checker))

util.allMatches(c, checker).forEach(l => {
if ((timeDifference <= config.deadlineAfter || config.deadlineAfter == 0) && !hasAlreadyVoted) {
stats.votes[l] = (stats.votes[l] || 0) + 1;
hasAlreadyVoted = true;
stats.commentors[comment.authorChannelId.value] = 1;
} else if (timeDifference > config.deadlineAfter && config.deadlineAfter > 0) {
++stats.votesAfterDeadline;
} else if (hasAlreadyVoted) {
++stats.commentors[comment.authorChannelId.value];
stats.cowardVotes[comment.authorDisplayName] = (stats.cowardVotes[comment.authorDisplayName] || 0) + 1
}
});
if (comments % 100 == 0) console.log(Object.keys(votes).sort(function(a, b) {
if (votes[a] > votes[b]) return -1;
if (votes[a] < votes[b]) return 1;
return 0;
}).map(function(l) {
var width = process.stdout.columns;
var barlength = Math.floor(width * (votes[l] / totalvotes)) || 0;
var textlen = `${contestants[l]}: ${votes[l]}`.length;
var filler = width - barlength - textlen;
return colors[l][0] + " ".repeat(filler > 0 ? barlength : barlength + filler) + "\033[100m" + " ".repeat(filler > 0 ? filler : 0) +
`${colors[l][1]}${contestants[l]}\x1b[39m: ${votes[l]}` + "\033[0m";
}).join("\n"));
});

getter.on('error', function (err) {
console.error('ERROR READING COMMENTS:', err)
if (stats.commentCount % 100 == 0) {
const totalVotes = Object.values(stats.votes).reduce((a, b) => a + b);

process.stdout.write("\x1bc");

console.log(`${stats.commentCount}/${stats.videoStats.commentCount} comments, ` +
`${fg.light_green}${totalVotes} valid votes${util.colors.reset}, ` +
`${fg.red}${stats.votesAfterDeadline} deadlined${util.colors.reset}, ` +
`${fg.yellow}${stats.shinyCowards} shiny cowards${util.colors.reset}, `+
`working for ${(Date.now() - stats.startedAt) / 1000}s`);

const terminalWidth = process.stdout.columns;
const multiplier = terminalWidth / stats.commentCount;
const voteLine = Math.floor(totalVotes * multiplier);
const SCLine = Math.floor(stats.shinyCowards * multiplier);
const deadLine = Math.floor(stats.votesAfterDeadline * multiplier);
const filler = terminalWidth - voteLine - deadLine - SCLine;

console.log(bg.light_green +
" ".repeat(voteLine) +
bg.yellow +
" ".repeat(SCLine) +
bg.red +
" ".repeat(deadLine) +
bg.dark_gray +
" ".repeat(filler) +
util.colors.reset);

console.log(usedLetters
.sort((a, b) => stats.votes[b] - stats.votes[a])
.map(l => {
const contestant = alphabet.indexOf(l);
const terminalWidth = process.stdout.columns;
const barLength = Math.floor(terminalWidth * (stats.votes[l] / totalVotes)) || 0;
const textLength = `${config.names[contestant]}: ${stats.votes[l]}`.length;
const filler = terminalWidth - barLength - textLength;

return (bg[config.colors[contestant]] +
" ".repeat(filler > 0 ? barLength : barLength + filler) +
bg.dark_gray +
" ".repeat(filler > 0 ? filler : 0) +
fg[config.colors[contestant]] +
config.names[contestant] +
fg.default +
": " +
stats.votes[l] +
util.colors.reset);
})
.join(""));
}
});

getter.on('end', function () {
process.stdout.write("\033c");
console.log("FINAL RESULTS:");
console.log(Object.keys(votes).sort(function(a, b) {
if (votes[a] > votes[b]) return -1;
if (votes[a] < votes[b]) return 1;
return 0;
}).map(function(l, i) {
var width = process.stdout.columns;
var barlength = Math.floor(width * (votes[l] / totalvotes)) || 0;
var textlen = `${contestants[l]}: ${votes[l]}`.length;
var filler = width - barlength - textlen;
// return `${contestants[l]}: ${votes[l]}` + "\n\033[107m" + " ".repeat(barlength) + "\033[100m" + " ".repeat(width - barlength) + "\033[0m";
return colors[l][0] + " ".repeat(filler > 0 ? barlength : barlength + filler) + "\033[100m" + " ".repeat(filler > 0 ? filler : 0) +
`${colors[l][1]}${contestants[l]}\x1b[39m: ${votes[l]}` + "\033[0m";
}).join("\n"));
console.log();
var width = process.stdout.columns;
var multiplier = width / comments;
var voteline = Math.floor(totalvotes * multiplier);
var deadlineline = Math.floor(deadlinevotes * multiplier);
var cowardline = Math.floor(shinycowards * multiplier);
var filler = width - voteline - deadlineline - cowardline;
var theShiniest = Object.keys(thecowards).reduce((shiniest, rn) => {
if (thecowards[rn] > thecowards[shiniest]) return rn;
else return shiniest;
})
console.log(`\x1b[102m${" ".repeat(voteline)}\x1b[43m${" ".repeat(cowardline)}\x1b[41m${" ".repeat(deadlineline)}\x1b[100m${" ".repeat(filler)}\x1b[0m`);
console.log(`Total comments: ${comments}`);
console.log(`Total votes: ${totalvotes + shinline + deadlinevotes}`);
console.log(`\x1b[33mShiny coward votes\x1b[0m: ${shinline}`);
console.log(`\x1b[31mVotes after deadline\x1b[0m: ${deadlinevotes}`);
console.log(`\x1b[92mValid votes\x1b[0m: ${totalvotes}`);
console.log(`The shiniest coward: ${theShiniest} (${thecowards[theShiniest]} votes)`);
console.log(`Work time: ${(Date.now() - start) / 1000}s`);
getter.on("end", () => {
const totalVotes = Object.values(stats.votes).reduce((a, b) => a + b);
process.stdout.write("\x1bc");
console.log(usedLetters
.sort((a, b) => stats.votes[b] - stats.votes[a])
.map((l, i) => {
const contestant = alphabet.indexOf(l);
const terminalWidth = process.stdout.columns;
const barLength = Math.floor(terminalWidth * (stats.votes[l] / totalVotes)) || 0;
const textLength = `${config.names[contestant]}: ${stats.votes[l]}`.length;
const filler = terminalWidth - barLength - textLength;

return (bg[config.colors[contestant]] +
" ".repeat(filler > 0 ? barLength : barLength + filler) +
bg.dark_gray +
" ".repeat(filler > 0 ? filler : 0) +
fg[config.colors[contestant]] +
config.names[contestant] +
fg.default +
": " +
stats.votes[l] +
util.colors.reset);
})
.join(""));

console.log()

const terminalWidth = process.stdout.columns;
const multiplier = terminalWidth / stats.commentCount;
const voteLine = Math.floor(totalVotes * multiplier);
const SCLine = Math.floor(stats.shinyCowards * multiplier);
const deadLine = Math.floor(stats.votesAfterDeadline * multiplier);
const filler = terminalWidth - voteLine - deadLine - SCLine;

console.log(bg.light_green +
" ".repeat(voteLine) +
bg.yellow +
" ".repeat(SCLine) +
bg.red +
" ".repeat(deadLine) +
bg.dark_gray +
" ".repeat(filler) +
util.colors.reset);

const theShiniestCoward = Object.keys(stats.cowardVotes)
.reduce((s, r) => (stats.cowardVotes[r] > stats.cowardVotes[s]) ? r : s);
const totalCowards = Object.values(stats.cowardVotes).reduce((a, b) => a + b);

console.log(`Total comments: ${stats.commentCount}`);
console.log(`Total votes: ${totalVotes + totalCowards + stats.votesAfterDeadline}`);
console.log(`${fg.yellow}Shiny coward votes${util.colors.reset}: ${totalCowards}`);
console.log(`${fg.red}Votes after deadline${util.colors.reset}: ${stats.votesAfterDeadline}`);
console.log(`${fg.light_green}Valid votes${util.colors.reset}: ${totalVotes}`);
console.log(`The shiniest coward: ${theShiniestCoward} (${stats.cowardVotes[theShiniestCoward]} votes)`);
console.log(`Work time: ${(Date.now() - stats.startedAt) / 1000}s`);
process.exit();
});
});

0 comments on commit 0ce41b3

Please sign in to comment.
You can’t perform that action at this time.