<commit_before>var alexa = require("alexa-app");
var search = require('youtube-search');
var ytdl = require('ytdl-core');
var s3 = require('s3');

global.__bucket = process.env.S3_BUCKET;

var app = new alexa.app("youtube");

var s3Client = s3.createClient({
    s3Options: {
        accessKeyId: process.env.S3_ACCESS_KEY,
        secretAccessKey: process.env.S3_SECRET_ACCESS_KEY
    }
});

var searchOpts = {
    maxResults: 1,
    type: 'video',
    key: process.env.YOUTUBE_API_KEY
};

var constants = {
    'token': 'string',
    'expectedPreviousToken': 'string'
};

var lastSearch;

app.pre = function(request, response, type) {
    if (request.applicationId != "amzn1.ask.skill.e252ffe0-987b-43c2-abec-58050fd7153b") {
        response.fail("Invalid application");
    }
};

app.intent("GetVideoIntent", {
        "slots": {
            "VideoQuery": "VIDEOS",
        },
        "utterances": [
            "search for {-|VideoQuery}",
            "find {-|VideoQuery}",
            "play {-|VideoQuery}",
            "start playing {-|VideoQuery}",
            "put on {-|VideoQuery}"
        ]
    },
    function(request, response) {
        var query = request.slot("VideoQuery");

        search(query, searchOpts, function(err, results) {
            if (err || results.length !== 1) {
                response.say('I could not complete your request at this moment.').send();
            } else {
                var metadata = results[0];
                if (metadata.link === undefined) {
                    response.say(query + ' did not return any results on YouTube.').send();
                } else {
                    response.say('I found a relevant video called '+metadata.title+'.');

                    var tmpfile = require('path').join('/tmp', metadata.id+'.mp3');
                    var key = require('path').join('audio', metadata.id);

                    ytdl(metadata.link, {
                        filter: function(format) {
                            return format.container === 'mp3';
                        }
                    }).pipe(fs.createWriteStream(tmpfile).on('finish', function () {
                        var params = {
                            localFile: tmpfile,
                            s3Params: {
                                Bucket: __bucket,
                                Key: key
                            }
                        };
                        var uploader = s3Client.uploadFile(params);
                        uploader.on('error', function(err) {
                            response.say('I had trouble downloading this video.').send();
                        });
                        uploader.on('end', function() {
                            lastSearch = s3.getPublicUrl(__bucket, key);
                            response.card({
                                'type': 'Simple',
                                'title': metadata.title,
                                'content': metadata.link
                            }).audioPlayerPlayStream('REPLACE_ALL', {
                                'url': lastSearch,
                                'streamFormat': 'AUDIO_MPEG',
                                'token': constants.token,
                                'expectedPreviousToken': constants.expectedPreviousToken
                            }).send();
                        });
                    }));
                }
            }
        });

        return false;
    }
);

app.intent("AMAZON.PauseIntent", {}, function(request, response) {
    response.audioPlayerStop();<commit_msg>Restructure<commit_after>var alexa = require("alexa-app");
var search = require('youtube-search');
var ytdl = require('ytdl-core');
var s3 = require('s3');

global.__bucket = process.env.S3_BUCKET;

var app = new alexa.app("youtube");

var s3Client = s3.createClient({
    s3Options: {
        accessKeyId: process.env.S3_ACCESS_KEY,
        secretAccessKey: process.env.S3_SECRET_ACCESS_KEY
    }
});

var searchOpts = {
    maxResults: 1,
    type: 'video',
    key: process.env.YOUTUBE_API_KEY
};

var constants = {
    'token': 'string',
    'expectedPreviousToken': 'string'
};

var lastSearch;

app.pre = function(request, response, type) {
    if (request.applicationId != "amzn1.ask.skill.e252ffe0-987b-43c2-abec-58050fd7153b") {
        response.fail("Invalid application");
    }
};

app.intent("GetVideoIntent", {
        "slots": {
            "VideoQuery": "VIDEOS",
        },
        "utterances": [
            "search for {-|VideoQuery}",
            "find {-|VideoQuery}",
            "play {-|VideoQuery}",
            "start playing {-|VideoQuery}",
            "put on {-|VideoQuery}"
        ]
    },
    function(request, response) {
        var query = request.slot("VideoQuery");

        search(query, searchOpts, function(err, results) {
            if (err || results.length !== 1) {
                response.say('I could not complete your request at this moment.').send();
            } else {
                var metadata = results[0];
                if (metadata.link === undefined) {
                    response.say(query + ' did not return any results on YouTube.').send();
                } else {
                    response.say('I found a relevant video called '+metadata.title+'.');

                    var tmpfile = require('path').join('/tmp', metadata.id+'.mp3');
                    var key = require('path').join('audio', metadata.id);

                    var writer = fs.createWriteStream(tmpfile);
                    writer.on('finish', function () {
                        var uploader = s3Client.uploadFile({
                            localFile: tmpfile,
                            s3Params: {
                                Bucket: __bucket,
                                Key: key
                            }
                        });
                        uploader.on('error', function(err) {
                            response.say('I had trouble downloading this video.').send();
                        });
                        uploader.on('end', function() {
                            lastSearch = s3.getPublicUrl(__bucket, key);
                            response.card({
                                'type': 'Simple',
                                'title': metadata.title,
                                'content': metadata.link
                            }).audioPlayerPlayStream('REPLACE_ALL', {
                                'url': lastSearch,
                                'streamFormat': 'AUDIO_MPEG',
                                'token': constants.token,
                                'expectedPreviousToken': constants.expectedPreviousToken
                            }).send();
                        });
                    });

                    ytdl(metadata.link, {
                        filter: function(format) {
                            return format.container === 'mp3';
                        }
                    }).pipe(writer);
                }
            }
        });

        return false;
    }
);

app.intent("AMAZON.PauseIntent", {}, function(request, response) {
    response.audioPlayerStop();
{'fraction_non_alphanumeric': 0.12474098632407792, 'fraction_numerical': 0.009946125155408206, 'mean_word_length': 1.3138382869926495, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '593076', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1799, 'n_words': 357}