forked from github/hubot-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aww.coffee
44 lines (36 loc) · 1.02 KB
/
aww.coffee
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
42
43
44
# Description:
# Hubot delivers a pic from Reddit's /r/aww frontpage
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# hubot aww - Display the picture from /r/aww
#
# Author:
# eliperkins
url = require("url")
module.exports = (robot) ->
robot.respond /aww/i, (msg) ->
search = escape(msg.match[1])
msg.http('http://www.reddit.com/r/aww.json')
.get() (err, res, body) ->
result = JSON.parse(body)
urls = [ ]
for child in result.data.children
if child.data.domain != "self.aww"
urls.push(child.data.url)
if urls.count <= 0
msg.send "Couldn't find anything cute..."
return
rnd = Math.floor(Math.random()*urls.length)
picked_url = urls[rnd]
parsed_url = url.parse(picked_url)
if parsed_url.host == "imgur.com"
parsed_url.host = "i.imgur.com"
parsed_url.pathname = parsed_url.pathname + ".jpg"
picked_url = url.format(parsed_url)
msg.send picked_url