1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | require('es6-promise').polyfill()
|
21 | axios = require('axios')
|
22 |
|
23 | franchiseServices = {
|
24 | "simpsons": "frinkiac.com",
|
25 | "futurama": "morbotron.com",
|
26 | }
|
27 |
|
28 | getRequestConfig = (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 |
|
39 | encode = (str) ->
|
40 | encodeURIComponent(str).replace /[!'()*]/g, (c) ->
|
41 | '%' + c.charCodeAt(0).toString(16)
|
42 |
|
43 |
|
44 |
|
45 | getImageUrl = (franchise, episode, timestamp, caption) ->
|
46 | apiBaseUrl = getFranchiseServiceUrl(franchise)
|
47 | "#{apiBaseUrl}/meme/#{episode}/#{timestamp}.jpg?lines=#{encode(caption)}#.jpg"
|
48 |
|
49 | getFranchiseServiceUrl = (franchise) ->
|
50 | if !franchise of franchiseServices then franchise = "simpsons"
|
51 | return "https://#{franchiseServices[franchise]}"
|
52 |
|
53 | getLongestWordLength = (words) ->
|
54 | longestWordLength = 0
|
55 | words.forEach (word) ->
|
56 | longestWordLength = word.length if word.length > longestWordLength
|
57 | longestWordLength
|
58 |
|
59 | getNumberOfWordsBeforeBreaking = (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 |
|
69 | addLineBreaks = (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 |
|
79 | trimWhitespace = (string) ->
|
80 | string.replace /^\s*|\s*$/g, ''
|
81 |
|
82 | formatCaption = (caption) ->
|
83 | addLineBreaks(trimWhitespace(caption))
|
84 |
|
85 | combineCaptions = (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 |
|
96 | module.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);
|