From 899525066f6f9d66810bf189a2fb492815fc14d9 Mon Sep 17 00:00:00 2001 From: Christoph Holter Date: Wed, 20 Apr 2016 13:21:48 +0200 Subject: [PATCH 1/9] Basic functionality for fabrik --- external-scripts.json | 2 +- package.json | 10 +++-- scripts/feedme.coffee | 85 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 scripts/feedme.coffee diff --git a/external-scripts.json b/external-scripts.json index a869d9f..6a5a2b3 100644 --- a/external-scripts.json +++ b/external-scripts.json @@ -9,4 +9,4 @@ "hubot-redis-brain", "hubot-rules", "hubot-shipit" -] \ No newline at end of file +] diff --git a/package.json b/package.json index 3657eed..70b20fe 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,11 @@ { "name": "hubot", - "version": "0.0.0", + "version": "1.0.0", "private": true, "author": "Lorenz Leutgeb ", "description": "A simple helpful robot for Catalysts", "dependencies": { + "datejs": "^1.0.0-rc3", "hubot": "^2.18.0", "hubot-diagnostics": "0.0.1", "hubot-google-images": "^0.2.6", @@ -17,9 +18,12 @@ "hubot-rules": "^0.1.1", "hubot-scripts": "^2.16.2", "hubot-shipit": "^0.2.0", - "hubot-slack": "^3.4.2" + "hubot-slack": "^3.4.2", + "jquery": "^2.2.3", + "jsdom": "^8.4.0", + "urllib-sync": "^1.1.2" }, "engines": { - "node": "0.10.x" + "node": "5.10.x" } } diff --git a/scripts/feedme.coffee b/scripts/feedme.coffee new file mode 100644 index 0000000..57c8f52 --- /dev/null +++ b/scripts/feedme.coffee @@ -0,0 +1,85 @@ +# Description: +# Shows what"s edible. +# +# Notes: +# This includes +# * fetching from fabrik +# * fetching from ernis + +urllib = require "urllib-sync" +jsdom = require "jsdom" +require "datejs" + +DAY = 1000 * 60 * 60 * 24 + +getDateDiff = (date1, date2, interval) -> + return Math.round((date1.getTime() - date2.getTime()) / interval) + +class Fabrik + target : "http://www.diefabrik.co.at/mittagsmenue/index.html" + holidayMagic : "urlaub" + errors: + closed : "fabrik is closed" + menuFromPast :"the menu is outdated" + menuFromFuture :"the menu is from the future" + resting :"fabrik is on a day off" + holiday :"fabrik is probably on holiday, fall back to manual check" + + constructor: (@robot) -> + + fetch: => + return urllib.request(@target).data + + checkHoliday: (text) => + return true if text.toLowerCase().indexOf(@holidayMagic) > 0 + + parseDates: ($) -> + docDate = $("h2:eq(0)").html() + return /(\d{2}\.\d{2}\.\d{4}) bis (\d{2}\.\d{2}\.\d{4})/.exec(docDate) + + extractMeal: ($) -> + # weekday: 0 Sunday to 6 Saturday + day = new Date().getDay() + + # Saturday & Sunday + return @errors.closed if day == 6 | day == 0 + menu = $(".contenttable .tr-#{(day-1)*2} .td-2").html() + # Ruhetag + return @errors.resting if menu.toLowerCase() == 'ruhetag' + # all good :) + return menu + + getMenue: () => + now = new Date() + lastCheck = new Date(@robot.brain.get "feedme.fabrik.lastCheck") + lastCheck = Date.parseExact("01.01.1970", "d.M.yyyy") if not lastCheck + # only check once per day, otherwise we're good to go + if getDateDiff(now, lastCheck, DAY) > 0 + rawbody = @fetch() + + return @errors.holiday if @checkHoliday(rawbody.toString()) + + # hack to include jQuery + $ = require("jquery")(jsdom.jsdom(rawbody).defaultView) + parsedDates = @parseDates($) + + # check for outdated menue + if now.getTime() > (Date.parseExact(parsedDates[2], "d.M.yyyy").getTime() + DAY) + @robot.brain.set "feedme.fabrik.save", @errors.menuFromPast + + # check for future menue + else if now.getTime() < Date.parseExact(parsedDates[1], "d.M.yyyy").getTime() + @robot.brain.set "feedme.fabrik.save", @errors.menuFromFuture + + else + @robot.brain.set "feedme.fabrik.save", @extractMeal($) + + @robot.brain.set "feedme.fabrik.lastCheck", now + +module.exports = (robot) -> + + fabrik = new Fabrik(robot) + robot.respond /feedme/i, (res) -> + fabrik.getMenue() + fabrikMenue = robot.brain.get "feedme.fabrik.save" + res.send "Heutiges Mittagsmenü: \nFabrik: \n\t#{fabrikMenue}" From 4321cabfd97de66d6ddea3f3003b3659ea0f8859 Mon Sep 17 00:00:00 2001 From: Christoph Holter Date: Thu, 21 Apr 2016 00:26:49 +0200 Subject: [PATCH 2/9] ernis already done :) --- scripts/feedme.coffee | 51 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/scripts/feedme.coffee b/scripts/feedme.coffee index 57c8f52..45dbb4d 100644 --- a/scripts/feedme.coffee +++ b/scripts/feedme.coffee @@ -15,6 +15,10 @@ DAY = 1000 * 60 * 60 * 24 getDateDiff = (date1, date2, interval) -> return Math.round((date1.getTime() - date2.getTime()) / interval) +fetch = (target) -> + return urllib.request(target).data + + class Fabrik target : "http://www.diefabrik.co.at/mittagsmenue/index.html" holidayMagic : "urlaub" @@ -27,9 +31,6 @@ class Fabrik constructor: (@robot) -> - fetch: => - return urllib.request(@target).data - checkHoliday: (text) => return true if text.toLowerCase().indexOf(@holidayMagic) > 0 @@ -37,9 +38,7 @@ class Fabrik docDate = $("h2:eq(0)").html() return /(\d{2}\.\d{2}\.\d{4}) bis (\d{2}\.\d{2}\.\d{4})/.exec(docDate) - extractMeal: ($) -> - # weekday: 0 Sunday to 6 Saturday - day = new Date().getDay() + extractMeal: ($, day) -> # Saturday & Sunday return @errors.closed if day == 6 | day == 0 @@ -51,11 +50,12 @@ class Fabrik getMenue: () => now = new Date() + day = now.getDay() lastCheck = new Date(@robot.brain.get "feedme.fabrik.lastCheck") lastCheck = Date.parseExact("01.01.1970", "d.M.yyyy") if not lastCheck # only check once per day, otherwise we're good to go if getDateDiff(now, lastCheck, DAY) > 0 - rawbody = @fetch() + rawbody = fetch(@target) return @errors.holiday if @checkHoliday(rawbody.toString()) @@ -72,14 +72,47 @@ class Fabrik @robot.brain.set "feedme.fabrik.save", @errors.menuFromFuture else - @robot.brain.set "feedme.fabrik.save", @extractMeal($) + @robot.brain.set "feedme.fabrik.save", @extractMeal($, day) @robot.brain.set "feedme.fabrik.lastCheck", now +class Ernis + target : "http://www.ernis.at" + errors: + closed : "ernis is closed" + + constructor: (@robot) -> + + extractMeal: ($, day) -> + return $("#accordion > div .moduletable:eq(#{day-1}) > div").text().trim() + + getMenue: () => + now = new Date() + day = now.getDay() + + lastCheck = new Date(@robot.brain.get "feedme.ernis.lastCheck") + lastCheck = Date.parseExact("01.01.1970", "d.M.yyyy") if not lastCheck + # only check once per day, otherwise we're good to go + if getDateDiff(now, lastCheck, DAY) > 0 + rawbody = fetch(@target) + + # hack to include jQuery + $ = require("jquery")(jsdom.jsdom(rawbody).defaultView) + # ernis closed + if day > 0 & day < 6 + @robot.brain.set "feedme.ernis.save", @extractMeal($, day) + else @robot.brain.set "feedme.ernis.save", @errors.closed + + @robot.brain.set "feedme.ernis.lastCheck", now + + module.exports = (robot) -> fabrik = new Fabrik(robot) + ernis = new Ernis(robot) robot.respond /feedme/i, (res) -> fabrik.getMenue() + ernis.getMenue() fabrikMenue = robot.brain.get "feedme.fabrik.save" - res.send "Heutiges Mittagsmenü: \nFabrik: \n\t#{fabrikMenue}" + ernisMenue = robot.brain.get "feedme.ernis.save" + res.send "Heutiges Mittagsmenü: \nFabrik: \n#{fabrikMenue}\n\nErnis: \n#{ernisMenue}" From 9fea29579d8f826b3a163ee81c2773f9b2adaf82 Mon Sep 17 00:00:00 2001 From: Christoph Holter Date: Thu, 21 Apr 2016 00:27:19 +0200 Subject: [PATCH 3/9] author note --- scripts/feedme.coffee | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/feedme.coffee b/scripts/feedme.coffee index 45dbb4d..60181a5 100644 --- a/scripts/feedme.coffee +++ b/scripts/feedme.coffee @@ -5,6 +5,8 @@ # This includes # * fetching from fabrik # * fetching from ernis +# +# Author: cholter urllib = require "urllib-sync" jsdom = require "jsdom" From e62351dedc9b22a330b7686be1973d37b3f0c033 Mon Sep 17 00:00:00 2001 From: Christoph Holter Date: Thu, 21 Apr 2016 10:56:41 +0200 Subject: [PATCH 4/9] adjust versioning as our food channel is staging env. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 70b20fe..755880d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hubot", - "version": "1.0.0", + "version": "0.1.0", "private": true, "author": "Lorenz Leutgeb ", "description": "A simple helpful robot for Catalysts", From 32642bf3edb0e20bfb2ea5dd94b1f0f159d058ed Mon Sep 17 00:00:00 2001 From: Christoph Holter Date: Thu, 21 Apr 2016 11:10:18 +0200 Subject: [PATCH 5/9] spelling, intendation + 'null' checks removed --- scripts/feedme.coffee | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/scripts/feedme.coffee b/scripts/feedme.coffee index 60181a5..242e757 100644 --- a/scripts/feedme.coffee +++ b/scripts/feedme.coffee @@ -25,16 +25,16 @@ class Fabrik target : "http://www.diefabrik.co.at/mittagsmenue/index.html" holidayMagic : "urlaub" errors: - closed : "fabrik is closed" - menuFromPast :"the menu is outdated" - menuFromFuture :"the menu is from the future" - resting :"fabrik is on a day off" - holiday :"fabrik is probably on holiday, fall back to manual check" + closed : "fabrik is closed" + menuFromPast :"the menu is outdated" + menuFromFuture :"the menu is from the future" + resting :"fabrik is on a day off" + holiday :"fabrik is probably on holiday, fall back to manual check" constructor: (@robot) -> checkHoliday: (text) => - return true if text.toLowerCase().indexOf(@holidayMagic) > 0 + return text.toLowerCase().indexOf(@holidayMagic) > 0 parseDates: ($) -> docDate = $("h2:eq(0)").html() @@ -43,18 +43,17 @@ class Fabrik extractMeal: ($, day) -> # Saturday & Sunday - return @errors.closed if day == 6 | day == 0 + return @errors.closed if day == 6 or day == 0 menu = $(".contenttable .tr-#{(day-1)*2} .td-2").html() # Ruhetag return @errors.resting if menu.toLowerCase() == 'ruhetag' # all good :) return menu - getMenue: () => + getMenu: () => now = new Date() day = now.getDay() - lastCheck = new Date(@robot.brain.get "feedme.fabrik.lastCheck") - lastCheck = Date.parseExact("01.01.1970", "d.M.yyyy") if not lastCheck + lastCheck = new Date(@robot.brain.get "feedme.fabrik.lastCheck" or 0) # only check once per day, otherwise we're good to go if getDateDiff(now, lastCheck, DAY) > 0 rawbody = fetch(@target) @@ -65,11 +64,11 @@ class Fabrik $ = require("jquery")(jsdom.jsdom(rawbody).defaultView) parsedDates = @parseDates($) - # check for outdated menue + # check for outdated menu if now.getTime() > (Date.parseExact(parsedDates[2], "d.M.yyyy").getTime() + DAY) @robot.brain.set "feedme.fabrik.save", @errors.menuFromPast - # check for future menue + # check for future menu else if now.getTime() < Date.parseExact(parsedDates[1], "d.M.yyyy").getTime() @robot.brain.set "feedme.fabrik.save", @errors.menuFromFuture @@ -88,12 +87,11 @@ class Ernis extractMeal: ($, day) -> return $("#accordion > div .moduletable:eq(#{day-1}) > div").text().trim() - getMenue: () => + getMenu: () => now = new Date() day = now.getDay() - lastCheck = new Date(@robot.brain.get "feedme.ernis.lastCheck") - lastCheck = Date.parseExact("01.01.1970", "d.M.yyyy") if not lastCheck + lastCheck = new Date(@robot.brain.get "feedme.ernis.lastCheck" or 0) # only check once per day, otherwise we're good to go if getDateDiff(now, lastCheck, DAY) > 0 rawbody = fetch(@target) @@ -113,8 +111,8 @@ module.exports = (robot) -> fabrik = new Fabrik(robot) ernis = new Ernis(robot) robot.respond /feedme/i, (res) -> - fabrik.getMenue() - ernis.getMenue() - fabrikMenue = robot.brain.get "feedme.fabrik.save" - ernisMenue = robot.brain.get "feedme.ernis.save" - res.send "Heutiges Mittagsmenü: \nFabrik: \n#{fabrikMenue}\n\nErnis: \n#{ernisMenue}" + fabrik.getMenu() + ernis.getMenu() + fabrikMenu = robot.brain.get "feedme.fabrik.save" + ernisMenu = robot.brain.get "feedme.ernis.save" + res.send "Heutiges Mittagsmenü: \nFabrik: \n#{fabrikMenu}\n\nErnis: \n#{ernisMenu}" From acca10166b4d0a139519011f433152814447f258 Mon Sep 17 00:00:00 2001 From: Christoph Holter Date: Thu, 21 Apr 2016 16:58:37 +0200 Subject: [PATCH 6/9] various small issues / see #1 --- scripts/feedme.coffee | 94 ++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 45 deletions(-) diff --git a/scripts/feedme.coffee b/scripts/feedme.coffee index 242e757..e5660fa 100644 --- a/scripts/feedme.coffee +++ b/scripts/feedme.coffee @@ -15,95 +15,99 @@ require "datejs" DAY = 1000 * 60 * 60 * 24 getDateDiff = (date1, date2, interval) -> - return Math.round((date1.getTime() - date2.getTime()) / interval) + Math.round((date1.getTime() - date2.getTime()) / interval) fetch = (target) -> - return urllib.request(target).data + urllib.request(target).data class Fabrik target : "http://www.diefabrik.co.at/mittagsmenue/index.html" holidayMagic : "urlaub" - errors: + errors : closed : "fabrik is closed" - menuFromPast :"the menu is outdated" - menuFromFuture :"the menu is from the future" - resting :"fabrik is on a day off" - holiday :"fabrik is probably on holiday, fall back to manual check" + menuFromPast : "the menu is outdated" + menuFromFuture : "the menu is from the future" + resting : "fabrik is on a day off" + holiday : "fabrik is probably on holiday, fall back to manual check" constructor: (@robot) -> checkHoliday: (text) => - return text.toLowerCase().indexOf(@holidayMagic) > 0 + text.toLowerCase().indexOf(@holidayMagic) > 0 parseDates: ($) -> docDate = $("h2:eq(0)").html() - return /(\d{2}\.\d{2}\.\d{4}) bis (\d{2}\.\d{2}\.\d{4})/.exec(docDate) + /(\d{2}\.\d{2}\.\d{4}) bis (\d{2}\.\d{2}\.\d{4})/.exec(docDate) extractMeal: ($, day) -> - - # Saturday & Sunday - return @errors.closed if day == 6 or day == 0 menu = $(".contenttable .tr-#{(day-1)*2} .td-2").html() # Ruhetag - return @errors.resting if menu.toLowerCase() == 'ruhetag' - # all good :) - return menu + if menu.toLowerCase() is 'ruhetag' then @errors.resting else menu getMenu: () => now = new Date() day = now.getDay() - lastCheck = new Date(@robot.brain.get "feedme.fabrik.lastCheck" or 0) + lastCheck = new Date(@robot.brain.get("feedme.fabrik.lastCheck") or 0) # only check once per day, otherwise we're good to go - if getDateDiff(now, lastCheck, DAY) > 0 - rawbody = fetch(@target) + return if getDateDiff(now, lastCheck, DAY) < 0 + + @robot.brain.set "feedme.fabrik.lastCheck", now + rawbody = fetch(@target) - return @errors.holiday if @checkHoliday(rawbody.toString()) + if @checkHoliday(rawbody.toString()) + @robot.brain.set "feedme.fabrik.save", @errors.holiday + return - # hack to include jQuery - $ = require("jquery")(jsdom.jsdom(rawbody).defaultView) - parsedDates = @parseDates($) + # hack to include jQuery + $ = require("jquery")(jsdom.jsdom(rawbody).defaultView) + parsedDates = @parseDates($) - # check for outdated menu - if now.getTime() > (Date.parseExact(parsedDates[2], "d.M.yyyy").getTime() + DAY) - @robot.brain.set "feedme.fabrik.save", @errors.menuFromPast + # check for outdated menu + if now.getTime() > (Date.parseExact(parsedDates[2], "d.M.yyyy").getTime() + DAY) + @robot.brain.set "feedme.fabrik.save", @errors.menuFromPast + return - # check for future menu - else if now.getTime() < Date.parseExact(parsedDates[1], "d.M.yyyy").getTime() - @robot.brain.set "feedme.fabrik.save", @errors.menuFromFuture + # check for future menu + if now.getTime() < Date.parseExact(parsedDates[1], "d.M.yyyy").getTime() + @robot.brain.set "feedme.fabrik.save", @errors.menuFromFuture + return - else - @robot.brain.set "feedme.fabrik.save", @extractMeal($, day) + if day == 5 or day == 6 + @robot.brain.set "feedme.fabrik.save", @errors.closed + return - @robot.brain.set "feedme.fabrik.lastCheck", now + @robot.brain.set "feedme.fabrik.save", @extractMeal($, day) class Ernis target : "http://www.ernis.at" - errors: - closed : "ernis is closed" + errors : + closed : "ernis is closed" constructor: (@robot) -> extractMeal: ($, day) -> - return $("#accordion > div .moduletable:eq(#{day-1}) > div").text().trim() + $("#accordion > div .moduletable:eq(#{day-1}) > div").text().trim() getMenu: () => now = new Date() day = now.getDay() - lastCheck = new Date(@robot.brain.get "feedme.ernis.lastCheck" or 0) + lastCheck = new Date(@robot.brain.get("feedme.ernis.lastCheck") or 0) # only check once per day, otherwise we're good to go - if getDateDiff(now, lastCheck, DAY) > 0 - rawbody = fetch(@target) + return if getDateDiff(now, lastCheck, DAY) < 0 + + @robot.brain.set "feedme.ernis.lastCheck", now + rawbody = fetch(@target) - # hack to include jQuery - $ = require("jquery")(jsdom.jsdom(rawbody).defaultView) - # ernis closed - if day > 0 & day < 6 - @robot.brain.set "feedme.ernis.save", @extractMeal($, day) - else @robot.brain.set "feedme.ernis.save", @errors.closed + # hack to include jQuery + $ = require("jquery")(jsdom.jsdom(rawbody).defaultView) + # ernis closed + if day == 5 or day == 6 + @robot.brain.set "feedme.ernis.save", @errors.closed + return - @robot.brain.set "feedme.ernis.lastCheck", now + @robot.brain.set "feedme.ernis.save", @extractMeal($, day) module.exports = (robot) -> @@ -115,4 +119,4 @@ module.exports = (robot) -> ernis.getMenu() fabrikMenu = robot.brain.get "feedme.fabrik.save" ernisMenu = robot.brain.get "feedme.ernis.save" - res.send "Heutiges Mittagsmenü: \nFabrik: \n#{fabrikMenu}\n\nErnis: \n#{ernisMenu}" + res.send "Heutiges Mittagsmenü: \nFabrik: \n#{fabrikMenu}\n\nErni's: \n#{ernisMenu}" From 8a929257fba991ecaef6e127890dbe0c61d720c1 Mon Sep 17 00:00:00 2001 From: Christoph Holter Date: Thu, 21 Apr 2016 17:18:10 +0200 Subject: [PATCH 7/9] change datejs to moment.js --- package.json | 2 +- scripts/feedme.coffee | 38 ++++++++++++++++---------------------- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 755880d..87dd3ff 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,6 @@ "author": "Lorenz Leutgeb ", "description": "A simple helpful robot for Catalysts", "dependencies": { - "datejs": "^1.0.0-rc3", "hubot": "^2.18.0", "hubot-diagnostics": "0.0.1", "hubot-google-images": "^0.2.6", @@ -21,6 +20,7 @@ "hubot-slack": "^3.4.2", "jquery": "^2.2.3", "jsdom": "^8.4.0", + "moment": "^2.13.0", "urllib-sync": "^1.1.2" }, "engines": { diff --git a/scripts/feedme.coffee b/scripts/feedme.coffee index e5660fa..89b2989 100644 --- a/scripts/feedme.coffee +++ b/scripts/feedme.coffee @@ -10,12 +10,8 @@ urllib = require "urllib-sync" jsdom = require "jsdom" -require "datejs" +moment = require "moment" -DAY = 1000 * 60 * 60 * 24 - -getDateDiff = (date1, date2, interval) -> - Math.round((date1.getTime() - date2.getTime()) / interval) fetch = (target) -> urllib.request(target).data @@ -45,12 +41,10 @@ class Fabrik # Ruhetag if menu.toLowerCase() is 'ruhetag' then @errors.resting else menu - getMenu: () => - now = new Date() - day = now.getDay() - lastCheck = new Date(@robot.brain.get("feedme.fabrik.lastCheck") or 0) + getMenu: (now, day) => + lastCheck = moment(@robot.brain.get("feedme.fabrik.lastCheck") or 0) # only check once per day, otherwise we're good to go - return if getDateDiff(now, lastCheck, DAY) < 0 + return if now.isSameOrBefore(lastCheck, 'day') @robot.brain.set "feedme.fabrik.lastCheck", now rawbody = fetch(@target) @@ -64,16 +58,16 @@ class Fabrik parsedDates = @parseDates($) # check for outdated menu - if now.getTime() > (Date.parseExact(parsedDates[2], "d.M.yyyy").getTime() + DAY) + if now.isAfter(moment(parsedDates[2], "DD.MM.YYYY"), 'day') @robot.brain.set "feedme.fabrik.save", @errors.menuFromPast return # check for future menu - if now.getTime() < Date.parseExact(parsedDates[1], "d.M.yyyy").getTime() + if now.isBefore(moment(parsedDates[1], "DD.MM.YYYY"), 'day') @robot.brain.set "feedme.fabrik.save", @errors.menuFromFuture return - if day == 5 or day == 6 + if day > 5 @robot.brain.set "feedme.fabrik.save", @errors.closed return @@ -89,13 +83,10 @@ class Ernis extractMeal: ($, day) -> $("#accordion > div .moduletable:eq(#{day-1}) > div").text().trim() - getMenu: () => - now = new Date() - day = now.getDay() - - lastCheck = new Date(@robot.brain.get("feedme.ernis.lastCheck") or 0) + getMenu: (now, day) => + lastCheck = moment(@robot.brain.get("feedme.ernis.lastCheck") or 0) # only check once per day, otherwise we're good to go - return if getDateDiff(now, lastCheck, DAY) < 0 + return if now.isSameOrBefore(lastCheck, 'day') @robot.brain.set "feedme.ernis.lastCheck", now rawbody = fetch(@target) @@ -103,7 +94,7 @@ class Ernis # hack to include jQuery $ = require("jquery")(jsdom.jsdom(rawbody).defaultView) # ernis closed - if day == 5 or day == 6 + if day > 5 @robot.brain.set "feedme.ernis.save", @errors.closed return @@ -114,9 +105,12 @@ module.exports = (robot) -> fabrik = new Fabrik(robot) ernis = new Ernis(robot) + now = moment() + day = now.isoWeekday() + robot.respond /feedme/i, (res) -> - fabrik.getMenu() - ernis.getMenu() + fabrik.getMenu(now, day) + ernis.getMenu(now, day) fabrikMenu = robot.brain.get "feedme.fabrik.save" ernisMenu = robot.brain.get "feedme.ernis.save" res.send "Heutiges Mittagsmenü: \nFabrik: \n#{fabrikMenu}\n\nErni's: \n#{ernisMenu}" From 0b70dcc38b04c3fadf91c710103204af56b1aeeb Mon Sep 17 00:00:00 2001 From: Christoph Holter Date: Thu, 21 Apr 2016 17:27:26 +0200 Subject: [PATCH 8/9] add comments + split long line --- scripts/feedme.coffee | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/scripts/feedme.coffee b/scripts/feedme.coffee index 89b2989..4ae7004 100644 --- a/scripts/feedme.coffee +++ b/scripts/feedme.coffee @@ -67,6 +67,7 @@ class Fabrik @robot.brain.set "feedme.fabrik.save", @errors.menuFromFuture return + # fabrik is closed if day > 5 @robot.brain.set "feedme.fabrik.save", @errors.closed return @@ -93,7 +94,7 @@ class Ernis # hack to include jQuery $ = require("jquery")(jsdom.jsdom(rawbody).defaultView) - # ernis closed + # erni's is closed if day > 5 @robot.brain.set "feedme.ernis.save", @errors.closed return @@ -106,6 +107,7 @@ module.exports = (robot) -> fabrik = new Fabrik(robot) ernis = new Ernis(robot) now = moment() + # ranging from 1 (Monday) to 7 (Sunday) day = now.isoWeekday() robot.respond /feedme/i, (res) -> @@ -113,4 +115,6 @@ module.exports = (robot) -> ernis.getMenu(now, day) fabrikMenu = robot.brain.get "feedme.fabrik.save" ernisMenu = robot.brain.get "feedme.ernis.save" - res.send "Heutiges Mittagsmenü: \nFabrik: \n#{fabrikMenu}\n\nErni's: \n#{ernisMenu}" + res.send "Heutiges Mittagsmenü:\n + \nFabrik: \n#{fabrikMenu}\n + \nErni's: \n#{ernisMenu}" From 5db42def64df34611d66fb3c07dfbc5bd298921b Mon Sep 17 00:00:00 2001 From: Christoph Holter Date: Thu, 21 Apr 2016 17:41:52 +0200 Subject: [PATCH 9/9] lastCheck only set after successful menu retrieval --- scripts/feedme.coffee | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/scripts/feedme.coffee b/scripts/feedme.coffee index 4ae7004..a4bfdba 100644 --- a/scripts/feedme.coffee +++ b/scripts/feedme.coffee @@ -46,7 +46,6 @@ class Fabrik # only check once per day, otherwise we're good to go return if now.isSameOrBefore(lastCheck, 'day') - @robot.brain.set "feedme.fabrik.lastCheck", now rawbody = fetch(@target) if @checkHoliday(rawbody.toString()) @@ -60,19 +59,18 @@ class Fabrik # check for outdated menu if now.isAfter(moment(parsedDates[2], "DD.MM.YYYY"), 'day') @robot.brain.set "feedme.fabrik.save", @errors.menuFromPast - return # check for future menu - if now.isBefore(moment(parsedDates[1], "DD.MM.YYYY"), 'day') + else if now.isBefore(moment(parsedDates[1], "DD.MM.YYYY"), 'day') @robot.brain.set "feedme.fabrik.save", @errors.menuFromFuture - return # fabrik is closed - if day > 5 + else if day > 5 @robot.brain.set "feedme.fabrik.save", @errors.closed - return - @robot.brain.set "feedme.fabrik.save", @extractMeal($, day) + else + @robot.brain.set "feedme.fabrik.save", @extractMeal($, day) + @robot.brain.set "feedme.fabrik.lastCheck", now class Ernis target : "http://www.ernis.at" @@ -89,7 +87,6 @@ class Ernis # only check once per day, otherwise we're good to go return if now.isSameOrBefore(lastCheck, 'day') - @robot.brain.set "feedme.ernis.lastCheck", now rawbody = fetch(@target) # hack to include jQuery @@ -97,9 +94,10 @@ class Ernis # erni's is closed if day > 5 @robot.brain.set "feedme.ernis.save", @errors.closed - return - @robot.brain.set "feedme.ernis.save", @extractMeal($, day) + else + @robot.brain.set "feedme.ernis.save", @extractMeal($, day) + @robot.brain.set "feedme.ernis.lastCheck", now module.exports = (robot) ->