Skip to content

Instantly share code, notes, and snippets.

@goldeneggg
Created March 17, 2015 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goldeneggg/2f362043240f0ac61539 to your computer and use it in GitHub Desktop.
Save goldeneggg/2f362043240f0ac61539 to your computer and use it in GitHub Desktop.
trend search by otter API
# Description:
# Trend search by otter API
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# hubot trend [window] [offset] [lang] <query> - Show otter's search result contents. window="m"(monthly),"w"(weekly),"d"(dately *default*),"h"(hourly)
#
# Notes:
# <optional notes required for the script>
#
# Author:
# goldeneggg
domainOtter = "http://otter.topsy.com"
apiKey = "09C43A9B270A470B8EB8F2946A9369F3"
module.exports = (robot) ->
robot.respond /trend(?:\s+)((a|m|w|d|h)\s+)?((0|[1-9]\d*)\s+)?(([a-z]+)\s+)?(.+)/i, (msg) ->
win = if msg.match[2]? then msg.match[2] else "d"
offset = if msg.match[4]? then msg.match[4] else 0
lang = if msg.match[6]? then "&allow_lang=#{msg.match[6]}" else ""
q = msg.match[7]
now = Math.floor(new Date())
call = now - 6500000
url = domainOtter + "/search.js?q=#{q}&window=#{win}&offset=#{offset}&perpage=10&call_timestamp=#{call}&apikey=#{apiKey}&_=#{now}" + lang
getMessage = (r) ->
"【#{r.trackback_total}】 [#{new Date(r.firstpost_date * 1000)}] #{r.content}\n tb: #{r.trackback_permalink} by @#{r.trackback_author_nick})\n"
callback = (json) ->
m = ""
for r in json.response.list
m += getMessage(r)
msg.send m
req = robot.http(url)
.header('Accept', 'application/json')
req.get() (err, res, body) ->
if err
return
status = res.statusCode
if status == 200
json = JSON.parse(body)
callback(json)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment