Skip to content

Commit

Permalink
Bugfix and performance improvement for aromav2
Browse files Browse the repository at this point in the history
  • Loading branch information
developerfromjokela committed Apr 25, 2022
1 parent 5b50960 commit a8bafc2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
52 changes: 22 additions & 30 deletions src/handlers/aromav2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Restaurant[]>((resolve, reject) => {
Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 => {
Expand Down
6 changes: 4 additions & 2 deletions src/parsers/aromiv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('<br>')) {
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)) {
Expand Down

0 comments on commit a8bafc2

Please sign in to comment.