<commit_before>"use strict";

const path = require("path");
const matcher = require("./util/matcher");
const TMDBSearcher = require("./util/TMDBSearcher");

class MovieIdentifier {

	constructor(identifier) {
		this.identifier = identifier;
		this.tmdb_searcher = new TMDBSearcher(this.identifier.options.api_keys.tmdb, this.identifier.options.cache_location);
	}

	// set all_able as true if you want to use Promise.all
	identify(path_to_file, all_able) {

		return new Promise((resolve, reject) => {

			let file_name = path.basename(path_to_file);

			// the rough query name
			let query =
				this.cleanQuery(
					this.cleanFileName(
						this.findFocus(
							file_name
							)));

			this.tmdb_searcher.search(query)
				.then(r => {
					resolve(r);
				})
				.catch(e => {
					if (all_able) {
						resolve(null);
					} else {
						reject(e);
					}
				});

		});

	}

	cleanQuery(query_name) {
		// a TMDB-relevant query (including year tags where apt)
		let has_year;
		for (let term of query_name.split(" ")) {
			if (matcher.is_year(term)) {
				has_year = term;
				break;
			}
		}

		let query = query_name;

		if (has_year) {
			query = query.replace(new RegExp(has_year, "g"), "");
			query = query += " y:" + has_year;
		}

		return query;
	}

	cleanFileName(name) {
		// removes terms such as 720p from file names.
		// e.g. `The.Movie.Name.720p` becomes `the movie name`
		name = name.toLowerCase();

		let tokens = this.identifier.tokenizer.tokenize(name);
		let final_terms = [];

		for (let token of tokens) {
			if (!~this.identifier.options.remove_terms.indexOf(token)) {
				final_terms.push(token);
			}
		}

		return final_terms.join(" ");
	}

	findFocus(name) {
		/*	1st stage of cleaning a file name, focusses on what it expects to be the correct term
			e.g. `the movie name 2016 hd` becomes `the movie name` with a year of `2016`

			see https://github.com/ReflexHub/media-identifier/wiki/Media-Naming-Conventions
			for naming conventions.

			todo
		*/

		let chunks = this.identifier.tokenizer.tokenize(name),
			final_chunks = [];

		for (let id in chunks) {
			let chunk = chunks[id];
			if (id == 0 && matcher.is_random_number(chunk)) {
				// 01 The Movie blah blah
				continue;
			}

			final_chunks.push(chunk);

			if (matcher.is_year(chunk) || matcher.is_remove_term(chunk)) {
				break;
			}<commit_msg>fixed edge case identification<commit_after>"use strict";

const path = require("path");
const matcher = require("./util/matcher");
const TMDBSearcher = require("./util/TMDBSearcher");

class MovieIdentifier {

	constructor(identifier) {
		this.identifier = identifier;
		this.tmdb_searcher = new TMDBSearcher(this.identifier.options.api_keys.tmdb, this.identifier.options.cache_location);
	}

	// set all_able as true if you want to use Promise.all
	identify(path_to_file, all_able) {

		return new Promise((resolve, reject) => {

			let file_name = path.basename(path_to_file);

			// the rough query name
			let query =
				this.cleanQuery(
					this.cleanFileName(
						this.findFocus(
							file_name
							)));

			this.tmdb_searcher.search(query)
				.then(r => {
					resolve(r);
				})
				.catch(e => {
					if (all_able) {
						resolve(null);
					} else {
						reject(e);
					}
				});

		});

	}

	cleanQuery(query_name) {
		// a TMDB-relevant query (including year tags where apt)
		let has_year;
		for (let term of query_name.split(" ").reverse()) {
			if (matcher.is_year(term)) {
				has_year = term;
				break;
			}
		}

		let query = query_name;

		if (has_year) {
			query = query.replace(new RegExp(has_year, "g"), "");
			query = query += " y:" + has_year;
		}

		return query;
	}

	cleanFileName(name) {
		// removes terms such as 720p from file names.
		// e.g. `The.Movie.Name.720p` becomes `the movie name`
		name = name.toLowerCase();

		let tokens = this.identifier.tokenizer.tokenize(name);
		let final_terms = [];

		for (let token of tokens) {
			if (!~this.identifier.options.remove_terms.indexOf(token)) {
				final_terms.push(token);
			}
		}

		return final_terms.join(" ");
	}

	findFocus(name) {
		/*	1st stage of cleaning a file name, focusses on what it expects to be the correct term
			e.g. `the movie name 2016 hd` becomes `the movie name` with a year of `2016`

			see https://github.com/ReflexHub/media-identifier/wiki/Media-Naming-Conventions
			for naming conventions.

			todo
		*/

		let chunks = this.identifier.tokenizer.tokenize(name),
			final_chunks = [];

		for (let id in chunks) {
			let chunk = chunks[id];
			if (id == 0 && matcher.is_random_number(chunk)) {
				// 01 The Movie blah blah
				continue;
			}

			final_chunks.push(chunk);

			if (matcher.is_year(chunk)) {

				if(!chunks.slice(id+1).find(v => matcher.is_year(v))){
					break;
				}

			}else if(matcher.is_remove_term(chunk)){
				break;
			}
{'fraction_non_alphanumeric': 0.13516870213206375, 'fraction_numerical': 0.007658869799213414, 'mean_word_length': 2.983511953833471, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 2, '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': '3676000', 'n_tokens_mistral': 2055, 'n_tokens_neox': 1653, 'n_words': 392}