Skip to content

Commit

Permalink
Merge pull request #471 from ojaha065/develop
Browse files Browse the repository at this point in the history
1.10.12
  • Loading branch information
ojaha065 authored Nov 24, 2024
2 parents c56f829 + b05182b commit d66ab8a
Show file tree
Hide file tree
Showing 9 changed files with 843 additions and 468 deletions.
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
["kotiherkku", "Tarkasta https://www.serviini.fi/kotiherkku-ruokapuoti"]
],
"extraParams": {
"VAIHA_URL": "https://www.vaiha.fi/kaikki-tapahtumat/pormestarin-ja-vaiha-savon-viikon-lounaat-vk44"
"VAIHA_PATTERN": "https://www.vaiha.fi/kaikki-tapahtumat/pormestarin-ja-vaiha-savon-viikon-lounaat-VK${weekNumber}"
},
"gitUrl": "https://github.com/ojaha065/lounasbotti",
"displayVoters": true,
Expand Down
1,247 changes: 803 additions & 444 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "lounasbotti",
"type": "module",
"version": "1.10.11",
"version": "1.10.12",
"private": true,
"description": "Slack bot for retrieving lunch menus of local restaurants. Very much WIP and not meant for public use.",
"main": "./dist/server.js",
"engineStrict": false,
"engines": {
"node": ">=20.18.0 <21.0.0"
"node": ">=22.11.0 <23.0.0"
},
"scripts": {
"start": "node dist/server.js",
Expand Down Expand Up @@ -38,20 +38,20 @@
"eslint": "^9.7.0"
},
"devDependencies": {
"@eslint/js": "^9.13.0",
"@eslint/js": "^9.15.0",
"@sentry/cli": "^2.28.0",
"@types/node": "^22.2.0",
"@types/node-schedule": "^2.1.6",
"eslint": "^9.7.0",
"globals": "^15.9.0",
"ts-node": "^10.9.2",
"typescript": "^5.6.2",
"typescript-eslint": "^8.12.2"
"typescript-eslint": "^8.15.0"
},
"dependencies": {
"@sentry/integrations": "^7.100.1",
"@sentry/node": "^8.17.0",
"@slack/bolt": "^4.0.1",
"@slack/bolt": "^4.1.0",
"cheerio": "^1.0.0-rc.12",
"date-holidays": "^3.23.8",
"dotenv": "^16.4.2",
Expand Down
24 changes: 22 additions & 2 deletions src/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,14 @@ const fetchWithTimeout = (url: string | URL, init: RequestInit = {}, allowRetry
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 8000);

return fetch(url, {...init, signal: controller.signal}).catch(async (error: unknown) => {
return fetch(url, {
headers: {
"User-Agent": `Mozilla/5.0 (compatible; Lounasbotti/${global.LOUNASBOTTI_VERSION})`,
...init.headers
},
signal: controller.signal,
...init,
}).catch(async (error: unknown) => {
console.error(error);
if (allowRetry) {
await new Promise(resolve => { setTimeout(resolve, 2000); });
Expand Down Expand Up @@ -106,4 +113,17 @@ const takeUntil = <T>(arr: T[], predicate: (item: T) => boolean): T[] => {
return arr.slice(0, index);
};

export { splitByBrTag, getCurrentWeekdayNameInFinnish, capitalizeString, clearObject, requireNonNullOrUndefined, fetchWithTimeout, decodeBase64, takeUntil };
/**
* Get the current week number from a date
* @param {Date} date Date object. Defaults to current date
* @returns {number} Week number
*/
const getWeekNumber = (date: Date = new Date()): number => {
const _date = new Date(Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()));
_date.setUTCDate(_date.getUTCDate() + 4 - (_date.getUTCDay() || 7));

const yearStart = new Date(Date.UTC(_date.getUTCFullYear(), 0, 1));
return Math.ceil((((_date.getTime() - yearStart.getTime()) / 86400000) + 1) / 7);
};

export { splitByBrTag, getCurrentWeekdayNameInFinnish, capitalizeString, clearObject, requireNonNullOrUndefined, fetchWithTimeout, decodeBase64, takeUntil, getWeekNumber };
1 change: 0 additions & 1 deletion src/model/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ async function tryToReadSettingsFromURL(url: URL): Promise<any> {
const response = await Utils.fetchWithTimeout(url.toString(), {
method: "GET",
headers: {
"User-Agent": `Mozilla/5.0 (compatible; Lounasbotti/${global.LOUNASBOTTI_VERSION};)`,
Accept: "application/json"
}
});
Expand Down
5 changes: 1 addition & 4 deletions src/model/dataProviders/RuokapaikkaFiDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ class RuokapaikkaFiDataProvider implements LounasDataProvider {
}).toString();

const response = await Utils.fetchWithTimeout(url.toString(), {
method: "GET",
headers: {
"User-Agent": `Mozilla/5.0 (compatible; Lounasbotti/${global.LOUNASBOTTI_VERSION}; +${this.settings.gitUrl})`
}
method: "GET"
});

if (!response.ok) {
Expand Down
5 changes: 1 addition & 4 deletions src/model/dataProviders/TalliDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ class TalliDataProvider implements LounasDataProvider {
}

const response = await Utils.fetchWithTimeout(this.baseUrl, {
method: "GET",
headers: {
"User-Agent": `Mozilla/5.0 (compatible; Lounasbotti/${global.LOUNASBOTTI_VERSION}; +${this.settings.gitUrl})`
}
method: "GET"
});

if (!response.ok) {
Expand Down
15 changes: 9 additions & 6 deletions src/model/dataProviders/VaihaDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,19 @@ class VaihdaDataProvider implements LounasDataProvider {
throw new Error(`This data provider only supports ${this.supportedRestaurants}`);
}

const fetchUrl = this.settings.extraParams?.VAIHA_URL;
const now = new Date();
if (tomorrowRequest) { now.setDate(now.getDate() + 1); }

const fetchUrl = this.settings.extraParams?.VAIHA_PATTERN
?.replaceAll("${weekNumber}", Utils.getWeekNumber(now).toString());

if (!fetchUrl) {
throw new Error("VAIHA_URL is missing");
throw new Error("VAIHA_PATTERN is missing");
}
console.debug(`VAIHA URL: ${fetchUrl}`);

const response = await Utils.fetchWithTimeout(fetchUrl, {
method: "GET",
headers: {
"User-Agent": `Mozilla/5.0 (compatible; Lounasbotti/${global.LOUNASBOTTI_VERSION}; +${this.settings.gitUrl})`
}
method: "GET"
});

if (!response.ok) {
Expand Down
2 changes: 1 addition & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ dotenv.config();

// Global
global.LOUNASBOTTI_JOBS = {};
global.LOUNASBOTTI_VERSION = process.env["npm_package_version"] ?? "1.10.11";
global.LOUNASBOTTI_VERSION = process.env["npm_package_version"] ?? "1.10.12";
global.LOUNASBOTTI_TO_BE_TRUNCATED = [];

import * as Sentry from "@sentry/node";
Expand Down

0 comments on commit d66ab8a

Please sign in to comment.