Gohan
2/25/2012 - 4:34 PM

hubot_script douban movie

hubot_script douban movie

# hello hubot, another example

showResult = (msg, entries, images) ->
  if entries.length == 0
    return
  msg.send entries.shift()
  msg.send images.shift()
  setTimeout ( ->
    showResult msg, entries, images
  ), 500

module.exports = (robot) ->
  robot.respond /(查)?(豆瓣电影) (.*)/i, (msg) ->
    msg
      .http('http://api.douban.com/movie/subjects?alt=json&q=' + msg.match[3])
      .get() (err, res, body) ->
        if err
          msg.send '错误' + err
          return 0
        
        @output = []
        @output.push '搜索关键字为' + msg.match[3] + '的电影'
        result = JSON.parse(body)
        total_count = result['opensearch:totalResults']['$t']
        item_count = result.entry.length
        @output.push '找到' + total_count + '结果, ' + '显示' + item_count + '个.'

        msg.send @output.join('\n')
        @entries = []
        @images = []
        @count = 1
        for movie in result.entry
          @output = []
          @output.push ''+@count+'. ' + movie.title['$t']
          @output.push '  Link:' + movie.link[1]['@href']
          @output.push '  Image:'
          @entries.push @output.join('\n')
          @images.push movie.link[2]['@href']
          @count=@count+1
        showResult msg, @entries, @images