class TwitterMeme::Main

Attributes

options[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/twitter_meme.rb, line 14
def initialize(options = {})
  @options = options.merge!(
    config_file: "#{ENV['HOME']}/.twitter_meme/config.yml"
  )
  puts "Loading config from #{options[:config_file]}"
  load_config

  puts "Going to process tweets from #{options[:twitter_username]}"

  begin
    Dir.mkdir("#{ENV['HOME']}/.twitter_meme/tmp")
  rescue Errno::EEXIST
  end

  FileUtils.touch("#{ENV['HOME']}/.twitter_meme/LAST_ID")

  self.last_id = 1 if last_id.zero?
end

Public Instance Methods

client() click to toggle source
# File lib/twitter_meme.rb, line 86
def client
  Twitter::REST::Client.new do |config|
    config.consumer_key        = options[:twitter_consumer_key]
    config.consumer_secret     = options[:twitter_consumer_secret]
    config.access_token        = options[:twitter_access_token]
    config.access_token_secret = options[:twitter_access_token_secret]
  end
end
generate_meme(text0, text1, tweet) click to toggle source
# File lib/twitter_meme.rb, line 109
def generate_meme(text0, text1, tweet)
  puts "Generating meme with text: '#{text0}, #{text1}'"
  meme_image_url = Meme.new(
    options[:imgflip_username],
    options[:imgflip_password],
    options[:imgflip_template_id]
  ).generate_meme(text0, text1)

  print "Downloading meme image #{meme_image_url}... "
  tmp_file_name = "#{ENV['HOME']}/.twitter_meme/tmp/meme_#{tweet.id}.jpg"
  File.write(tmp_file_name, Net::HTTP.get(URI.parse(meme_image_url)))
  print "done\n"

  tmp_file_name
end
last_id() click to toggle source
# File lib/twitter_meme.rb, line 125
def last_id
  File.read("#{ENV['HOME']}/.twitter_meme/LAST_ID").chomp.to_i
end
last_id=(last_id) click to toggle source
# File lib/twitter_meme.rb, line 129
def last_id=(last_id)
  file = File.open("#{ENV['HOME']}/.twitter_meme/LAST_ID", 'w')
  file.write(last_id)
  file.close
end
load_config() click to toggle source
# File lib/twitter_meme.rb, line 33
def load_config
  options.merge!(Util.symbolize_keys(YAML.load_file(options[:config_file])))
end
process!() click to toggle source
# File lib/twitter_meme.rb, line 37
def process!
  loop do
    # hot reload config between sleeps
    load_config

    tweets.reverse.each do |tweet|
      puts 'Going to tweet!'

      # use full_text here so we don't get a truncated twitter link
      text0, text1 = Util.split_text(CGI.unescapeHTML(tweet.full_text))
      next unless text0
      text1 ||= ''

      meme_file = generate_meme(text0, text1, tweet)

      send_tweet(tweet, meme_file)

      FileUtils.rm(meme_file)
    end

    puts "last_id: #{last_id}"

    # api rate limits
    sleep 60
  end
end
send_tweet(tweet, meme_file) click to toggle source
# File lib/twitter_meme.rb, line 64
def send_tweet(tweet, meme_file)
  self.last_id = tweet.id

  print 'Tweeting to main account... '
  # my_tweet = client.update_with_media(nil, File.new(meme_file))
  my_tweet = client.update_with_media(CGI.unescapeHTML(tweet.text), File.new(meme_file))
  print "done (#{my_tweet.url})\n"

  if options[:reply_to_orginal_tweet]
    print "Tweeting reply to #{options[:twitter_username]}... "
    reply_tweet = client.update(
      "#{options[:twitter_username]} #{my_tweet.url}",
      in_reply_to_status_id: last_id
    )
    print "done (#{reply_tweet.url})\n"
  end
rescue => e
  puts "Hit a snag, retrying... (#{e.message}) "
  sleep 60
  retry
end
tweets() click to toggle source
# File lib/twitter_meme.rb, line 95
def tweets
  print 'Checking for new tweets... '
  tweets = client.user_timeline(
    options[:twitter_username],
    since_id:        last_id,
    count:           200,
    trim_user:       true,
    exclude_replies: true
  )
  print "done (found #{tweets.length})\n"

  tweets
end