<commit_before>/**
 * Created by Peter on 2015. 10. 23..
 *
 * 동네예보 RSS 요청 주 소: http://www.kma.go.kr/wid/queryDFS.jsp?
 * 요청 방식
 *      1. mx, my를 이용하는 방법 : 뒤에 gridx=mx, gridy=my 를 넣어서 request.
 *          ex)http://www.kma.go.kr/wid/queryDFS.jsp?gridx=59&gridy=125
 *      2. AreaNo를 이용하는 방법 : 뒤에 zone=AreaNo 를 넣어서 request
 *          ex)http://www.kma.go.kr/wid/queryDFSRSS.jsp?zone=1159068000
 * 주의 : 두가지 요청방식에 따라 response 형식이 다르니 주의 해야한다
 */
"use strict";
var events = require('events');
var async = require('async');
var req = require('request');
var config = require('../../config/config');
var fs = require('fs');
var convert = require('../../utils/coordinate2xy');
var xml2json  = require('xml2js').parseString;
var shortRssDb = require('../../models/modelShortRss');
var town = require('../../models/town');

var modelKmaTownShortRss = require('../../models/kma/kma.town.short.rss.model.js');
var kmaTimelib = require('../../lib/kmaTimeLib');

/*
 *   @constructor
 */
function TownRss(){
    var self = this;

    self.addrGrid = 'http://www.kma.go.kr/wid/queryDFS.jsp';
    self.addrZond = 'http://www.kma.go.kr/wid/queryDFSRSS.jsp';

    self.TIME_PERIOD_TOWN_RSS = (1000*60*60*3);
    self.MAX_SHORT_COUNT = 60;
    self.SUCCESS = 0;
    self.ERROR = -1;
    self.RETRY = -2;

    events.EventEmitter.call(this);

    self.on('recvFail', function(index, item){
        setTimeout(function(){
            self.getData(index, item);
        }, 500);
    });

    self.on('recvSuccess', function(index){
        self.receivedCount++;

        if(self.receivedCount == self.coordDb.length){
            log.info('receive complete! : count=', self.receivedCount);
        }
    });

    return self;
}

/*
 *
 */
TownRss.prototype.loadList = function(completionCallback){
    var self = this;
    self.coordDb = [];

    town.getCoord(function(err, coordList){
        if(err){
            log.error('ShortRSS> failed to get coord');
        } else {
            log.silly('ShortRSS> coord: ', coordList);
            coordList.forEach(function (coord) {
                var item = {mCoord: coord};
                self.coordDb.push(item);
            });

            log.info('ShortRSS> coord count : ', self.coordDb.length);
        }

        if(completionCallback) {
            completionCallback(err);
        }
    });

    return self;
};

/*
 *   @param index
 *   @param url
 *   @param callback
 */
TownRss.prototype.getShortRss = function(index, url, callback){
    var self = this;
    var meta = {};

    meta.method = 'getShortRss';
    meta.url = url;

    log.verbose('get short rss URL : ', url);
    req.get(url, {json:true}, function(err, response, body){
        if(err){
            log.warn('failed to req (%d)', index);
            log.warn(err);
            if(callback){
                callback(self.RETRY);
            }
            return;
        }

        if(!response.statusCode || response.statusCode === 404 || response.statusCode === 403){
            log.warn('WARN!!! StatusCode : ', statusCode);
            if(callback){
                callback(self.RETRY);
            }
            return;
        }

        //log.info(body);
        xml2json(body, function(err, result){
            if(err){
                log.error('>>ERROR : failed to convert to json');
                if(callback){
                    callback(self.ERROR);
                }
            }
            try{
                //log.info(result)
                if(callback){
                    callback(self.SUCCESS, result);
                }
            }<commit_msg>#1914 fixed double calling callback function<commit_after>/**
 * Created by Peter on 2015. 10. 23..
 *
 * 동네예보 RSS 요청 주 소: http://www.kma.go.kr/wid/queryDFS.jsp?
 * 요청 방식
 *      1. mx, my를 이용하는 방법 : 뒤에 gridx=mx, gridy=my 를 넣어서 request.
 *          ex)http://www.kma.go.kr/wid/queryDFS.jsp?gridx=59&gridy=125
 *      2. AreaNo를 이용하는 방법 : 뒤에 zone=AreaNo 를 넣어서 request
 *          ex)http://www.kma.go.kr/wid/queryDFSRSS.jsp?zone=1159068000
 * 주의 : 두가지 요청방식에 따라 response 형식이 다르니 주의 해야한다
 */
"use strict";
var events = require('events');
var async = require('async');
var req = require('request');
var config = require('../../config/config');
var fs = require('fs');
var convert = require('../../utils/coordinate2xy');
var xml2json  = require('xml2js').parseString;
var shortRssDb = require('../../models/modelShortRss');
var town = require('../../models/town');

var modelKmaTownShortRss = require('../../models/kma/kma.town.short.rss.model.js');
var kmaTimelib = require('../../lib/kmaTimeLib');

/*
 *   @constructor
 */
function TownRss(){
    var self = this;

    self.addrGrid = 'http://www.kma.go.kr/wid/queryDFS.jsp';
    self.addrZond = 'http://www.kma.go.kr/wid/queryDFSRSS.jsp';

    self.TIME_PERIOD_TOWN_RSS = (1000*60*60*3);
    self.MAX_SHORT_COUNT = 60;
    self.SUCCESS = 0;
    self.ERROR = -1;
    self.RETRY = -2;

    events.EventEmitter.call(this);

    self.on('recvFail', function(index, item){
        setTimeout(function(){
            self.getData(index, item);
        }, 500);
    });

    self.on('recvSuccess', function(index){
        self.receivedCount++;

        if(self.receivedCount == self.coordDb.length){
            log.info('receive complete! : count=', self.receivedCount);
        }
    });

    return self;
}

/*
 *
 */
TownRss.prototype.loadList = function(completionCallback){
    var self = this;
    self.coordDb = [];

    town.getCoord(function(err, coordList){
        if(err){
            log.error('ShortRSS> failed to get coord');
        } else {
            log.silly('ShortRSS> coord: ', coordList);
            coordList.forEach(function (coord) {
                var item = {mCoord: coord};
                self.coordDb.push(item);
            });

            log.info('ShortRSS> coord count : ', self.coordDb.length);
        }

        if(completionCallback) {
            completionCallback(err);
        }
    });

    return self;
};

/*
 *   @param index
 *   @param url
 *   @param callback
 */
TownRss.prototype.getShortRss = function(index, url, callback){
    var self = this;
    var meta = {};

    meta.method = 'getShortRss';
    meta.url = url;

    log.verbose('get short rss URL : ', url);
    req.get(url, {json:true}, function(err, response, body){
        if(err){
            log.warn('failed to req (%d)', index);
            log.warn(err);
            if(callback){
                callback(self.RETRY);
            }
            return;
        }

        if(!response.statusCode || response.statusCode === 404 || response.statusCode === 403){
            log.warn('WARN!!! StatusCode : ', statusCode);
            if(callback){
                callback(self.RETRY);
            }
            return;
        }

        //log.info(body);
        xml2json(body, function(err, result){
            if(err){
                log.error('>>ERROR : failed to convert to json');
                if(callback){
                    return callback(self.ERROR);
                }
            }
            try{
                //log.info(result)
                if(callback){
                    callback(self.SUCCESS, result);
                }
            }
{'fraction_non_alphanumeric': 0.15766888150609082, 'fraction_numerical': 0.014950166112956811, 'mean_word_length': 1.9429735234215886, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 10, 'xml': 6}, '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': '3746233', 'n_tokens_mistral': 2489, 'n_tokens_neox': 2424, 'n_words': 450}