{"size": 1155, "ext": "js", "max_stars_repo_path": "serverless/lib/magicseaweed.js", "max_stars_repo_name": "davidhampgonsalves/life-dashboard", "max_stars_repo_stars_event_min_datetime": "2019-12-19T14:50:41.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-21T18:18:19.000Z", "max_issues_repo_path": "serverless/lib/magicseaweed.js", "max_issues_repo_name": "davidhampgonsalves/life-dashboard", "max_issues_repo_issues_event_min_datetime": "2020-12-27T04:17:21.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-12T08:39:06.000Z", "max_forks_repo_path": "serverless/lib/magicseaweed.js", "max_forks_repo_name": "davidhampgonsalves/life-dashboard", "max_forks_repo_forks_event_min_datetime": "2020-12-26T17:40:27.000Z", "max_forks_repo_forks_event_max_datetime": "2021-02-14T18:11:10.000Z"}
{"max_stars_count": 767, "max_issues_count": 7, "max_forks_count": 17, "avg_line_length": 31.2162162162, "max_line_length": 92, "alphanum_fraction": 0.5965367965}
'use strict';

const R = require('ramda');
const rp = require('request-promise-native');
const fs = require('fs');
const moment = require('moment-timezone');

const filterByToday = R.filter(f => {
  const timestamp = moment.unix(f.timestamp).tz('America/Halifax')
  return timestamp.isSame(moment().tz('America/Halifax'), "day");
});

module.exports.fetchForecast = async () => {
  try {
    const key = R.trim(fs.readFileSync('creds/magicseaweed.txt', "utf8"));

    const forecasts = await Promise.all([
      rp({ uri: `http://magicseaweed.com/api/${key}/forecast/?spot_id=787` }),
      rp({ uri: `http://magicseaweed.com/api/${key}/forecast/?spot_id=787` }),
    ]);

    return R.pipe(
      R.map(JSON.parse),
      R.flatten,
			filterByToday,
			f => ({
          maxRating: Math.max(...R.pluck('solidRating', f)),
          fadedRating: Math.max(...R.pluck('fadedRating', f)),
          height: R.max(...R.map(R.path(["swell", "components", "combined", "height"]), f)),
          period: R.max(...R.map(R.path(["swell", "components", "combined", "period"]), f)),
        }))(forecasts);
  } catch (e) {
		console.error(e);
    return {};
  }
};
