<commit_before>'use strict';

var express = require('express');
var bodyParser = require('body-parser');
var http = require('http');
var path = require('path');
var favicon = require('serve-favicon');
var morgan = require('morgan'); // formerly express.logger
var errorhandler = require('errorhandler');
var app = express();
var EventEmitter = require("events").EventEmitter;

var server = http.createServer(app);
var io = require('socket.io').listen(server);

// all environments
app.set('port', 8000);
app.engine('html', require('ejs').renderFile);

// express/connect middleware
app.use(favicon(__dirname + '/app/favicon.ico'));
app.use(morgan('dev'));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));

// serve up static assets
app.use(express.static(path.join(__dirname, 'app')));

app.post('/videos/videos.json', function(req, res) {
    var fs = require('fs');
    var fileName = __dirname + '/app/videos/videos.json';
    var fileContent = fs.readFileSync(fileName);
    var content = JSON.parse(fileContent);
    var videoUpdated = req.body;
    for (var i in content) {
        if (content[i].videoId === videoUpdated.videoId) {
            content[i] = videoUpdated;
            console.log("Updated " + content[i]);
            break;
        }
    }
    fs.writeFile(fileName, JSON.stringify(content, null, 4), function(err) {
        if (err) return console.log(err);
        console.log('writing to ' + fileName);
    });
});

app.post('/videos/:playlist', function(req, res) {
    var fs = require('fs');
    var fileName = __dirname + '/app/videos/' + req.params.playlist;
    var fileContent = fs.readFileSync(fileName);
    var content = JSON.parse(fileContent);
    var videoUpdated = req.body;
    for (var i in content) {
        if (content[i].videoId === videoUpdated.videoId) {
            content[i] = videoUpdated;
            console.log("found!");
            break;
        }
    }
    fs.writeFile(fileName, JSON.stringify(content, null, 4), function(err) {
        if (err) return console.log(err);
        console.log('writing to ' + fileName);
    });
});

// development only
if ('development' === app.get('env')) {
    app.use(errorhandler());
}

// omxcontrol

var exec = require('child_process').exec;
var pipe = false;
var map = false;
var playlistVideos = [];

function omx(mapper) {
    map = mapper;
    return omx.express;
}

omx.start = function(onFinish) {
    if (!pipe && playlistVideos.length > 0) {
        var video = playlistVideos.shift();
        var videoFile = video.file;
        var option = video.option;
        var tempPipe = 'omxcontrol' + guid();
        pipe = tempPipe;
        exec('mkfifo ' + pipe);
        videoPlaying = true;
        videoPause = false;
        io.emit("pause", {
            playing: videoPlaying,
            pause: videoPause
        });
        if (!option) {
            option = "";
        }
        console.log('omxplayer "' + videoFile + '" ' + option + ' < ' + pipe);
        exec('omxplayer "' + videoFile + '" ' + option + ' < ' + pipe, function(error, stdout, stderr) {
            console.log(stdout);
            console.log("killing temp pipe");
            if (pipe == tempPipe) {
                exec('rm ' + tempPipe);
                pipe = false;
            }<commit_msg>Turn on tv when new video are asked<commit_after>'use strict';

var express = require('express');
var bodyParser = require('body-parser');
var http = require('http');
var path = require('path');
var favicon = require('serve-favicon');
var morgan = require('morgan'); // formerly express.logger
var errorhandler = require('errorhandler');
var app = express();

var server = http.createServer(app);
var io = require('socket.io').listen(server);

// all environments
app.set('port', 8000);
app.engine('html', require('ejs').renderFile);

// express/connect middleware
app.use(favicon(__dirname + '/app/favicon.ico'));
app.use(morgan('dev'));

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}));

// serve up static assets
app.use(express.static(path.join(__dirname, 'app')));

app.post('/videos/videos.json', function(req, res) {
    var fs = require('fs');
    var fileName = __dirname + '/app/videos/videos.json';
    var fileContent = fs.readFileSync(fileName);
    var content = JSON.parse(fileContent);
    var videoUpdated = req.body;
    for (var i in content) {
        if (content[i].videoId === videoUpdated.videoId) {
            content[i] = videoUpdated;
            console.log("Updated " + content[i]);
            break;
        }
    }
    fs.writeFile(fileName, JSON.stringify(content, null, 4), function(err) {
        if (err) return console.log(err);
        console.log('writing to ' + fileName);
    });
});

app.post('/videos/:playlist', function(req, res) {
    var fs = require('fs');
    var fileName = __dirname + '/app/videos/' + req.params.playlist;
    var fileContent = fs.readFileSync(fileName);
    var content = JSON.parse(fileContent);
    var videoUpdated = req.body;
    for (var i in content) {
        if (content[i].videoId === videoUpdated.videoId) {
            content[i] = videoUpdated;
            console.log("found!");
            break;
        }
    }
    fs.writeFile(fileName, JSON.stringify(content, null, 4), function(err) {
        if (err) return console.log(err);
        console.log('writing to ' + fileName);
    });
});

// development only
if ('development' === app.get('env')) {
    app.use(errorhandler());
}

// omxcontrol

var exec = require('child_process').exec;
var pipe = false;
var map = false;
var playlistVideos = [];

function turn_on_and_switch_tv() {
    console.log('Turning on tv');
    exec('echo "on 0" | cec-client -s');
    console.log('Switching to raspberry pi input (1.0.0.0) for tv');
    exec('echo "tx 4F:82:10:00" | cec-client -s');
}

function omx(mapper) {
    map = mapper;
    return omx.express;
}

omx.start = function(onFinish) {
    turn_on_and_switch_tv();
    if (!pipe && playlistVideos.length > 0) {
        var video = playlistVideos.shift();
        var videoFile = video.file;
        var option = video.option;
        var tempPipe = 'omxcontrol' + guid();
        pipe = tempPipe;
        exec('mkfifo ' + pipe);
        videoPlaying = true;
        videoPause = false;
        io.emit("pause", {
            playing: videoPlaying,
            pause: videoPause
        });
        if (!option) {
            option = "";
        }
        console.log('omxplayer "' + videoFile + '" ' + option + ' < ' + pipe);
        exec('omxplayer "' + videoFile + '" ' + option + ' < ' + pipe, function(error, stdout, stderr) {
            console.log(stdout);
            console.log("killing temp pipe");
            if (pipe == tempPipe) {
                exec('rm ' + tempPipe);
                pipe = false;
            }
{'fraction_non_alphanumeric': 0.15998834329010636, 'fraction_numerical': 0.0037884307154305696, 'mean_word_length': 2.6962843295638126, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '1582695', 'n_tokens_mistral': 2155, 'n_tokens_neox': 2117, 'n_words': 482}