<commit_before>"use strict";
require("babel/polyfill");
var axios = require("axios");
var cheerio = require("cheerio");
var range = require("lodash.range");
var moment = require("moment");
require("moment-timezone");
var icalendar = require("icalendar");


function parseUTCTime({month, day, time}) {
    var s = month +"-"+ day +"-"+ time;
    return moment.utc(s, "MMM-D-HHmm").tz("Europe/Helsinki");
}

function fetchOpeningHours(url, headerText) {
    return axios.get(url)
    .then(function(res) {
        var $ = cheerio.load(res.data);

        var data = $("table td span").filter((i, el) => $(el).text().includes(headerText)).text();
        data = data.replace(headerText, "");
        data = data.split(",");
        data = data.map(v => v.trim());


        return data.reduce((res, entry) => {
            var [month, days, ...hours] = entry.split(" ");

            if (days.includes("-")) {
                let [start, end] = days.split("-").map(d => parseInt(d, 10));
                days = range(start, end+1);
            } else {
                days = [parseInt(days, 10)];
            }

            days = days.map(d => String(d));

            hours = hours.map(h => {
                var [start, end] = h.split("-");
                return {start, end};
            });


            days.forEach(day => {
                hours.forEach(h => {
                    var start = parseUTCTime({month, day, time: h.start});
                    var end = parseUTCTime({month, day, time: h.end});
                    res.push({start, end});
                });
            });

            return res;

        }, []);

    });

}

function generateICAL(openingHours) {
    var cal = new icalendar.iCalendar();

    openingHours.forEach(towerOpen => {
        var eventID = String(towerOpen.start.unix()) + String(towerOpen.end.unix());
        var e = new icalendar.VEvent(eventID);

        var diff = towerOpen.end.diff(towerOpen.start);
        var minutes = diff / 1000 / 60;
        var fullHours = Math.floor(minutes / 60);
        var remainingMinutes =  minutes % 60;
        var summary = "<NAME> " + fullHours + "h";
        if (remainingMinutes) {
            summary += " " + remainingMinutes + "min";
        }

        e.setSummary(summary);
        e.setDate(towerOpen.start.toDate(), towerOpen.end.toDate());
        cal.addComponent(e);
    });

    return cal.toString();
}

fetchOpeningHours("https://ais.fi/ais/bulletins/envfra.htm", "JYVASKYLA TWR OPR HR:")
.then(openingHours => {
    process.stdout.write(generateICAL(openingHours));
    // console.log(openingHours);
    // (generateICAL(openingHours));
})
.catch(function(err) {
    console.error(err.stack);
    process.exit(1);<commit_msg>Do not add hour if none<commit_after>"use strict";
require("babel/polyfill");
var axios = require("axios");
var cheerio = require("cheerio");
var range = require("lodash.range");
var moment = require("moment");
require("moment-timezone");
var icalendar = require("icalendar");


function parseUTCTime({month, day, time}) {
    var s = month +"-"+ day +"-"+ time;
    return moment.utc(s, "MMM-D-HHmm").tz("Europe/Helsinki");
}

function fetchOpeningHours(url, headerText) {
    return axios.get(url)
    .then(function(res) {
        var $ = cheerio.load(res.data);

        var data = $("table td span").filter((i, el) => $(el).text().includes(headerText)).text();
        data = data.replace(headerText, "");
        data = data.split(",");
        data = data.map(v => v.trim());


        return data.reduce((res, entry) => {
            var [month, days, ...hours] = entry.split(" ");

            if (days.includes("-")) {
                let [start, end] = days.split("-").map(d => parseInt(d, 10));
                days = range(start, end+1);
            } else {
                days = [parseInt(days, 10)];
            }

            days = days.map(d => String(d));

            hours = hours.map(h => {
                var [start, end] = h.split("-");
                return {start, end};
            });


            days.forEach(day => {
                hours.forEach(h => {
                    var start = parseUTCTime({month, day, time: h.start});
                    var end = parseUTCTime({month, day, time: h.end});
                    res.push({start, end});
                });
            });

            return res;

        }, []);

    });

}

function generateICAL(openingHours) {
    var cal = new icalendar.iCalendar();

    openingHours.forEach(towerOpen => {
        var eventID = String(towerOpen.start.unix()) + String(towerOpen.end.unix());
        var e = new icalendar.VEvent(eventID);
        var summary = "<NAME>";

        var diff = towerOpen.end.diff(towerOpen.start);
        var minutes = diff / 1000 / 60;
        var fullHours = Math.floor(minutes / 60);
        var remainingMinutes =  minutes % 60;

        if (fullHours) {
            summary += " " + fullHours + "h";
        }

        if (remainingMinutes) {
            summary += " " + remainingMinutes + "min";
        }

        e.setSummary(summary);
        e.setDate(towerOpen.start.toDate(), towerOpen.end.toDate());
        cal.addComponent(e);
    });

    return cal.toString();
}

fetchOpeningHours("https://ais.fi/ais/bulletins/envfra.htm", "JYVASKYLA TWR OPR HR:")
.then(openingHours => {
    process.stdout.write(generateICAL(openingHours));
})
.catch(function(err) {
    console.error(err.stack);
    process.exit(1);
{'fraction_non_alphanumeric': 0.1714494607932736, 'fraction_numerical': 0.005849022116614878, 'mean_word_length': 2.112627986348123, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, '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': '1899874', 'n_tokens_mistral': 1781, 'n_tokens_neox': 1682, 'n_words': 335}