const moment = require('moment');

const plannedWork = require('./plannedWork');
const { baseURL, routes, imgMap } = require('./constants.js');

function buildLink({ route, datetime }) {
  const date = moment(datetime);
  return `${baseURL}?tag=${route}&date=${date.format('MM/DD/YYYY')}&time=&method=getstatus4`;
}

function plannedWorkByDate(datetime) {
  const pages = routes.map((route) => {
    const link = buildLink({ route, datetime });
    return plannedWork(link);
  });

  // each advisory can have multiple messages
  const messagesByRoute = {};
  return Promise.all(pages)
    .then((advisories) => {
      advisories.forEach((advisory, index) => {
        const route = routes[index];
        messagesByRoute[route] = advisory;
      });
      return messagesByRoute;
}

module.exports = plannedWorkByDate;

const moment = require('moment');

const plannedWork = require('./plannedWork');
const DateRange = require('./DateRange');
const { baseURL, routes, imgMap } = require('./constants.js');

function buildLink({ route, datetime }) {
  const date = moment(datetime);
  return `${baseURL}?tag=${route}&date=${date.format('MM/DD/YYYY')}&time=&method=getstatus4`;
}

function plannedWorkByDate(datetime) {
  const pages = routes.map((route) => {
    const link = buildLink({ route, datetime });
    return plannedWork(link);
  });

  // each advisory can have multiple messages
  const messagesByRoute = {};
  return Promise.all(pages)
    .then((advisories) => {
      advisories.forEach((advisory, index) => {
        const route = routes[index];
        messagesByRoute[route] = advisory;
      });
      return messagesByRoute;
    });
}

/**
 * Scrapes service advisories across multiple date ranges
 *
 * @param Number num - How many results to return
 * @param Date from - Which date to start with
 * @return Array[Object] - Batches of work for a specified
 *   date range
 */
function getWorkBatches(num=3, from=Date.now()) {
  const batches = [];
  const start = new DateRange(from);

  for (let offset = 0; offset < num; offset++) {
    const dateRange = start.next(offset);
    const advisories = plannedWorkByDate(dateRange.start);
    batches.push(Promise.resolve(advisories)
      .then((plannedWork) => ({
        type: dateRange.type,
        start: dateRange.start,
        end: dateRange.end,
        plannedWork,
      })))
  }

  return Promise.all(batches)
}

module.exports = getWorkBatches;

Implement crawling by date range
Implement crawling by date range

The website will display Service Advisories first by date range, then by
train route. Previous commits added methods for scraping all routes for
a particular date. This commit adds a method to invoke that method for
multiple date ranges.
