<commit_before>'use strict';

const Promise = require('bluebird'),
  path = require('path'),
  urlParser = require('url'),
  merge = require('lodash.merge'),
  log = require('intel'),
  messageMaker = require('../../support/messageMaker'),
  Crawler = require('simplecrawler');

const make = messageMaker('crawler').make;

const defaultOptions = ({
  maxDepth: 3
});

module.exports = {
  name() {
    return path.basename(__dirname);
  },
  open(context, options) {
    this.options = merge({}, defaultOptions, options.crawler);
  },
  processMessage(message, queue) {
    if (message.type === 'url' && message.source !== 'crawler') {
      return new Promise((resolve) => {
        const url = urlParser.parse(message.url);
        const crawler = new Crawler(url.hostname, url.path, url.port);

        crawler.maxDepth = this.options.maxDepth;
        crawler.downloadUnsupported = false;
        crawler.supportedMimeTypes = [/text\/html/];

        crawler.on('fetchcomplete', (queueItem) => {
          log.debug('Crawler found URL %s', queueItem.url);
          // FIXME disregard initial url (might have been redirected!)<commit_msg>Skip crawl if depth is 0 or 1.<commit_after>'use strict';

const Promise = require('bluebird'),
  path = require('path'),
  urlParser = require('url'),
  merge = require('lodash.merge'),
  log = require('intel'),
  messageMaker = require('../../support/messageMaker'),
  Crawler = require('simplecrawler');

const make = messageMaker('crawler').make;

const defaultOptions = ({
  maxDepth: 3
});

module.exports = {
  name() {
    return path.basename(__dirname);
  },
  open(context, options) {
    this.options = merge({}, defaultOptions, options.crawler);
  },
  processMessage(message, queue) {
    if (message.type === 'url' && message.source !== 'crawler') {
      if (this.options.maxDepth <= 1) {
        return Promise.resolve();
      }

      return new Promise((resolve) => {
        const url = urlParser.parse(message.url);
        const crawler = new Crawler(url.hostname, url.path, url.port);

        crawler.maxDepth = this.options.maxDepth;
        crawler.downloadUnsupported = false;
        crawler.supportedMimeTypes = [/text\/html/];

        crawler.on('fetchcomplete', (queueItem) => {
          log.debug('Crawler found URL %s', queueItem.url);
          // FIXME disregard initial url (might have been redirected!)
{'fraction_non_alphanumeric': 0.1572986577181208, 'fraction_numerical': 0.0020973154362416107, 'mean_word_length': 3.3128390596745025, 'pattern_counts': {'":': 0, '<': 4, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1180766', 'n_tokens_mistral': 729, 'n_tokens_neox': 725, 'n_words': 159}