<commit_before>#!/usr/bin/env node

var Q        = require('q');
var open     = require('open');
var events   = require('events');
var https    = require('https');
var libapi   = require('./api');
var libdata  = require('./data');
var termMenu = require('terminal-menu-2');
var exec     = require('child_process').exec;
var spawn    = require('child_process').spawn;

var OPTIONS  = require('./getopts');
var COLUMNS  = process.stdout.columns || 80;
var ROWS     = process.stdout.rows || 24;
var colMax   = COLUMNS - 4;
var rowMax   = ROWS - 2;

var MENU, RUNNING = {}, ITEMS = [], HASH;
var INDICATOR_ON = ' ◉ ', INDICATOR_OFF = ' ◯ ';
var VIDEO_TO_START = 'Play this video', VIDEO_TO_STOP = 'Stop this video';

Q().
then(function() {
  ITEMS = libdata.readCache('subscriptions');
  HASH = libdata.readCache('hash');
  makeMenu();
}).
then(function() {
  return libdata.checkCookie();
}).
then(function(cookie) {
  if (!MENU) process.stdout.write('Retrieving list of videos ... ');
  var pagesNeeded = Math.ceil(rowMax / libapi.subscription.itemsPerPage);
  return libapi.subscription.get(serial(pagesNeeded), cookie);
}).
then(function(data) {
  var hash = cacheHash(data);
  libdata.saveCache(data, 'subscriptions');
  libdata.saveCache(hash, 'hash');
  ITEMS = data;
  if (HASH !== hash) {     // if there are new videos, update menu
    if (!MENU.detailsPage) { // update menu if we are not on details page
      makeMenu();
    }
  }
}).
catch(function(err) {
  error(err);
});

// events

var ytEvents = new events.EventEmitter();

ytEvents.on('start', function(index) {
  var url = ITEMS[index].url;
  if (MENU.detailsPage) {
    if (MENU.items[0].label === VIDEO_TO_STOP) {
      return killall(RUNNING[url].pid);
    }
    MENU.items[0].label = VIDEO_TO_STOP;
    MENU._drawRow(0);
  } else {
    if (MENU.items[index].label.slice(0, 3) === INDICATOR_ON) {
      return killall(RUNNING[url].pid);
    }<commit_msg>fix bugs;<commit_after>#!/usr/bin/env node

var Q        = require('q');
var open     = require('open');
var events   = require('events');
var https    = require('https');
var libapi   = require('./api');
var libdata  = require('./data');
var termMenu = require('terminal-menu-2');
var exec     = require('child_process').exec;
var spawn    = require('child_process').spawn;

var OPTIONS  = require('./getopts');
var COLUMNS  = process.stdout.columns || 80;
var ROWS     = process.stdout.rows || 24;
var colMax   = COLUMNS - 4;
var rowMax   = ROWS - 2;

var MENU, RUNNING = {}, ITEMS = [], HASH;
var INDICATOR_ON = ' ◉ ', INDICATOR_OFF = ' ◯ ';
var VIDEO_TO_START = 'Play this video', VIDEO_TO_STOP = 'Stop this video';

Q().
then(function() {
  ITEMS = libdata.readCache('subscriptions');
  HASH = libdata.readCache('hash');
  makeMenu();
}).
then(function() {
  return libdata.checkCookie();
}).
then(function(cookie) {
  if (!MENU) process.stdout.write('Retrieving list of videos ... ');
  var pagesNeeded = Math.ceil(rowMax / libapi.subscription.itemsPerPage);
  return libapi.subscription.get(serial(pagesNeeded), cookie);
}).
then(function(data) {
  var hash = cacheHash(data);
  libdata.saveCache(data, 'subscriptions');
  libdata.saveCache(hash, 'hash');
  ITEMS = data;
  if (HASH !== hash) {     // if there are new videos, update menu
    if (!MENU || !MENU.detailsPage) { // update menu if we are not on details page
      makeMenu();
    }
  }
}).
catch(function(err) {
  error(err);
});

// events

var ytEvents = new events.EventEmitter();

ytEvents.on('start', function(index) {
  var url = ITEMS[index].url;
  if (MENU.detailsPage) {
    if (MENU.items[0].label === VIDEO_TO_STOP) {
      return killall(RUNNING[url].pid);
    }
    MENU.items[0].label = VIDEO_TO_STOP;
    MENU._drawRow(0);
  } else {
    if (MENU.items[index].label.slice(0, 3) === INDICATOR_ON) {
      return killall(RUNNING[url].pid);
    }
{'fraction_non_alphanumeric': 0.17233324682065923, 'fraction_numerical': 0.006228912535686478, 'mean_word_length': 3.654589371980676, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1096397', 'n_tokens_mistral': 1408, 'n_tokens_neox': 1358, 'n_words': 306}