-
Notifications
You must be signed in to change notification settings - Fork 3
Feature/feedme #1
Changes from 5 commits
8995250
4321cab
9fea295
e62351d
32642bf
acca101
8a92925
0b70dcc
5db42de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,4 @@ | |
"hubot-redis-brain", | ||
"hubot-rules", | ||
"hubot-shipit" | ||
] | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
# Description: | ||
# Shows what"s edible. | ||
# | ||
# Notes: | ||
# This includes | ||
# * fetching from fabrik | ||
# * fetching from ernis | ||
# | ||
# Author: cholter | ||
|
||
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) | ||
|
||
fetch = (target) -> | ||
return urllib.request(target).data | ||
|
||
|
||
class Fabrik | ||
target : "http://www.diefabrik.co.at/mittagsmenue/index.html" | ||
holidayMagic : "urlaub" | ||
errors: | ||
closed : "fabrik is closed" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Formatting of lines 28 through 32 is inconsistent with formatting of line 25 and 26. |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
parseDates: ($) -> | ||
docDate = $("h2:eq(0)").html() | ||
return /(\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' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be |
||
# all good :) | ||
return menu | ||
|
||
getMenu: () => | ||
now = new Date() | ||
day = now.getDay() | ||
lastCheck = new Date(@robot.brain.get "feedme.fabrik.lastCheck" or 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
# only check once per day, otherwise we're good to go | ||
if getDateDiff(now, lastCheck, DAY) > 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think an early return would be nice here. All the code below would need one level of indentation less. |
||
rawbody = fetch(@target) | ||
|
||
return @errors.holiday if @checkHoliday(rawbody.toString()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
|
||
# 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 future menu | ||
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($, 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() | ||
|
||
getMenu: () => | ||
now = new Date() | ||
day = now.getDay() | ||
|
||
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) | ||
|
||
# hack to include jQuery | ||
$ = require("jquery")(jsdom.jsdom(rawbody).defaultView) | ||
# ernis closed | ||
if day > 0 & day < 6 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure about |
||
@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.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}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Depends on a release candidate. Scary. Needs justification.