<commit_before>/* Magic Mirror
 * Node Helper: Calendar - CalendarFetcher
 *
 * By <NAME> https://michaelteeuw.nl
 * MIT Licensed.
 */
const CalendarUtils = require("./calendarutils");
const Log = require("logger");
const ical = require("node-ical");
const fetch = require("node-fetch");
const digest = require("digest-fetch");
const https = require("https");

/**
 *
 * @param {string} url The url of the calendar to fetch
 * @param {number} reloadInterval Time in ms the calendar is fetched again
 * @param {string[]} excludedEvents An array of words / phrases from event titles that will be excluded from being shown.
 * @param {number} maximumEntries The maximum number of events fetched.
 * @param {number} maximumNumberOfDays The maximum number of days an event should be in the future.
 * @param {object} auth The object containing options for authentication against the calendar.
 * @param {boolean} includePastEvents If true events from the past maximumNumberOfDays will be fetched too
 * @param {boolean} selfSignedCert If true, the server certificate is not verified against the list of supplied CAs.
 * @class
 */
const CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, includePastEvents, selfSignedCert) {
	let reloadTimer = null;
	let events = [];

	let fetchFailedCallback = function () {};
	let eventsReceivedCallback = function () {};

	/**
	 * Initiates calendar fetch.
	 */
	const fetchCalendar = () => {
		clearTimeout(reloadTimer);
		reloadTimer = null;
		const nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
		let fetcher = null;
		let httpsAgent = null;
		let headers = {
			"User-Agent": "Mozilla/5.0 (Node.js " + nodeVersion + ") MagicMirror/" + global.version + " (https://github.com/MichMich/MagicMirror/)"
		};

		if (selfSignedCert) {
			httpsAgent = new https.Agent({
				rejectUnauthorized: false
			});
		}
		if (auth) {
			if (auth.method === "bearer") {
				headers.Authorization = "Bearer " + auth.pass;
			} else if (auth.method === "digest") {
				fetcher = new digest(auth.user, auth.pass).fetch(url, { headers: headers, httpsAgent: httpsAgent });
			} else {
				headers.Authorization = "Basic " + Buffer.from(auth.user + ":" + auth.pass).toString("base64");
			}
		}
		if (fetcher === null) {
			fetcher = fetch(url, { headers: headers, httpsAgent: httpsAgent });
		}

		fetcher
			.catch((error) => {
				fetchFailedCallback(this, error);
				scheduleTimer();
			})
			.then((response) => {
				if (response.status !== 200) {
					fetchFailedCallback(this, response.statusText);
					scheduleTimer();
				}
				return response;
			})
			.then((response) => response.text())
			.then((responseData) => {
				let data = [];

				try {
					data = ical.parseICS(responseData);
					Log.debug("parsed data=" + JSON.stringify(data));
					events = CalendarUtils.filterEvents(data, {
						excludedEvents,
						includePastEvents,
						maximumEntries,
						maximumNumberOfDays
					});
				} catch (error) {
					fetchFailedCallback(self, error.message);
					scheduleTimer();
					return;<commit_msg>Fix left over self reference<commit_after>/* Magic Mirror
 * Node Helper: Calendar - CalendarFetcher
 *
 * By <NAME> https://michaelteeuw.nl
 * MIT Licensed.
 */
const CalendarUtils = require("./calendarutils");
const Log = require("logger");
const ical = require("node-ical");
const fetch = require("node-fetch");
const digest = require("digest-fetch");
const https = require("https");

/**
 *
 * @param {string} url The url of the calendar to fetch
 * @param {number} reloadInterval Time in ms the calendar is fetched again
 * @param {string[]} excludedEvents An array of words / phrases from event titles that will be excluded from being shown.
 * @param {number} maximumEntries The maximum number of events fetched.
 * @param {number} maximumNumberOfDays The maximum number of days an event should be in the future.
 * @param {object} auth The object containing options for authentication against the calendar.
 * @param {boolean} includePastEvents If true events from the past maximumNumberOfDays will be fetched too
 * @param {boolean} selfSignedCert If true, the server certificate is not verified against the list of supplied CAs.
 * @class
 */
const CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEntries, maximumNumberOfDays, auth, includePastEvents, selfSignedCert) {
	let reloadTimer = null;
	let events = [];

	let fetchFailedCallback = function () {};
	let eventsReceivedCallback = function () {};

	/**
	 * Initiates calendar fetch.
	 */
	const fetchCalendar = () => {
		clearTimeout(reloadTimer);
		reloadTimer = null;
		const nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
		let fetcher = null;
		let httpsAgent = null;
		let headers = {
			"User-Agent": "Mozilla/5.0 (Node.js " + nodeVersion + ") MagicMirror/" + global.version + " (https://github.com/MichMich/MagicMirror/)"
		};

		if (selfSignedCert) {
			httpsAgent = new https.Agent({
				rejectUnauthorized: false
			});
		}
		if (auth) {
			if (auth.method === "bearer") {
				headers.Authorization = "Bearer " + auth.pass;
			} else if (auth.method === "digest") {
				fetcher = new digest(auth.user, auth.pass).fetch(url, { headers: headers, httpsAgent: httpsAgent });
			} else {
				headers.Authorization = "Basic " + Buffer.from(auth.user + ":" + auth.pass).toString("base64");
			}
		}
		if (fetcher === null) {
			fetcher = fetch(url, { headers: headers, httpsAgent: httpsAgent });
		}

		fetcher
			.catch((error) => {
				fetchFailedCallback(this, error);
				scheduleTimer();
			})
			.then((response) => {
				if (response.status !== 200) {
					fetchFailedCallback(this, response.statusText);
					scheduleTimer();
				}
				return response;
			})
			.then((response) => response.text())
			.then((responseData) => {
				let data = [];

				try {
					data = ical.parseICS(responseData);
					Log.debug("parsed data=" + JSON.stringify(data));
					events = CalendarUtils.filterEvents(data, {
						excludedEvents,
						includePastEvents,
						maximumEntries,
						maximumNumberOfDays
					});
				} catch (error) {
					fetchFailedCallback(this, error.message);
					scheduleTimer();
					return;
{'fraction_non_alphanumeric': 0.13427335362922607, 'fraction_numerical': 0.0025636917160711424, 'mean_word_length': 3.710943396226415, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 4, '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': '4881732', 'n_tokens_mistral': 2123, 'n_tokens_neox': 1787, 'n_words': 529}