From a8bafc2ad443e98306f60510478123b0fa95591c Mon Sep 17 00:00:00 2001 From: Developer From Jokela Date: Mon, 25 Apr 2022 09:41:40 +0300 Subject: [PATCH] Bugfix and performance improvement for aromav2 --- package.json | 2 +- src/handlers/aromav2.ts | 52 +++++++++++++++++------------------------ src/parsers/aromiv2.ts | 6 +++-- 3 files changed, 27 insertions(+), 33 deletions(-) diff --git a/package.json b/package.json index 277f7e9..3d16069 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "foodmenu", - "version": "1.0.9.3", + "version": "1.0.9.4", "description": "This API provides additional food menus which are not available as JSON.\nThis middleware converts them to JSON format.", "main": "build/main.js", "scripts": { diff --git a/src/handlers/aromav2.ts b/src/handlers/aromav2.ts index fe2ee30..08d25c3 100644 --- a/src/handlers/aromav2.ts +++ b/src/handlers/aromav2.ts @@ -22,6 +22,21 @@ const urlRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()] let httpClient = new Http(); let userCache = new CacheContainer(new MemoryStorage()); +// Disable image loading to save +const chromeConfig = { + profile: { + default_content_setting_values: {'images': 2, + 'plugins': 2, 'popups': 2, 'geolocation': 2, + 'notifications': 2, 'auto_select_certificate': 2, 'fullscreen': 2, + 'mouselock': 2, 'mixed_script': 2, 'media_stream': 2, + 'media_stream_mic': 2, 'media_stream_camera': 2, 'protocol_handlers': 2, + 'ppapi_broker': 2, 'automatic_downloads': 2, 'midi_sysex': 2, + 'push_messaging': 2, 'ssl_cert_decisions': 2, 'metro_switch_to_desktop': 2, + 'protected_media_identifier': 2, 'app_banner': 2, 'site_engagement': 2, + 'durable_storage': 2} + } +} + function getRestaurantList(driver: ThenableWebDriver) { return new Promise((resolve, reject) => { @@ -174,24 +189,12 @@ export function getMenuOptions(req: Request, res: Response) { if (cachedValue) { responseStatus(res, 200, true, {restaurants: cachedValue}); } else { - let options = new Options().headless().windowSize({width: 1270, height: 780}); + let options = new Options().headless().windowSize({width: 700, height: 100}); if ((global as any).seleniumArgs != null) { options.addArguments((global as any).seleniumArgs.split(",")); } - // Disable image loading to save - const chromeConfig = { - prefs: { - profile: { - managed_default_content_settings: { - images: 2 - }, - default_content_setting_values: { - images: 2 - } - } - } - } - const driver = new Builder().setChromeOptions(options).withCapabilities(chromeConfig).forBrowser("chrome").build(); + options.setUserPreferences(chromeConfig) + const driver = new Builder().setChromeOptions(options).forBrowser("chrome").build(); driver.get(url+(fullUrl ? "" : "/Default.aspx")).then(() => { getRestaurantList(driver).then(restaurants => { userCache.setItem(hashKey, restaurants, {ttl: 3600}).then(() => { @@ -238,6 +241,7 @@ export function getRestaurantPage(req: Request, res: Response) { const fetchDocument = (pdfUrl: string) => { const fetchDate = (date: string, callback: (restaurants: Day[], diets: Diet[]) => void, errorCallback: (error: Error | null) => void) => { let rssFeedUrl = pdfUrl.replace("%dmd%", date).replace("Pdf.aspx", "Rss.aspx").replace("pdf.aspx", "rss.aspx"); + httpClient.get(rssFeedUrl, (rssError, rssResponse) => { if (rssError || rssResponse == undefined) { errorCallback(rssError); @@ -299,24 +303,12 @@ export function getRestaurantPage(req: Request, res: Response) { if (value) fetchDocument(value as string); else { - let options = new Options().headless().windowSize({width: 1270, height: 780}); + let options = new Options()/*.headless()*/.windowSize({width: 700, height: 100}); if ((global as any).seleniumArgs != null) { options.addArguments((global as any).seleniumArgs.split(",")); } - // Disable image loading to save - const chromeConfig = { - prefs: { - profile: { - managed_default_content_settings: { - images: 2 - }, - default_content_setting_values: { - images: 2 - } - } - } - } - const driver = new Builder().setChromeOptions(options).withCapabilities(chromeConfig).forBrowser("chrome").build(); + options.setUserPreferences(chromeConfig) + const driver = new Builder().setChromeOptions(options).forBrowser("chrome").build(); driver.get(url+(fullUrl ? "" : "/Default.aspx")).then(() => { selectRestaurant(driver, id).then(() => { getRestaurantPDFLink(driver).then(pdfUrl => { diff --git a/src/parsers/aromiv2.ts b/src/parsers/aromiv2.ts index 63f82bd..6faf338 100644 --- a/src/parsers/aromiv2.ts +++ b/src/parsers/aromiv2.ts @@ -153,14 +153,16 @@ export async function parseRSSFeed(content: any, callback: (content: Day[]|undef if (title && desc) { title = title[0].value; desc = desc[0].value; - id = id[0] || type + id = id[0]?.value let tempMenuList: Menu[] = []; // Items parser for (let subItem of desc.split('
')) { let split = subItem.split(':'); let name = split[0].trim(); + if (name[name.length-1] === ".") + name = name.slice(0,-1); let value = split[1].trimStart(); - tempMenuList.push(new Menu(name, [new Meal(HashUtils.sha1Digest(name+'_'+id), value)])) + tempMenuList.push(new Menu(name, [new Meal(HashUtils.sha1Digest(type+name+'_'+value), value)])) } // Parse date if (title.match(dateRegex)) {