/
meme.rb
221 lines (171 loc) · 6.21 KB
/
meme.rb
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
require 'net/http'
require 'rubygems'
require 'nokogiri'
require 'cgi'
##
# Generate memes using http://memegenerator.net
class Meme
##
# Sometimes your meme will have an error, fix it!
class Error < RuntimeError; end
##
# Every meme generator needs a version
VERSION = '1.7.1'
##
# For statistics!
USER_AGENT = "meme/#{VERSION} Ruby/#{RUBY_VERSION}"
##
# We have some generators up-in-here
GENERATORS = Hash.new do |_, k|
raise Error, "unknown generator #{k}"
end
##
# For creating advice-dog type meme images.
#
# These can accept up to two lines of text
def self.advice_dog name, id, template_name, first_line = nil
template = [id, 'AdviceDogSpinoff', template_name, first_line]
template.compact
GENERATORS[name] = template
end
##
# For creating vertical type meme images
#
# These can accept multiple lines of text
def self.vertical name, id, template_name
GENERATORS[name] = [id, 'Vertical', template_name]
end
# keep generators in alphabetical order
advice_dog 'ANTEATER', 41191, 'anteater'
advice_dog 'A_DODSON', 106375, 'Antoine-Dodson'
advice_dog 'A_DOG', 940, 'Advice-Dog'
advice_dog 'A_FATHER', 1436, 'High-Expectations-Asian-Father'
advice_dog 'BEAR-GRYLLS', 89714, 'Bear-Grylls'
advice_dog 'BUTTHURT_DWELLER', 1438, 'Butthurt-Dweller'
advice_dog 'B_FROG', 1211, 'Foul-Bachelorette-Frog'
advice_dog 'B_FROG2', 1045, 'Foul-Bachelor-Frog'
advice_dog 'COOL_STORY_HOUSE', 16948, 'cool-story-bro-house'
advice_dog 'CREEPER', 173501, 'Minecraft-Creeper'
advice_dog 'C_WOLF', 931, 'Courage-Wolf'
advice_dog 'F_FRY', 84688, 'Futurama-Fry'
advice_dog 'G_GRANDPA', 185650, 'Grumpy-Grandpa'
advice_dog 'H_MERMAID', 405224, 'Hipster-Mermaid'
advice_dog 'I_DONT_ALWAYS', 38926, 'The-Most-Interesting-Man-in-the-World'
advice_dog 'I_WOLF', 926, 'Insanity-Wolf'
advice_dog 'J_DUCREUX', 1356, 'Joseph-Ducreux'
advice_dog 'KEANU', 47718, 'Keanu-reeves'
advice_dog 'MINECRAFT', 122309, 'Minecraft'
advice_dog 'O-RLY-OWL', 117041, 'O-RLY-OWL', 'ORLY???'
advice_dog 'OBAMA', 1332, 'Obama-'
advice_dog 'PHILOSORAPTOR', 984, 'Philosoraptor'
advice_dog 'P_OAK', 24321, 'Professor-Oak'
advice_dog 'SCUMBAG', 364688, 'Scumbag-Steve'
advice_dog 'SERIOUS_FISH', 6374627,'Spongebob-Serious-Fish'
advice_dog 'SPARTA', 1013, 'sparta'
advice_dog 'SPIDERMAN', 1037, 'Question-Spiderman'
advice_dog 'SWEDISH_CHEF', 186651, 'Swedish-Chef'
advice_dog 'S_AWKWARD_PENGUIN', 983, 'Socially-Awkward-Penguin'
advice_dog 'TOWNCRIER', 434537, 'Towncrier'
advice_dog 'TROLLFACE', 1030, 'Troll-Face'
advice_dog 'UNICORN_BOY', 57022, 'unicorn-boy'
advice_dog 'US_POINT', 131083, 'Uncle-Sam-Point', 'I WANT YOU'
advice_dog 'V_BABY', 11140, 'Victory-Baby'
advice_dog 'XZIBIT', 3114, 'XZIBIT'
advice_dog 'Y_U_NO', 165241, 'Y-U-NO', 'Y U NO'
vertical 'INCEPTION', 107949, 'Inception'
# keep generators in alphabetical order
##
# Looks up generator name
def GENERATORS.match(name)
# TODO meme Y U NO DEMETAPHONE?
return self[name] if has_key? name
matcher = Regexp.new(name, Regexp::IGNORECASE)
_, generator = find { |k,v| matcher =~ k || v.grep(matcher).any? }
generator || self[name] # raises the error if generator is nil
end
##
# Interface for the executable
def self.run argv = ARGV
generator = ARGV.shift
if generator == '--list' then
width = GENERATORS.keys.map { |command| command.length }.max
GENERATORS.sort.each do |command, (id, name, _)|
puts "%-*s %s" % [width, command, name]
end
exit
end
abort "#{$0} [GENERATOR|--list] LINE [ADDITONAL_LINES]" if ARGV.empty?
meme = new generator
link = meme.generate *ARGV
meme.paste link
if $stdout.tty?
puts link
else
puts meme.fetch link
end
link
rescue Interrupt
exit
rescue SystemExit
raise
rescue Exception => e
abort "ERROR: #{e.message} (#{e.class})"
end
##
# Generates links for +generator+
def initialize generator
@template_id, @template_type, @generator_name, @default_line = GENERATORS.match generator
end
##
# Generates a meme with +line1+ and +line2+. For some generators you only
# have to supply one line because the first line is defaulted for you.
# Isn't that great?
def generate *args
url = URI.parse 'http://memegenerator.net/Instance/CreateOrEdit'
res = nil
location = nil
# Put the default line in front unless theres more than 1 text input.
args.unshift(@default_line) unless args.size > 1
raise Error, "two lines are required for #{@generator_name}" unless args.size > 1
post_data = { 'templateType' => @template_type,
'templateID' => @template_id,
'generatorName' => @generator_name }
# go through each argument and add it back into the post data as textN
(0..args.size).map {|num| post_data.merge! "text#{num}" => args[num] }
Net::HTTP.start url.host do |http|
post = Net::HTTP::Post.new url.path
post['User-Agent'] = USER_AGENT
post.set_form_data post_data
res = http.request post
location = res['Location']
redirect = url + location
get = Net::HTTP::Get.new redirect.request_uri
get['User-Agent'] = USER_AGENT
res = http.request get
end
doc = Nokogiri.HTML res.body
doc.css("a[href=\"#{location}\"] img").first['src']
end
def fetch link
url = URI.parse link
res = nil
Net::HTTP.start url.host do |http|
get = Net::HTTP::Get.new url.request_uri
get['User-Agent'] = USER_AGENT
res = http.request get
end
res.body
end
##
# Tries to find clipboard copy executable and if found puts +link+ in your
# clipboard.
def paste link
clipboard = %w{
/usr/bin/pbcopy
/usr/bin/xclip
}.find { |path| File.exist? path }
if clipboard
IO.popen clipboard, 'w' do |io| io.write link end
end
end
end