<commit_before>/*jslint node: true */

'use strict';

var provider = require('./provider');
var async = require('async');
var request = require('request');
var fs = require('fs');
var AdmZip = require('adm-zip');
var path = require('path');
var log = require('winston');
var srt2vtt = require('srt2vtt');
var openSRT = require('popcorn-opensubtitles');

exports.getMovieSubs = function (imdb_code, cb) {
    request
        .get({
            url: provider.subtitles().url + '/' + imdb_code,
            json: true
        }, function (err, response, body) {
            if (!err && response.statusCode === 200 && !!body && body.success && body.subtitles > 0) {
                var subtitles = body.subs[imdb_code];
                cb(null, subtitles);
                _downloadMovieSubs(subtitles, imdb_code);
            }

            request
                .get({
                    url: provider.subtitles().mirrorUrl + '/' + imdb_code,
                    json: true
                }, function (err, response, body) {
                    if (err || response.statusCode >= 400 || !body || !body.success) {
                        return cb(err);
                    }

                    var subtitles = body.subtitles > 0 ? body.subs[imdb_code] : [];
                    cb(null, subtitles);
                    _downloadMovieSubs(subtitles, imdb_code);
                });
        });
};

/**
 *
 * @param {object} query {
 *  moviehash,
 *  imdbid,
 *  season,
 *  episode,
 *  tag
 * }
 * @param cb
 */
exports.getSerieSubs = function (query, cb) {
    var userAgent = 'Popcorn Time v1';
    openSRT.searchEpisode(query, userAgent).then(function (subtitles) {
        for (var lang in subtitles) {
            subtitles[lang] = subtitles[lang].url;
        }
        cb(null, subtitles);
        _downloadSerieSubs(subtitles, query.imdbid, query.season, query.episode);
    });
};

var _downloadSerieSubs = function (subtitles, imdb_code, season, episode) {
    if (!subtitles)
        return;

    var seriePath = path.normalize(path.resolve(__dirname, '..', 'public/subtitles/' + imdb_code) + '/');
    var seasonPath = path.normalize(seriePath + season + '/');
    var episodePath = path.normalize(seasonPath + episode + '/');
    console.log(episodePath);
    if (!fs.existsSync(episodePath)) {
        
        if (!fs.existsSync(seriePath))
            fs.mkdirSync(seriePath);

        if (!fs.existsSync(seasonPath))
            fs.mkdirSync(seasonPath);

        fs.mkdirSync(episodePath);

        var srtPath = path.normalize(episodePath + 'srt/');
        if (!fs.existsSync(srtPath)) {
            fs.mkdirSync(srtPath);
        }

        var vttPath = path.normalize(episodePath + 'vtt/');
        if (!fs.existsSync(vttPath)) {
            fs.mkdirSync(vttPath);
        }

        async.each(subtitles, function (language, cb) {
            _downloadSrt(srtPath, vttPath, language);

            cb();
        });
    }
};<commit_msg>removed console.log<commit_after>/*jslint node: true */

'use strict';

var provider = require('./provider');
var async = require('async');
var request = require('request');
var fs = require('fs');
var AdmZip = require('adm-zip');
var path = require('path');
var log = require('winston');
var srt2vtt = require('srt2vtt');
var openSRT = require('popcorn-opensubtitles');

exports.getMovieSubs = function (imdb_code, cb) {
    request
        .get({
            url: provider.subtitles().url + '/' + imdb_code,
            json: true
        }, function (err, response, body) {
            if (!err && response.statusCode === 200 && !!body && body.success && body.subtitles > 0) {
                var subtitles = body.subs[imdb_code];
                cb(null, subtitles);
                _downloadMovieSubs(subtitles, imdb_code);
            }

            request
                .get({
                    url: provider.subtitles().mirrorUrl + '/' + imdb_code,
                    json: true
                }, function (err, response, body) {
                    if (err || response.statusCode >= 400 || !body || !body.success) {
                        return cb(err);
                    }

                    var subtitles = body.subtitles > 0 ? body.subs[imdb_code] : [];
                    cb(null, subtitles);
                    _downloadMovieSubs(subtitles, imdb_code);
                });
        });
};

/**
 *
 * @param {object} query {
 *  moviehash,
 *  imdbid,
 *  season,
 *  episode,
 *  tag
 * }
 * @param cb
 */
exports.getSerieSubs = function (query, cb) {
    var userAgent = 'Popcorn Time v1';
    openSRT.searchEpisode(query, userAgent).then(function (subtitles) {
        for (var lang in subtitles) {
            subtitles[lang] = subtitles[lang].url;
        }
        cb(null, subtitles);
        _downloadSerieSubs(subtitles, query.imdbid, query.season, query.episode);
    });
};

var _downloadSerieSubs = function (subtitles, imdb_code, season, episode) {
    if (!subtitles)
        return;

    var seriePath = path.normalize(path.resolve(__dirname, '..', 'public/subtitles/' + imdb_code) + '/');
    var seasonPath = path.normalize(seriePath + season + '/');
    var episodePath = path.normalize(seasonPath + episode + '/');

    if (!fs.existsSync(episodePath)) {
        
        if (!fs.existsSync(seriePath))
            fs.mkdirSync(seriePath);

        if (!fs.existsSync(seasonPath))
            fs.mkdirSync(seasonPath);

        fs.mkdirSync(episodePath);

        var srtPath = path.normalize(episodePath + 'srt/');
        if (!fs.existsSync(srtPath)) {
            fs.mkdirSync(srtPath);
        }

        var vttPath = path.normalize(episodePath + 'vtt/');
        if (!fs.existsSync(vttPath)) {
            fs.mkdirSync(vttPath);
        }

        async.each(subtitles, function (language, cb) {
            _downloadSrt(srtPath, vttPath, language);

            cb();
        });
    }
};
{'fraction_non_alphanumeric': 0.14309906858594412, 'fraction_numerical': 0.0037256562235393736, 'mean_word_length': 2.0443298969072163, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7625028', 'n_tokens_mistral': 1914, 'n_tokens_neox': 1824, 'n_words': 360}