Class: TwitterMeme::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_meme.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Main

Returns a new instance of Main.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



12
13
14
# File 'lib/twitter_meme.rb', line 12

def options
  @options
end

Instance Method Details

#clientObject



86
87
88
89
90
91
92
93
# 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) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# 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_idObject



125
126
127
# 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) ⇒ Object



129
130
131
132
133
# 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_configObject



33
34
35
# File 'lib/twitter_meme.rb', line 33

def load_config
  options.merge!(Util.symbolize_keys(YAML.load_file(options[:config_file])))
end

#process!Object



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
# 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) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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

#tweetsObject



95
96
97
98
99
100
101
102
103
104
105
106
107
# 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