diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..13c179d --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*.rbc +*.swp +/TAGS +/doc +/pkg +/tmp +.DS_Store diff --git a/History.txt b/History.txt new file mode 100644 index 0000000..243220d --- /dev/null +++ b/History.txt @@ -0,0 +1,6 @@ +=== 1.0.0 / 2011-02-08 + +* 1 major enhancement + + * Birthday! + diff --git a/Manifest.txt b/Manifest.txt new file mode 100644 index 0000000..ae1542a --- /dev/null +++ b/Manifest.txt @@ -0,0 +1,6 @@ +History.txt +Manifest.txt +README.txt +Rakefile +bin/meme +lib/meme.rb diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..f2847b0 --- /dev/null +++ b/README.txt @@ -0,0 +1,62 @@ += meme + +* http://github.com/drbrain/meme +* http://docs.seattlerb.org/meme + +== DESCRIPTION: + +Generate meme images using http://memegenerator.net! Save yourself some time! + +== FEATURES/PROBLEMS: + +* Only has a static list of three meme images +* No tests + +== SYNOPSIS: + + meme Y-U-NO 'write tests?' + +You can also drive it like an API. + +== REQUIREMENTS: + +* nokogiri +* internet connection + +== INSTALL: + + gem install meme + +== DEVELOPERS: + +After checking out the source, run: + + $ rake newb + +This task will install any missing dependencies, run the tests/specs, +and generate the RDoc. + +== LICENSE: + +(The MIT License) + +Copyright (c) 2011 Eric Hodel + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +'Software'), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..c7b9322 --- /dev/null +++ b/Rakefile @@ -0,0 +1,18 @@ +# -*- ruby -*- + +require 'rubygems' +require 'hoe' + +Hoe.plugin :git + +Hoe.spec 'meme' do + developer 'Eric Hodel', 'drbrain@segment7.net' + + rdoc_locations << 'docs.seattlerb.org:/data/www/docs.seattlerb.org/meme/' + + extra_deps << ['nokogiri', '~> 1.4'] + + required_ruby_version = '>= 1.9.2' +end + +# vim: syntax=ruby diff --git a/bin/meme b/bin/meme new file mode 100644 index 0000000..058b2d4 --- /dev/null +++ b/bin/meme @@ -0,0 +1,6 @@ +#!/usr/local/bin/ruby19 + +require 'meme' + +Meme.run ARGV + diff --git a/lib/meme.rb b/lib/meme.rb new file mode 100644 index 0000000..549040d --- /dev/null +++ b/lib/meme.rb @@ -0,0 +1,122 @@ +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.0' + + ## + # 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 + + GENERATORS['Y_U_NO'] = [165241, 'Y-U-NO', 'Y U NO'] + GENERATORS['B_FROG'] = [1211, 'Foul-Bachelorette-Frog'] + GENERATORS['PHILOSORAPTOR'] = [984, 'Philosoraptor'] + + ## + # 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.each do |command, (id, name, _)| + puts "%-*s %s" % [width, command, name] + end + + exit + end + + line1 = ARGV.shift + line2 = ARGV.shift + + abort "#{$0} GENERATOR LINE [LINE]" unless line1 + + meme = new generator + link = meme.generate line1, line2 + + meme.paste link + + puts link + end + + ## + # Generates links for +generator+ + + def initialize generator + @template_id, @generator_name, @default_line = GENERATORS[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 line1, line2 = nil + url = URI.parse 'http://memegenerator.net/Instance/CreateOrEdit' + res = nil + location = nil + + unless line2 then + line2 = line1 + line1 = @default_line + end + + raise Error, "must supply both lines for #{generator_name}" unless line1 + + Net::HTTP.start url.host do |http| + post = Net::HTTP::Post.new url.path + post['User-Agent'] = USER_AGENT + post.set_form_data('templateType' => 'AdviceDogSpinoff', + 'text0' => line1, + 'text1' => line2, + 'templateID' => @template_id, + 'generatorName' => @generator_name) + + 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 + + ## + # Puts +link+ in your clipboard, if you're on OS X, that is. + + def paste link + IO.popen 'pbcopy', 'w' do |io| io.write link end + end + +end +