Skip to content

Instantly share code, notes, and snippets.

@STAR-ZERO
Created April 19, 2012 06:36
Show Gist options
  • Save STAR-ZERO/2419136 to your computer and use it in GitHub Desktop.
Save STAR-ZERO/2419136 to your computer and use it in GitHub Desktop.
Rubyでツイートするよ
# -*- encoding: utf-8 -*-
#
# $ ruby tweet.rb hogehoge で投稿するよ
#
require "oauth"
require "twitter"
require "pp"
require "json"
CONSUMER_KEY = "☆ConsumerKeyを入れてね☆"
CONSUMER_SECRET = "☆ConsumerSecretを入れてね☆"
tweet = ARGV.join(" ")
if tweet.empty?
puts "引数入力してね"
exit
end
token_json = nil
if File.exist?("token.json")
#ファイルから取得
open("token.json") do |io|
token_json = JSON.load(io)
end
else
consumer = OAuth::Consumer.new(CONSUMER_KEY, CONSUMER_SECRET, :site => "http://twitter.com")
request_token = consumer.get_request_token
system("open " + request_token.authorize_url)
print "ピンを入力してね>"
pin = STDIN.gets
pin = pin.gsub(/\n/, '')
#トークン取得
access_token = request_token.get_access_token(:oauth_verifier => pin)
token_json = {
"token" => access_token.token,
"secret" => access_token.secret
}
#ファイルに保存
open("token.json", "w") do |io|
JSON.dump(token_json, io)
end
end
begin
Twitter.configure do |config|
config.consumer_key = CONSUMER_KEY
config.consumer_secret = CONSUMER_SECRET
config.oauth_token = token_json["token"]
config.oauth_token_secret = token_json["secret"]
end
#投稿
Twitter.update(tweet)
rescue
puts "エラー起きちゃった"
else
puts "投稿したよ"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment