UNPKG

3.64 kBtext/coffeescriptView Raw
1# Description:
2# Hubot plugin for searching for Simpsons screencaps on Frinkiac
3#
4# Dependencies:
5# axios
6#
7# Configuration:
8# None
9#
10# Commands:
11# hubot simpsons me <query> | <caption override> - displays a screenshot from The Simpsons related to your search
12# hubot futurama me <query> | <caption override> - displays a screenshot from Futurama related to your search
13#
14# Notes:
15# None
16#
17# Author:
18# None
19
20require('es6-promise').polyfill()
21axios = require('axios')
22
23franchiseServices = {
24 "simpsons": "frinkiac.com",
25 "futurama": "morbotron.com",
26}
27
28getRequestConfig = (franchise, endpoint, params) ->
29 apiBaseUrl = getFranchiseServiceUrl(franchise)
30 searchUrl = "#{apiBaseUrl}/api/search"
31 captionUrl = "#{apiBaseUrl}/api/caption"
32 url = if endpoint is 'search' then searchUrl else captionUrl
33 return {
34 method: 'get'
35 url: url
36 params: params
37 }
38
39encode = (str) ->
40 encodeURIComponent(str).replace /[!'()*]/g, (c) ->
41 '%' + c.charCodeAt(0).toString(16)
42
43# we append '#.jpg' to url string because some chat clients (eg. hip chat)
44# will not exapand images if they don't end in an image extension
45getImageUrl = (franchise, episode, timestamp, caption) ->
46 apiBaseUrl = getFranchiseServiceUrl(franchise)
47 "#{apiBaseUrl}/meme/#{episode}/#{timestamp}.jpg?lines=#{encode(caption)}#.jpg"
48
49getFranchiseServiceUrl = (franchise) ->
50 if !franchise of franchiseServices then franchise = "simpsons"
51 return "https://#{franchiseServices[franchise]}"
52
53getLongestWordLength = (words) ->
54 longestWordLength = 0
55 words.forEach (word) ->
56 longestWordLength = word.length if word.length > longestWordLength
57 longestWordLength
58
59getNumberOfWordsBeforeBreaking = (words) ->
60 longestWordLength = getLongestWordLength(words)
61 if longestWordLength <= 5
62 wordsBeforeBreaking = 5
63 else if 5 < longestWordLength <= 8
64 wordsBeforeBreaking = 4
65 else
66 wordsBeforeBreaking = 3
67 wordsBeforeBreaking
68
69addLineBreaks = (str) ->
70 newString = ''
71 words = str.split(' ')
72 wordsBeforeBreaking = getNumberOfWordsBeforeBreaking(words)
73 words.forEach (word, i) ->
74 i++
75 delimiter = if i % wordsBeforeBreaking then ' ' else '\n'
76 newString += word + delimiter
77 newString
78
79trimWhitespace = (string) ->
80 string.replace /^\s*|\s*$/g, ''
81
82formatCaption = (caption) ->
83 addLineBreaks(trimWhitespace(caption))
84
85combineCaptions = (captions) ->
86 if captions.length <= 4
87 newCaption = ''
88 captions.forEach (caption, i) ->
89 newCaption += formatCaption(caption.Content)
90 unless i == (captions.length - 1)
91 newCaption += '\n'
92 newCaption
93 else
94 captions[0].Content
95
96module.exports = (robot) ->
97 robot.respond /((simpsons|futurama) (search|me)|frinkiac) (.*)/i, (msg) ->
98 franchise = msg.match[2] || "simpsons"
99 query = msg.match[4].split('|')
100 customCaption = query[1]
101
102 axios(getRequestConfig(franchise, 'search', {q: query[0]}))
103 .then (response) ->
104 if (response.data.length)
105 frame = Math.floor(Math.random() * response.data.length)
106 episode = response.data[frame].Episode
107 timestamp = response.data[frame].Timestamp
108
109 if customCaption
110 msg.send getImageUrl(franchise, episode, timestamp, formatCaption(customCaption))
111
112 else
113 axios(getRequestConfig(franchise, 'caption', {e: episode, t: timestamp}))
114 .then (response) ->
115 msg.send getImageUrl(franchise, episode, timestamp, combineCaptions(response.data.Subtitles))
116
117 else
118 console.log("D'oh! I couldn't find anything for `#{query[0]}`.");
119
120 .catch (error) ->
121 console.error(error);