Skip to content
This repository has been archived by the owner on Mar 12, 2019. It is now read-only.

Feature/feedme #1

Merged
merged 9 commits into from
Apr 21, 2016
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion external-scripts.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
"hubot-redis-brain",
"hubot-rules",
"hubot-shipit"
]
]
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "hubot",
"version": "0.0.0",
"version": "0.1.0",
"private": true,
"author": "Lorenz Leutgeb <lorenz.leutgeb@catalysts.cc>",
"description": "A simple helpful robot for Catalysts",
"dependencies": {
"datejs": "^1.0.0-rc3",
Copy link
Contributor

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.

"hubot": "^2.18.0",
"hubot-diagnostics": "0.0.1",
"hubot-google-images": "^0.2.6",
Expand All @@ -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"
}
}
118 changes: 118 additions & 0 deletions scripts/feedme.coffee
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"
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return can be omitted.


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'
Copy link
Contributor

@lorenzleutgeb lorenzleutgeb Apr 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be if menu.toLowerCase() is 'ruhetag' then @errors.resting else menu without any return.

# all good :)
return menu

getMenu: () =>
now = new Date()
day = now.getDay()
lastCheck = new Date(@robot.brain.get "feedme.fabrik.lastCheck" or 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The or operator takes precedence over function calls, so this is missing parentheses.

# only check once per day, otherwise we're good to go
if getDateDiff(now, lastCheck, DAY) > 0
Copy link
Contributor

Choose a reason for hiding this comment

The 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())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think return can be omitted here as well.


# 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about & again, and is safe.

@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}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be "...Erni's...".