<commit_before>#!/usr/bin/env node
"use strict";

var program = require('commander'),
    querystring = require('querystring'),
    chalk = require('chalk'),
    fs = require('fs'),
    https = require('https');

var PERISCOPE_URL_RE = /^https:\/\/www.periscope.tv\/w\/*/i,
    DELTA_POST = 400, // Sending post request every DELTA_POST ms
    CONFIG = {},
    lastPost = 0;

// define color helper for terminal output
var error = chalk.red,
    info = chalk.blue,
    action = chalk.magenta;

program
    .version('0.0.1')
    .option('-u, --url <url>', 'periscope URL', null)
    .parse(process.argv);

/*
 * Checking cli args
 */
if(!process.argv.slice(2).length) {
    program.help();
}
if(!program.url) {
    console.log(error('Error: you must specify a periscope URL.'));
    program.help();
}
if(!program.url.match(PERISCOPE_URL_RE)) {
    console.log(error('Error: wrong URL format'));
    program.help();
}

var data = fs.readFileSync('./../../config.json');
try {
    CONFIG = JSON.parse(data);
} catch (e) {
    console.log(error('Error while parsing config.json file:'), e);
}

/**
 * Get PubNub key to subscribe
 * @param {String} token periscope token
 */
function getAccessPublic(token){
    var data = [];
    var options = require('url').parse( 'https://api.periscope.tv/api/v2/getAccessPublic?token=' + token);
    options.rejectUnauthorized = false;
    options.agent = new https.Agent( options );
    console.log('https://api.periscope.tv/api/v2/getAccessPublic?token=' + token);
    return https.get(options, function(res){
        res.on('data', function (chunk){
            data.push(chunk);
        });
        res.on('end', function(){
            var d = JSON.parse(data.join(''));
            var keys = {
                auth_key      : d.auth_token,
                subscribe_key : d.subscriber,
                publish_key   : d.publisher,
                channel       : d.channel
            };
            console.log(keys);
            subscribeOnPubnub(keys);
        });
    }).on('error', function(e) {
        console.log(error('Error getAccessPublic: ' + e.message));
    });
};

/**<commit_msg>Revert 40d206bee10aa2cf191e7dfb230f5df1eea410e1 for server/node/app.js as it introduced issue #3<commit_after>#!/usr/bin/env node
"use strict";

var program = require('commander'),
    querystring = require('querystring'),
    chalk = require('chalk'),
    fs = require('fs'),
    https = require('https');

var PERISCOPE_URL_RE = /^https:\/\/www.periscope.tv\/w\/*/i,
    DELTA_POST = 400, // Sending post request every DELTA_POST ms
    CONFIG = {},
    lastPost = 0;

// define color helper for terminal output
var error = chalk.red,
    info = chalk.blue,
    action = chalk.magenta;

program
    .version('0.0.1')
    .option('-u, --url <url>', 'periscope URL', null)
    .parse(process.argv);

/*
 * Checking cli args
 */
if(!process.argv.slice(2).length) {
    program.help();
}
if(!program.url) {
    console.log(error('Error: you must specify a periscope URL.'));
    program.help();
}
if(!program.url.match(PERISCOPE_URL_RE)) {
    console.log(error('Error: wrong URL format'));
    program.help();
}

var data = fs.readFileSync('./../../config.json');
try {
    CONFIG = JSON.parse(data);
} catch (e) {
    console.log(error('Error while parsing config.json file:'), e);
}

/**
 * Get PubNub key to subscribe
 * @param {String} token periscope token
 */
function getAccessPublic(token){
    var data = [];
    return https.get('https://api.periscope.tv/api/v2/getAccessPublic?token=' + token, function(res){
        res.on('data', function (chunk){
            data.push(chunk);
        });
        res.on('end', function(){
            var d = JSON.parse(data.join(''));
            var keys = {
                auth_key      : d.auth_token,
                subscribe_key : d.subscriber,
                publish_key   : d.publisher,
                channel       : d.channel
            };
            subscribeOnPubnub(keys);
        });
    }).on('error', function(e) {
        console.log(error('Error getAccessPublic: ' + e.message));
    });
};

/**
{'fraction_non_alphanumeric': 0.1684954889051451, 'fraction_numerical': 0.009997561570348696, 'mean_word_length': 2.5453759723422644, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 3, 'lorem ipsum': 0, 'www.': 2, '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': '4357868', 'n_tokens_mistral': 1372, 'n_tokens_neox': 1326, 'n_words': 277}