<commit_before>module.exports = function () {
    var winston = require('winston');
    var express = require('express');
    var unirest = require('unirest');
    var app = express();

    // Models
    var Game = require('../../models/game');
    var Genre = require('../../models/genre');
    var Platform = require('../../models/platform');
    var Editor = require('../../models/editor');
    var Developer = require('../../models/developer');

    // Enums
    var CODE = require('../../enums/codes');

    var MASHAP_KEY = "<KEY>";
    var ACCEPT_JSON = "application/json";

    // Automatically add new games from Metacritic
    // Type : {coming-soon, new-releases}
    // Platform : {ps4, xboxone, ps3, xbox360, pc, wii-u, 3ds, vita, ios}
    // ie : http://localhost:8080/utils/metacritic/game-list/new-releases
    app.get('/metacritic/game-list/:type', function (req, res) {

        winston.info("Metacritic - Searching for games on Metacritic...");

        var type = req.params.type;

        //var platform = req.params.platform;
        var platform = "pc" // Force the platform to pc for this app

        unirest.get("https://byroredux-metacritic.p.mashape.com/game-list/pc/" + type)
            .header("X-Mashape-Key", MASHAP_KEY)
            .header("Accept", ACCEPT_JSON)
            .end(function (result) {

                if (result.body.results) {
                    var games = result.body.results;

                    asyncLoop(0, games, function () {
                        res.json(CODE.SUCCESS_POST);
                    });
                }
                else {
                    winston.info("Metacritic - No result ! Maybe an error ?");
                    res.json(CODE.SERVER_ERROR);
                }
            }
        )
        ;
    });

    // Recursive loop with callback to process correctly each game
    function asyncLoop(i, games, callback) {
        if (i < games.length) {
            var mcGame = games[i];
            winston.info("Metacritic - Check game name : " + mcGame.name);

            // Check if a game already exist with this name
            Game.findOneQ({name: mcGame.name})
                .then(function (game) {
                    if (game) {
                        winston.info("Metacritic ---- Game '" + game.name + "' already exist in db !");
                        asyncLoop(i + 1, games, callback);
                    } else {
                        winston.info("Metacritic ---- Game '" + mcGame.name + "' is new, so add it !");

                        // Build the game object from mcGame
                        var game = new Game();
                        game.name = mcGame.name;
                        game.metacritic.url = mcGame.url;
                        game.releaseDate = mcGame.rlsdate;
                        game.overview = mcGame.summary;
                        game.platform = mcGame.platform;

                        if (mcGame.score != "tbd") {
                            game.metacritic.score = mcGame.score;
                        }

                        // Save the current game to db
                        game.saveQ()
                            .then(function (game) {
                                winston.info("---- " + game.name + " added !");
                                asyncLoop(i + 1, games, callback);
                            })
                            .catch(function (err) {
                                console.error(err)
                                asyncLoop(i + 1, games, callback);
                            });
                    }
                })
                .catch(function (err) {
                    console.error(err)
                    res.send(CODE.SERVER_ERROR);
                });
        } else {
            callback();
        }
    }

    // Automatically update games list from Metacritic data
    // Platform : {3 : pc}
    // ie : http://localhost:8084/utils/metacritic/search/Tupa
    app.get('/metacritic/search/:name', function (req, res) {

        var platform_id = 3; // Fix platform id to pc for the app
        var name = req.params.name;

        winston.info("Metacritic - Searching for '" + name + "' on Metacritic...");

        unirest.post("https://byroredux-metacritic.p.mashape.com/search/game")
            .header("X-Mashape-Key", MASHAP_KEY)
            .header("Content-Type", "application/x-www-form-urlencoded")
            .header("Accept", ACCEPT_JSON)
            .send({
                "max_pages": 1,
                "platform": platform_id,
                "retry": 4,
                "title": name
            })
            .end(function (result) {

                //winston.info(result.body.result);

                if (result.body.count > 0) {

                    winston.info("Metacritic - " + result.body.count + " games found for : " + name);

                    //winston.info("Metacritic - Add first game to db...");

                    winston.info("Metacritic - Trying to add '" + result.body.results[0].name + "' to db...")

                    //var mcGame = result.body.results[0];

                    addGamesRecursive(0, result.body.results, function () {
                        winston.info("Metacritic - Recursive search ok");
                        res.send();
                    });

                } else {
                    winston.info("Metacritic - No result ! Maybe an error ?");
                    res.send();
                }
            }
        );
    });

    // Automatically update game information from Metacritic data
    // Platform : {3 : pc}<commit_msg>Metacritic fix with unirest module.<commit_after>module.exports = function () {
    var winston = require('winston');
    var express = require('express');
    var unirest = require('unirest');
    var app = express();

    // Models
    var Game = require('../../models/game');
    var Genre = require('../../models/genre');
    var Platform = require('../../models/platform');
    var Editor = require('../../models/editor');
    var Developer = require('../../models/developer');

    // Enums
    var CODE = require('../../enums/codes');

    var MASHAP_KEY = "<KEY>";
    var ACCEPT_JSON = "application/json";

    // Automatically add new games from Metacritic
    // Type : {coming-soon, new-releases}
    // Platform : {ps4, xboxone, ps3, xbox360, pc, wii-u, 3ds, vita, ios}
    // ie : http://localhost:8080/utils/metacritic/game-list/new-releases
    app.get('/metacritic/game-list/:type', function (req, res) {

        winston.info("Metacritic - Searching for games on Metacritic...");

        var type = req.params.type;

        //var platform = req.params.platform;
        var platform = "pc" // Force the platform to pc for this app

        unirest.get("https://byroredux-metacritic.p.mashape.com/game-list/pc/" + type)
            .header("X-Mashape-Key", MASHAP_KEY)
            .header("Accept", ACCEPT_JSON)
            .end(function (result) {

                if (result.body.results) {
                    var games = result.body.results;

                    asyncLoop(0, games, function () {
                        res.json(CODE.SUCCESS_POST);
                    });
                }
                else {
                    winston.info("Metacritic - No result ! Maybe an error ?");
                    res.json(CODE.SERVER_ERROR);
                }
            }
        )
        ;
    });

    // Recursive loop with callback to process correctly each game
    function asyncLoop(i, games, callback) {
        if (i < games.length) {
            var mcGame = games[i];
            winston.info("Metacritic - Check game name : " + mcGame.name);

            // Check if a game already exist with this name
            Game.findOneQ({name: mcGame.name})
                .then(function (game) {
                    if (game) {
                        winston.info("Metacritic ---- Game '" + game.name + "' already exist in db !");
                        asyncLoop(i + 1, games, callback);
                    } else {
                        winston.info("Metacritic ---- Game '" + mcGame.name + "' is new, so add it !");

                        // Build the game object from mcGame
                        var game = new Game();
                        game.name = mcGame.name;
                        game.metacritic.url = mcGame.url;
                        game.releaseDate = mcGame.rlsdate;
                        game.overview = mcGame.summary;
                        game.platform = mcGame.platform;

                        if (mcGame.score != "tbd") {
                            game.metacritic.score = mcGame.score;
                        }

                        // Save the current game to db
                        game.saveQ()
                            .then(function (game) {
                                winston.info("---- " + game.name + " added !");
                                asyncLoop(i + 1, games, callback);
                            })
                            .catch(function (err) {
                                console.error(err)
                                asyncLoop(i + 1, games, callback);
                            });
                    }
                })
                .catch(function (err) {
                    console.error(err)
                    res.send(CODE.SERVER_ERROR);
                });
        } else {
            callback();
        }
    }

    // Automatically update games list from Metacritic data
    // Platform : {3 : pc}
    // ie : http://localhost:8084/utils/metacritic/search/Tupa
    app.get('/metacritic/search/:name', function (req, res) {

        var platform_id = 5; // Fix platform id to pc for the app
        var name = req.params.name;

        winston.info("Metacritic - Searching for '" + name + "' on Metacritic...");

        unirest.post("https://byroredux-metacritic.p.mashape.com/search/game")
            .header("X-Mashape-Key", MASHAP_KEY)
            //.header("Content-Type", "application/x-www-form-urlencoded")
            .header("Accept", ACCEPT_JSON)
            .send({
                "platform": platform_id,
                "title": name
            })
            .end(function (result) {

                //winston.info(result.body.result);

                if (result.body.count > 0) {

                    winston.info("Metacritic - " + result.body.count + " games found for : " + name);

                    //winston.info("Metacritic - Add first game to db...");

                    winston.info("Metacritic - Trying to add '" + result.body.results[0].name + "' to db...")

                    //var mcGame = result.body.results[0];

                    addGamesRecursive(0, result.body.results, function () {
                        winston.info("Metacritic - Recursive search ok");
                        res.send();
                    });

                } else {
                    winston.info("Metacritic - No result ! Maybe an error ?");
                    res.send();
                }
            }
        );
    });

    // Automatically update game information from Metacritic data
    // Platform : {3 : pc}
{'fraction_non_alphanumeric': 0.1296098343132015, 'fraction_numerical': 0.0046321040441831465, 'mean_word_length': 1.263508064516129, 'pattern_counts': {'":': 6, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6514604', 'n_tokens_mistral': 3191, 'n_tokens_neox': 2925, 'n_words': 663}