const FeedMe = require('feedme');
const https = require('https');
const EventEmitter = require('events').EventEmitter;

const RECENT_PERIOD = 5 * 60 * 1000; // ms
const earthquakeFeed = 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.atom';

function mapCategories(categories) {
  return categories.reduce((catMap, category) => {
    return Object.assign({}, catMap, { [category.label]: category.term });
  }, {});
}

// watch for earthquakes
class Seismometer extends EventEmitter {
  constructor(period) {
    super();

    this.pollingPeriod = period || 1000;
  }

  checkForEarthquakes() {
    const parse = quakeItem => {
      const info = mapCategories(quakeItem.category);

      // parse magnitude from title because it's more precise there
      const magnitude = parseFloat(/M (-?\d+\.\d+)/.exec(quakeItem.title)[1]);

      const locationParts = quakeItem['georss:point'].split(/\s/);
      const latitude = parseFloat(locationParts[0]);
      const longitude = parseFloat(locationParts[1]);

      return {
        date: new Date(Date.parse(quakeItem.updated)),
        magnitude,
        latitude,
        longitude,
      }
    };

    https.get(earthquakeFeed, res => {
      if (res.statusCode !== 200) {
        console.log(`Earthquake API server returned non-OK status code ${res.statusCode}`);
        return;
      }

      const parser = new FeedMe();

      parser.on('updated', updateTime => {
        if (updateTime !== this.lastUpdateTime) {
          this.lastUpdateTime = updateTime;
        }
      })

      parser.on('item', item => {
        const quake = parse(item);

        if (this.lastQuake === undefined) {
          // prime the watcher
          this.lastQuake = quake;
        }

        const happenedRecently = (Date.now() - quake.date.getTime()) < RECENT_PERIOD;

        if (this.lastQuake.date.getTime() < quake.date.getTime() && happenedRecently) {
          this.emit('quake', quake);
          this.lastQuake = quake;
        }
      });

      res.pipe(parser);
    });
  }

  watch() {
    this.pollingId = setInterval(() => this.checkForEarthquakes(), this.pollingPeriod);
  }

  stopWatching() {
    clearInterval(this.pollingId);
    this.pollingId = undefined;
  }
}

module.exports = Seismometer;

@@ -784,30 +784,26 @@
 agnitude
- = parseFloat(
+Matches = 
 /M (-?%5Cd
@@ -832,16 +832,266 @@
 m.title)
+;%0A%0A      if (magnitudeMatches === null %7C%7C magnitudeMatches.length %3C 2) %7B%0A        console.log(%60Failed to read earthquake magnitude from data %22$%7BquakeItem.title%7D%22%60);%0A        return undefined;%0A      %7D%0A%0A      const magnitude = parseFloat(magnitudeMatches
 %5B1%5D);%0A%0A 
@@ -1409,16 +1409,42 @@
  %7D;%0A%0A   
+ const feedUpdateRequest =
  https.g
@@ -1883,24 +1883,143 @@
 rse(item);%0A%0A
+        if (quake === undefined) %7B%0A          // No quake was parsed from the data, ignore%0A          return;%0A        %7D%0A%0A
         if (
@@ -2417,23 +2417,86 @@
 arser);%0A
-
     %7D);
+%0A%0A    feedUpdateRequest.on('error', (err) =%3E console.log(err));
 %0A  %7D%0A%0A  

Handle errors more gracefully