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

var fs = require('fs');
var path = require('path');
var program = require('commander');
var inquirer = require('inquirer');
var Handlebars = require('handlebars');
var shell = require('shelljs');
var chalk = require('chalk');

var MovieDB = require('moviedb')(
  fs.readFileSync(
    path.resolve(__dirname, 'api-key.txt'),
    {encoding: `utf8`}
  ).trim()
);

var template = Handlebars.compile(
  fs.readFileSync(
    path.resolve(__dirname, 'movie-tags.hbs'),
    {encoding: `utf8`}
  )
);

program
  .version('1.0.0')
  .usage('mkv-file')
  .parse(process.argv);

if (!program.args.length) {
  program.help();
}

const MKV = program.args[0];

var query = MKV.match(/([^\n\//]+)\.(?:mkv|MKV)$/);

if (!query) {
  console.error(chalk.red(`File extension must be ".mkv" or ".MKV".`));
  process.exit(1);
} else {
  query = query[1];
}

MovieDB.searchMovie({query: query}, (err, res) => {
  if (err) {
    console.error(chalk.red(err));
    process.exit(1);
  }

  // TODO : Deal with multiple pages of results
  // TODO : Deal with 1 result

  if (!res.results.length) {
    console.error(chalk.red(`No results found for "${query}".`));
     process.exit(1);
  }

  var movieChoices = res.results.map((result) => {
    return {
      id: result.id,
      title: result.title,
      releaseDate: result.release_date,
      releaseYear: result.release_date.split('-')[0] ? result.release_date.split('-')[0] : undefined
    };
  });

  // Prompt for the specific movie if there are many results and grab its unique ID
  var question = [
    {
      type: `list`,
      name: `movieID`,
      message: `Which movie did you want?`,
      choices: movieChoices.map((movie) => {
        return {
          name: `${chalk.bold(movie.title)} – ${movie.releaseYear ? movie.releaseYear : '?'}`,
          value: movie.id
        }
      })
    }
  ];

  inquirer.prompt(question, (answer) => {
    MovieDB.movieInfo({id: answer.movieID}, (err, res) => {
      if (err) {
        console.error(chalk.red(err));
        process.exit(1);
      }

      fs.writeFileSync(`tags.xml`, template(res));

      // Merge tags into file
      shell.exec(`mkvpropedit '${MKV}' --tags all:tags.xml`);
      shell.rm(`tags.xml`);

      console.log(chalk.green(`File successfully tagged!`));
      process.exit();
    })
  });
});<commit_msg>Adding global title to MKV<commit_after>#!/usr/bin/env node

var fs = require('fs');
var path = require('path');
var program = require('commander');
var inquirer = require('inquirer');
var Handlebars = require('handlebars');
var shell = require('shelljs');
var chalk = require('chalk');

var MovieDB = require('moviedb')(
  fs.readFileSync(
    path.resolve(__dirname, 'api-key.txt'),
    {encoding: `utf8`}
  ).trim()
);

var template = Handlebars.compile(
  fs.readFileSync(
    path.resolve(__dirname, 'movie-tags.hbs'),
    {encoding: `utf8`}
  )
);

program
  .version('1.0.0')
  .usage('mkv-file')
  .parse(process.argv);

if (!program.args.length) {
  program.help();
}

const MKV = program.args[0];

var query = MKV.match(/([^\n\//]+)\.(?:mkv|MKV)$/);

if (!query) {
  console.error(chalk.red(`File extension must be ".mkv" or ".MKV".`));
  process.exit(1);
} else {
  query = query[1];
}

MovieDB.searchMovie({query: query}, (err, res) => {
  if (err) {
    console.error(chalk.red(err));
    process.exit(1);
  }

  // TODO : Deal with multiple pages of results
  // TODO : Deal with 1 result

  if (!res.results.length) {
    console.error(chalk.red(`No results found for "${query}".`));
     process.exit(1);
  }

  var movieChoices = res.results.map((result) => {
    return {
      id: result.id,
      title: result.title,
      releaseDate: result.release_date,
      releaseYear: result.release_date.split('-')[0] ? result.release_date.split('-')[0] : undefined
    };
  });

  // Prompt for the specific movie if there are many results and grab its unique ID
  var question = [
    {
      type: `list`,
      name: `movieID`,
      message: `Which movie did you want?`,
      choices: movieChoices.map((movie) => {
        return {
          name: `${chalk.bold(movie.title)} – ${movie.releaseYear ? movie.releaseYear : '?'}`,
          value: movie.id
        }
      })
    }
  ];

  inquirer.prompt(question, (answer) => {
    MovieDB.movieInfo({id: answer.movieID}, (err, res) => {
      if (err) {
        console.error(chalk.red(err));
        process.exit(1);
      }

      fs.writeFileSync(`tags.xml`, template(res));

      // Add main title to file
      shell.exec(`mkvpropedit --set title="${res.title}" "${MKV}"`);

      // Merge tags into file
      shell.exec(`mkvpropedit "${MKV}" --tags all:tags.xml`);

      // Remove tags XML
      shell.rm(`tags.xml`);

      console.log(chalk.green(`File successfully tagged!`));
      process.exit();
    })
  });
});
{'fraction_non_alphanumeric': 0.1876675603217158, 'fraction_numerical': 0.005774386471437409, 'mean_word_length': 2.895582329317269, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '1900140', 'n_tokens_mistral': 1728, 'n_tokens_neox': 1670, 'n_words': 356}