Class: TwitterMeme::Main
- Inherits:
-
Object
- Object
- TwitterMeme::Main
- Defined in:
- lib/twitter_meme.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #client ⇒ Object
- #generate_meme(text0, text1, tweet) ⇒ Object
-
#initialize(options = {}) ⇒ Main
constructor
A new instance of Main.
- #last_id ⇒ Object
- #last_id=(last_id) ⇒ Object
- #load_config ⇒ Object
- #process! ⇒ Object
- #tweets ⇒ Object
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 = .merge!( config_file: "#{ENV['HOME']}/.twitter_meme/config.yml" ) puts "Loading config from #{[:config_file]}" load_config puts "Going to process tweets from #{[: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
#options ⇒ Object
Returns the value of attribute options.
12 13 14 |
# File 'lib/twitter_meme.rb', line 12 def @options end |
Instance Method Details
#client ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/twitter_meme.rb', line 76 def client Twitter::REST::Client.new do |config| config.consumer_key = [:twitter_consumer_key] config.consumer_secret = [:twitter_consumer_secret] config.access_token = [:twitter_access_token] config.access_token_secret = [:twitter_access_token_secret] end end |
#generate_meme(text0, text1, tweet) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/twitter_meme.rb', line 99 def generate_meme(text0, text1, tweet) puts "Generating meme with text: '#{text0}, #{text1}'" meme_image_url = Meme.new( [:imgflip_username], [:imgflip_password], [: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 ⇒ Object
115 116 117 |
# File 'lib/twitter_meme.rb', line 115 def last_id File.read("#{ENV['HOME']}/.twitter_meme/LAST_ID").chomp.to_i end |
#last_id=(last_id) ⇒ Object
119 120 121 122 123 |
# File 'lib/twitter_meme.rb', line 119 def last_id=(last_id) file = File.open("#{ENV['HOME']}/.twitter_meme/LAST_ID", 'w') file.write(last_id) file.close end |
#load_config ⇒ Object
33 34 35 |
# File 'lib/twitter_meme.rb', line 33 def load_config .merge!(Util.symbolize_keys(YAML.load_file([: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 63 64 65 66 67 68 69 70 71 72 73 74 |
# 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!' self.last_id = tweet.id # 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) 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 [:reply_to_orginal_tweet] print "Tweeting reply to #{[:twitter_username]}... " reply_tweet = client.update( "#{[:twitter_username]} #{my_tweet.url}", in_reply_to_status_id: last_id ) print "done (#{reply_tweet.url})\n" end end puts "last_id: #{last_id}" # api rate limits sleep 60 end end |
#tweets ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/twitter_meme.rb', line 85 def tweets print 'Checking for new tweets... ' tweets = client.user_timeline( [:twitter_username], since_id: last_id, count: 200, trim_user: true, exclude_replies: true ) print "done (found #{tweets.length})\n" tweets end |