<commit_before>'use strict';

/**
 * Module dependencies.
 */
var
  util      = require('util'),
  cheerio   = require('cheerio'),
  moment    = require('moment'),
  request   = require('request'),
  Provider  = require('../../lib/provider');


var noop    = function () {};

var EPost = function () {
  Provider.call(this);
  this.id = 'epost';
  this.name = '우체국';
  this.source = 'website';
  this.TRACKING_REGEXP = /^[0-9]{13}$/;
};

util.inherits(EPost, Provider);


EPost.prototype.fetch = function (tracking, done) {
  done = done || noop;

  request({
    method: 'GET',
    url: 'https://service.epost.go.kr/trace.RetrieveDomRigiTraceList.comm',
    qs: {
      sid1: tracking,
      displayHeader: 'N'
    },
    timeout: 1000*30
  }, function (e, res, body) {
    if (e) { return done(e); }

    if (res.statusCode !== 200) {
      return done(new Error('response status isnt 200'));
    }

    done(null, body);
  });
};

EPost.prototype.parse = function (body, done) {
  done = done || noop;

  process.nextTick(function () {
    var $;

    try {
      $ = cheerio.load(body);
    } catch (e) {
      return done(e);
    }

    var
      $tables = $('table'),
      $overall = $tables.filter(function (index, el) {
        return ~$(el).find('th').first().text().indexOf('등기번호');
      }).find('td');

    if (~$overall.text().indexOf('배달정보를 찾지 못했습니다')) {
      return done(null, null);
    }

    if (!$overall.length) {
      return done(new Error('Cannot find element'));
    }

    var payload = {
      provider: {
        id: this.id,
        name: this.name,
        source: this.source
      },
      tracking: $overall.eq(0).text().trim(),
      remarks: $overall.eq(3).text().trim(),
      status: $overall.eq(4).text().trim()
    };


    [$overall.eq(1), $overall.eq(2)].forEach(function ($el) {
      $el.find('br').replaceWith('\n');
    });

    var
      sentChunks = $overall.eq(1).text().split('\n'),
      receivedChunks = $overall.eq(2).text().split('\n');

    payload.sender = sentChunks[0].trim();
    payload.sentAt = sentChunks[1].trim().replace(/[\/\.]/g, '-');
    payload.recipient = receivedChunks[0].trim();
    payload.receivedAt = receivedChunks[1].trim().replace(/[\/\.]/g, '-');


    payload.histories = $tables.filter(function (index, el) {
      return ~$(el).children('caption').text().indexOf('배송진행현황');
    }).find('tr').map(function (index, el) {
      var $columns = $(el).children('td');
      var $columns = $(el).children('td'),
          _date = $columns.eq(0).text().trim().replace(/[\/\.]/g, '-') +
              ' ' + $columns.eq(1).text().trim();

      return {
        date: moment(_date, 'YYYY-MM-DD HH:mm').toDate(),
        location: $columns.eq(2).text().trim(),
        status: $columns.eq(3).text().replace(/\s+/g, ' ').trim()
      };
    }).get().sort(function (a, b) {
      if (a.date.getTime() > b.date.getTime()) {
        return 1;
      }
      if (a.date.getTime()< b.date.getTime()) {
        return -1;
      }

      return 0;
    });

    done(null, payload);
  }.bind(this));
};


module.exports = exports = EPost;<commit_msg>epost/website: Now `sentAt` and `receivedAt` properties are instance of `Date`<commit_after>'use strict';

/**
 * Module dependencies.
 */
var
  util      = require('util'),
  cheerio   = require('cheerio'),
  moment    = require('moment'),
  request   = require('request'),
  Provider  = require('../../lib/provider');


var noop    = function () {};

var EPost = function () {
  Provider.call(this);
  this.id = 'epost';
  this.name = '우체국';
  this.source = 'website';
  this.TRACKING_REGEXP = /^[0-9]{13}$/;
};

util.inherits(EPost, Provider);


EPost.prototype.fetch = function (tracking, done) {
  done = done || noop;

  request({
    method: 'GET',
    url: 'https://service.epost.go.kr/trace.RetrieveDomRigiTraceList.comm',
    qs: {
      sid1: tracking,
      displayHeader: 'N'
    },
    timeout: 1000*30
  }, function (e, res, body) {
    if (e) { return done(e); }

    if (res.statusCode !== 200) {
      return done(new Error('response status isnt 200'));
    }

    done(null, body);
  });
};

EPost.prototype.parse = function (body, done) {
  done = done || noop;

  process.nextTick(function () {
    var $;

    try {
      $ = cheerio.load(body);
    } catch (e) {
      return done(e);
    }

    var
      $tables = $('table'),
      $overall = $tables.filter(function (index, el) {
        return ~$(el).find('th').first().text().indexOf('등기번호');
      }).find('td');

    if (~$overall.text().indexOf('배달정보를 찾지 못했습니다')) {
      return done(null, null);
    }

    if (!$overall.length) {
      return done(new Error('Cannot find element'));
    }

    var payload = {
      provider: {
        id: this.id,
        name: this.name,
        source: this.source
      },
      tracking: $overall.eq(0).text().trim(),
      remarks: $overall.eq(3).text().trim(),
      status: $overall.eq(4).text().trim()
    };


    [$overall.eq(1), $overall.eq(2)].forEach(function ($el) {
      $el.find('br').replaceWith('\n');
    });

    var
      sentChunks = $overall.eq(1).text().split('\n'),
      receivedChunks = $overall.eq(2).text().split('\n');

    payload.sender = sentChunks[0].trim();
    payload.recipient = receivedChunks[0].trim();



    payload.histories = $tables.filter(function (index, el) {
      return ~$(el).children('caption').text().indexOf('배송진행현황');
    }).find('tr').map(function (index, el) {
      var $columns = $(el).children('td');
      var $columns = $(el).children('td'),
          _date = $columns.eq(0).text().trim().replace(/[\/\.]/g, '-') +
              ' ' + $columns.eq(1).text().trim();

      return {
        date: moment(_date, 'YYYY-MM-DD HH:mm').toDate(),
        location: $columns.eq(2).text().trim(),
        status: $columns.eq(3).text().replace(/\s+/g, ' ').trim()
      };
    }).get().sort(function (a, b) {
      if (a.date.getTime() > b.date.getTime()) {
        return 1;
      }
      if (a.date.getTime()< b.date.getTime()) {
        return -1;
      }

      return 0;
    });

    payload.sentAt = payload.histories[0].date;
    payload.receivedAt = payload.histories[payload.histories.length - 1].date;

    done(null, payload);
  }.bind(this));
};


module.exports = exports = EPost;
{'fraction_non_alphanumeric': 0.21207775653282346, 'fraction_numerical': 0.011153601019757807, 'mean_word_length': 2.498885172798216, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '1849330', 'n_tokens_mistral': 2274, 'n_tokens_neox': 2239, 'n_words': 389}