forked from hubot-archive/hubot-pugme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpugme.coffee
96 lines (76 loc) · 2.51 KB
/
pugme.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Description:
# Pugme is the most important thing in life
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# hubot pug me - Receive a pug
# hubot pug bomb N - Get N pugs
module.exports = (robot) ->
robot.respond /pug me|pug bomb( (\d+))?/i, (msg) ->
count = msg.match[2]
if not count
count = if (msg.match.input.match /bomb/i)? then 5 else 1
robot.http("https://www.reddit.com/r/pugs.json?sort=top&t=week")
.get() (error, response, body) ->
if error?
robot.logger.error(error)
msg.reply "I'm brain damaged :("
return
if response.statusCode >= 400
txt = "[#{response.statusCode}] #{response.statusMessage}"
robot.logger.error(txt)
msg.reply "I'm brain damaged :("
return
try
pugs = getPugs(body, count)
catch error
robot.logger.error "[pugme] #{error}"
msg.reply "I'm brain damaged :("
return
msg.reply pug for pug in pugs
imgurAlbumRegex = /imgur\.com\/a\//i
imgurDirectLinkRegex = /i\.imgur\.com/i
imgurReplaceRegex = /\.imgur\.com\/(.+)/i
imageRegex = /^(https?:\/\/.+\/(.+)\.(jpg|png|gif|jpeg$))/
getPugs = (response, n) ->
try
posts = JSON.parse response
catch error
throw new Error "JSON parse failed"
unless posts.data?.children? && posts.data.children.length > 0
throw new Error "Could not find any posts"
imagePosts = posts.data.children.filter((child) -> not child.data.is_self)
# filter out imgur album links
imagePosts = imagePosts.filter((imagePost) ->
return not imgurAlbumRegex.test(imagePost.data.url))
# convert non-direct image links on imgur to direct links.
for imagePost in imagePosts
if not imgurDirectLinkRegex.test(imagePost.data.url)
imagePost.data.url =
imagePost.data.url.replace(imgurReplaceRegex, "i.imgur.com/$1.jpg")
# remove all other posts that are not images.
imagePosts = imagePosts.filter((imagePost) ->
return imageRegex.test(imagePost.data.url))
if n > imagePosts.length
n = imagePosts.length
return (imagePost.data.url for imagePost in (sample(imagePosts, n)))
sample = (array, n) ->
if not Array.isArray(array)
throw new Error("Argument 'array' is not an array.")
if n >= array.length
return array
items = []
if n <= 0
return items
for [1..n]
index = Math.floor(Math.random()*array.length)
item = array[index]
lastItem = array.pop()
array[index] = lastItem
items.push(item)
return items