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

var google = require('google')
var jsdom = require('jsdom');
var applescript = require("applescript");
var _ = require('lodash');
var url = require('url');
var async = require('async');
var keyword;
var Processor;
var path = require('path')
  , fs = require('fs')
  , lib  = path.join(path.dirname(fs.realpathSync(__filename)), '/lib')
  , jquery = fs.readFileSync(lib + "/jquery-1.7.2.min.js").toString()
var args = process.argv.slice(2)
  , argc = args.length
  , argv = require('optimist')
    .usage('Usage: $0 [keyword] [options]')
    .alias('c', 'clean')
    .alias('i', 'itunes')
    .alias('s', 'set')
    .alias('t', 'title')
    .describe('i', 'Get song name from itunes')
    .describe('c', 'Show only lyric')
    .describe('s', 'Set lyric of current song to itunes')
    .describe('t', 'Show song title')
    .check(function (argv) {
      if ( !argv.i && _.isEmpty(argv._)) {
        throw ''
      }
    }).argv

Processor = function() {
  var urlList;
  var processor = {};
  var processorList
  var process;
  var mapped;
  var $;

  processor['metrolyrics.com'] = function () {
    var content
      , hasLink
      forPrint = '';
    $('#lyrics-body p br').parent().contents().each(function (k, obj) {
        content = obj.textContent; 
        hasLink = content.indexOf("From:") !== -1
        if (obj._nodeName === 'br') {
          forPrint += "\n\n";
        }
        else if (obj._nodeName === 'span' && !hasLink) {
          forPrint += content;
          forPrint += "\n";
        }
    });
    return forPrint;
  }

  processorList = _.keys(processor)

  process = function() {
    mapped = urlList.filter(function(obj) {
      if (!obj.link) { return false; }
      var hostname = url.parse(obj.link).hostname
        , startWithPrefix = /^www.(.*)/g
        , match = startWithPrefix.exec(hostname);
      obj.processor = match && match[1]
      return match && _.contains(processorList, match[1])
    });
  }

  this.setLinks = function(urls) {
    urlList = urls;
    process();
  }

  this.getReqObj = function () {
    return mapped[0];
  }

  this.printLyric = function (jQuery) {
    // bind jQuery
    $ = jQuery;

    var processorName = mapped[0].processor
      , processed = processor[processorName]();


    console.log(processed);
  }
}

if (argv.i) {
  async.parallel([
      function(callback) {
        var song_name = 'tell application "iTunes" to name of current track as string';
        applescript.execString(song_name, function(err, rtn) {
          if (err) {
            callback(err, null);
          }
          else {
            callback(null, rtn);<commit_msg>Add --set for set lyric for current song.<commit_after>#!/usr/bin/env node 

var google = require('google')
var jsdom = require('jsdom');
var applescript = require("applescript");
var _ = require('lodash');
var url = require('url');
var async = require('async');
var keyword;
var Processor;
var path = require('path')
  , fs = require('fs')
  , lib  = path.join(path.dirname(fs.realpathSync(__filename)), '/lib')
  , jquery = fs.readFileSync(lib + "/jquery-1.7.2.min.js").toString()
var args = process.argv.slice(2)
  , argc = args.length
  , argv = require('optimist')
    .usage('Usage: $0 [keyword] [options]')
    .alias('c', 'clean')
    .alias('i', 'itunes')
    .alias('s', 'set')
    .alias('t', 'title')
    .describe('i', 'Get song name from itunes')
    .describe('c', 'Show only lyric')
    .describe('s', 'Set lyric of current song to itunes')
    .describe('t', 'Show song title')
    .check(function (argv) {
      if ( !argv.i && _.isEmpty(argv._)) {
        throw ''
      }
    }).argv

Processor = function() {
  var urlList;
  var processor = {};
  var processorList
  var process;
  var mapped;
  var $;

  processor['metrolyrics.com'] = function () {
    var content
      , hasLink
      forPrint = '';
    $('#lyrics-body p br').parent().contents().each(function (k, obj) {
        content = obj.textContent; 
        hasLink = content.indexOf("From:") !== -1
        if (obj._nodeName === 'br') {
          forPrint += "\n\n";
        }
        else if (obj._nodeName === 'span' && !hasLink) {
          forPrint += content;
          forPrint += "\n";
        }
    });
    return forPrint;
  }

  processorList = _.keys(processor)

  process = function() {
    mapped = urlList.filter(function(obj) {
      if (!obj.link) { return false; }
      var hostname = url.parse(obj.link).hostname
        , startWithPrefix = /^www.(.*)/g
        , match = startWithPrefix.exec(hostname);
      obj.processor = match && match[1]
      return match && _.contains(processorList, match[1])
    });
  }

  this.setLinks = function(urls) {
    urlList = urls;
    process();
  }

  this.getReqObj = function () {
    return mapped[0];
  }

  this.printLyric = function (jQuery) {
    // bind jQuery
    $ = jQuery;

    var processorName = mapped[0].processor
      , processed = processor[processorName]();

    if (argv.s) {
      var script = 'tell application "iTunes" to set lyrics of current track to "' + processed + '"' 
      applescript.execString(script, function(err, rtn) {
        if (err) {
          console.log("===============");
          console.log("SET LYRIC ERROR!");
          console.log(err);
          console.log("===============");
        }
        else {
          console.log("===============");
          console.log("SET LYRIC DONE!");
          console.log("===============");
        }
      })
    }
    console.log(processed);
  }
}

if (argv.i) {
  async.parallel([
      function(callback) {
        var song_name = 'tell application "iTunes" to name of current track as string';
        applescript.execString(song_name, function(err, rtn) {
          if (err) {
            callback(err, null);
          }
          else {
            callback(null, rtn);
{'fraction_non_alphanumeric': 0.17549838132560913, 'fraction_numerical': 0.0034077355597205657, 'mean_word_length': 2.4713187463039623, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '429665', 'n_tokens_mistral': 1903, 'n_tokens_neox': 1855, 'n_words': 434}