Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 42 lines (40 sloc) 1.475 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
# Description:
# Pulls a movie gif from the best tumblog
#
# Dependencies:
# None
#
# Configuration:
# HUBOT_TUMBLR_API_KEY - A Tumblr OAuth Consumer Key will work fine
#
# Commands:
# hubot movie me - Displays a moving still from IWDRM
#
# Author:
# iangreenleaf

module.exports = (robot) ->
  robot.respond /(movie|iwdrm)( me)?( .*)/i, (msg) ->
    tumblr_request = (offset, success) ->
      params = { api_key: process.env.HUBOT_TUMBLR_API_KEY, limit: 1, offset: offset }
      msg.http('http://api.tumblr.com/v2/blog/iwdrm.tumblr.com/posts/photo')
        .query(params)
        .get() (err, res, body) ->
          if err
            robot.logger.error err
          else if res.statusCode != 200
            robot.logger.error "Received status code #{res.statusCode}."
          else
            success JSON.parse(body)
    clean_quotes = (text) ->
      for entity,replacement of { "&#822[01];": '"', "’": "'", "…": "--" }
        text = text.replace RegExp(entity, "g"), replacement
      text
    tumblr_request 0, (data) ->
      total_posts = data.response.total_posts
      offset = Math.round((total_posts - 1) * Math.random())
      tumblr_request offset, (data) ->
        post = data.response.posts.pop()
        msg.send post.photos.pop().original_size.url
        quote = clean_quotes /<i>(.*?)<\/i>/.exec(post.caption)[1]
        title = /<a [^>]*imdb.com[^>]*>(.*?)<\/a>/.exec(post.caption)[1]
        msg.send "#{quote} #{title}"
Something went wrong with that request. Please try again.