<commit_before>/**
 * Created by aleckim on 2017. 6. 21..
 */

var router = require('express').Router();
var async = require('async');
var req = require('request');
var config = require('../../config/config');

router.use(function timestamp(req, res, next){
    var printTime = new Date();
    log.info('+ nation > request | Time[', printTime.toISOString(), ']');

    next();
});

/**
 *
 * @param cityName
 * @param callback
 */
function requestApi(cityName, callback) {
    var version = "v000803";
    var url = config.push.serviceServer+"/"+version+"/town"+"/"+encodeURIComponent(cityName);

    log.info('Start '+cityName+' '+new Date());
    req(url, {json:true}, function(err, response, body) {
        log.info('Finished '+cityName+' '+new Date());
        if (err) {
            return callback(err);
        }
        if ( response.statusCode >= 400) {
            err = new Error("response.statusCode="+response.statusCode);
            log.error(err);
            return callback(err);
        }
        callback(undefined, body);
    });
}

router.get('/SouthKorea', function(req, res) {

    //서울(서울경기), 춘천(강원도), 부산(경남), 대구(경북), 제주(제주), 광주(전남), 대전(충남), 청주(충북), 전주(전북), 강릉,
    //수원, 인천, 안동, 울릉도/독도, 목표, 여수, 울산, 백령도, 창원
    var cityArray = ["서울특별시", "강원도/춘천시", "부산광역시", "대구광역시", "제주특별자치도/제주시", "광주광역시", "대전광역시",
        "충청북도/청주시", "전라북도/전주시", "강원도/강릉시"];

    async.map(cityArray,
        function (city, callback) {
            requestApi(city, function (err, weatherInfo) {
                if (err) {
                    return callback(err);<commit_msg>new city in national weather #739 update accept-language, query params<commit_after>/**
 * Created by aleckim on 2017. 6. 21..
 */

var router = require('express').Router();
var async = require('async');
var req = require('request');
var config = require('../../config/config');

var ControllerTown24h = require('../../controllers/controllerTown24h');
var cTown = new ControllerTown24h();

router.use(function timestamp(req, res, next){
    var printTime = new Date();
    log.info('+ nation > request | Time[', printTime.toISOString(), ']');

    next();
});

/**
 *
 * @param cityName
 * @param version
 * @param query
 * @param lang
 * @param callback
 */
function requestApi(cityName, version, query, lang, callback) {
    var apiEndpoint = '/town';
    if (version === 'v000901') {
       apiEndpoint = '/kma/addr'
    }
    var url = config.push.serviceServer+"/"+version+apiEndpoint+"/"+encodeURIComponent(cityName);

    if (query) {
        var count = 0;
        for (var key in query) {
            url += count === 0? "?":"&";
            url += key+'='+query[key];
            count++;
        }
    }

    /**
     * 못 받으면 다음 요청에 받도록 timeout을 길게 함
     */
    var options = {json: true, timeout: 9000, headers: {'Accept-Language' : lang}};

    log.info({city: cityName, date: new Date(), url: url});

    req(url, options, function(err, response, body) {
        log.info('Finished '+cityName+' '+new Date());
        if (err) {
            return callback(err);
        }
        if ( response.statusCode >= 400) {
            err = new Error("response.statusCode="+response.statusCode);
            log.error(err);
            return callback(err);
        }
        callback(undefined, body);
    });
}

router.get('/:nation', [cTown.checkQueryValidation], function(req, res) {
    //서울(서울경기), 춘천(강원도), 부산(경남), 대구(경북), 제주(제주), 광주(전남), 대전(충남), 청주(충북), 전주(전북), 강릉,
    //수원, 인천, 안동, 울릉도/독도, 목표, 여수, 울산, 백령도, 창원
    var cityArray = ["서울특별시", "강원도/춘천시", "부산광역시", "대구광역시", "제주특별자치도/제주시", "광주광역시", "대전광역시",
        "충청북도/청주시", "전라북도/전주시", "강원도/강릉시", "인천광역시", "전라남도/목포시", "전라남도/여수시", "경상북도/안동시", "울산광역시"];

    var acceptLanguage;
    if (req.headers) {
        if (req.headers.hasOwnProperty('accept-language')) {
           acceptLanguage = req.headers['accept-language'];
        }
        if (req.headers.hasOwnProperty('Accept-Language')) {
            acceptLanguage = req.headers['Accept-Language'];
        }
    }

    async.map(cityArray,
        function (city, callback) {
            requestApi(city, req.version, req.query, acceptLanguage, function (err, weatherInfo) {
                if (err) {
                    return callback(err);
{'fraction_non_alphanumeric': 0.18327822390174775, 'fraction_numerical': 0.011100614076523381, 'mean_word_length': 2.644578313253012, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, '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': '3296177', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1894, 'n_words': 328}