Skip to content

Instantly share code, notes, and snippets.

@kakajika
Last active August 29, 2015 14:12
Show Gist options
  • Save kakajika/c1a1c9896717a9090eb6 to your computer and use it in GitHub Desktop.
Save kakajika/c1a1c9896717a9090eb6 to your computer and use it in GitHub Desktop.
Nexus6 release checker for Hubot.
# nexus6.coffee
#
# Description:
# Nexus6 release checker for Hubot.
#
# Commands:
# hubot nexus6 - Nexus6の出荷状態を確認します.
# hubot nexus6 [white|blue] - 色を指定してNexus6の出荷状態を確認します.
cron = require('cron').CronJob
client = require 'cheerio-httpcli'
class ShippingStatusChecker
constructor: ->
check: (url, callback) ->
client.fetch url, (err, $, res) ->
if err
return console.log(err)
status = $('.shipping-status')
resultText = "#{status.text()}"
available = !(status.find('*').hasClass('not-available'))
callback(resultText, available)
class CheckerBot extends ShippingStatusChecker
constructor: (@robot, @url, @name, @key) ->
respond: (msg) ->
@check @url, (resultText) =>
msg.send "#{@name} #{resultText} #{@url}"
crawl: ->
@check @url, (resultText, available) =>
if resultText != @robot.brain.get(@key)
@robot.brain.set(@key, resultText)
room = if available then "general" else "sandbox"
sendParam = {room: "#{room}@#{process.env.HUBOT_XMPP_HOST}"}
@robot.send sendParam, "#{@name} #{resultText} #{@url}"
module.exports = (robot) ->
nameBlue = "Nexus6(32GB, ダークブルー)"
nameWhite = "Nexus6(32GB, クラウドホワイト)"
urlBlue = "https://play.google.com/store/devices/details?id=nexus_6_blue_32gb"
urlWhite = "https://play.google.com/store/devices/details?id=nexus_6_white_32gb"
checkerBlue = new CheckerBot(robot, urlBlue, nameBlue, "resultTextBlue")
checkerWhite = new CheckerBot(robot, urlWhite, nameWhite, "resultTextWhite")
robot.respond /nexus6$/i, (msg) ->
checkerBlue.respond msg
checkerWhite.respond msg
robot.respond /nexus6 blue/i, (msg) ->
checkerBlue.respond msg
robot.respond /nexus6 white/i, (msg) ->
checkerWhite.respond msg
new cron '*/5 * * * *', () ->
checkerBlue.crawl()
checkerWhite.crawl()
, null, true, "Asia/Tokyo"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment