add twitter-posting script that has been running for a while
closes #124
This commit is contained in:
parent
479c02415e
commit
91b3fe77fe
50
extras/twitter.rb
Normal file
50
extras/twitter.rb
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
class Twitter
|
||||||
|
cattr_accessor :CONSUMER_KEY, :CONSUMER_SECRET, :AUTH_TOKEN, :AUTH_SECRET
|
||||||
|
|
||||||
|
# these need to be overridden in config/initializers/production.rb
|
||||||
|
@@CONSUMER_KEY = nil
|
||||||
|
@@CONSUMER_SECRET = nil
|
||||||
|
@@AUTH_TOKEN = nil
|
||||||
|
@@AUTH_SECRET = nil
|
||||||
|
|
||||||
|
MAX_TWEET_LEN = 140
|
||||||
|
|
||||||
|
# https://t.co/eyW1U2HLtP
|
||||||
|
TCO_LEN = 23
|
||||||
|
|
||||||
|
def self.oauth_consumer
|
||||||
|
OAuth::Consumer.new(self.CONSUMER_KEY, self.CONSUMER_SECRET,
|
||||||
|
{ :site => "https://api.twitter.com" })
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.oauth_request(req, method = :get, post_data = nil)
|
||||||
|
if !self.AUTH_TOKEN
|
||||||
|
raise "no auth token configured"
|
||||||
|
end
|
||||||
|
|
||||||
|
begin
|
||||||
|
Timeout.timeout(120) do
|
||||||
|
at = OAuth::AccessToken.new(self.oauth_consumer, self.AUTH_TOKEN,
|
||||||
|
self.AUTH_SECRET)
|
||||||
|
|
||||||
|
if method == :get
|
||||||
|
res = at.get(req)
|
||||||
|
elsif method == :post
|
||||||
|
res = at.post(req, post_data)
|
||||||
|
else
|
||||||
|
raise "what kind of method is #{method}?"
|
||||||
|
end
|
||||||
|
|
||||||
|
if res.class == Net::HTTPUnauthorized
|
||||||
|
raise "not authorized"
|
||||||
|
end
|
||||||
|
|
||||||
|
if res.body.to_s == ""
|
||||||
|
raise res.inspect
|
||||||
|
else
|
||||||
|
return JSON.parse(res.body)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
54
script/post_to_twitter
Executable file
54
script/post_to_twitter
Executable file
|
@ -0,0 +1,54 @@
|
||||||
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
ENV["RAILS_ENV"] ||= "production"
|
||||||
|
|
||||||
|
APP_PATH = File.expand_path('../../config/application', __FILE__)
|
||||||
|
require File.expand_path('../../config/boot', __FILE__)
|
||||||
|
require APP_PATH
|
||||||
|
Rails.application.require_environment!
|
||||||
|
|
||||||
|
LAST_STORY_KEY = "twitter:last_story_id"
|
||||||
|
|
||||||
|
last_story_id = (Keystore.value_for(LAST_STORY_KEY) || Story.last.id).to_i
|
||||||
|
|
||||||
|
Story.where("id > ? AND is_expired = ?", last_story_id, false).
|
||||||
|
order(:id).each do |s|
|
||||||
|
if !s.tags.map(&:tag).include?("meta")
|
||||||
|
status = "\n" + (s.url.present?? s.url + "\n" : "") + s.short_id_url
|
||||||
|
|
||||||
|
tco_status = "\n" + (s.url.present?? ("X" * Twitter::TCO_LEN) + "\n" :
|
||||||
|
"") + ("X" * Twitter::TCO_LEN)
|
||||||
|
|
||||||
|
left_len = Twitter::MAX_TWEET_LEN - tco_status.length
|
||||||
|
|
||||||
|
title = s.title
|
||||||
|
|
||||||
|
if title.match(/^([dm] |@)/i)
|
||||||
|
# prevent these tweets from activating twitter shortcuts
|
||||||
|
# https://dev.twitter.com/docs/faq#tweeting
|
||||||
|
title = "- #{title}"
|
||||||
|
end
|
||||||
|
|
||||||
|
if title.bytesize > left_len
|
||||||
|
status = title[0, left_len - 3] + "..." + status
|
||||||
|
else
|
||||||
|
status = title + status
|
||||||
|
end
|
||||||
|
|
||||||
|
res = Twitter.oauth_request("/1.1/statuses/update.json", :post,
|
||||||
|
{ "status" => status })
|
||||||
|
|
||||||
|
begin
|
||||||
|
if !res["id_str"].match(/\d+/)
|
||||||
|
raise
|
||||||
|
end
|
||||||
|
rescue => e
|
||||||
|
puts "failed posting story #{s.id} (#{status.inspect}): #{e.inspect}\n" +
|
||||||
|
"#{res.inspect}"
|
||||||
|
exit
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Keystore.put(LAST_STORY_KEY, s.id)
|
||||||
|
sleep 2
|
||||||
|
end
|
Loading…
Reference in a new issue