Created
August 29, 2010 12:27
-
-
Save hokorobi/556250 to your computer and use it in GitHub Desktop.
twitterclient.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
if ARGV.size < 1 | |
exit 1 | |
else | |
tweet = ARGV[0].encode('UTF-8', 'Windows-31J') | |
end | |
# メッセージボックス表示 | |
require 'win32ole' | |
def show(msg) | |
wsh = WIN32OLE.new('WScript.Shell') | |
title = "Message" | |
wsh.Popup(msg, 0, title, 0 + 64 + 0x40000) | |
end | |
# 設定ファイル読み込み | |
require "yaml" | |
tn = Time.now | |
configFile = String.new("#{ENV['HOME']}/.twitter") | |
if File.readable?(configFile) then | |
begin | |
config = File.read(configFile) | |
configObj = YAML.load(config) | |
rescue => ex | |
show(ex.message) | |
exit 1 | |
end | |
else | |
printf("make %s\n", configFile) | |
exit 1 | |
end | |
# 呟きの長さチェック | |
if tweet.length > 140 then | |
require "Jmp" | |
require "uri" | |
if URI.extract(tweet) then | |
jmp = Jmp.new(configObj['jmpuser'], configObj['jmpapikey'], tweet) | |
tweet = jmp.shorten() | |
end | |
if tweet.length> 140 then | |
show("文字列が長すぎます。#{tweet.length}") | |
exit 1 | |
end | |
end | |
# twitter へ post | |
require 'rubygems' | |
require 'twitter' | |
#puts configObj['token'] | |
#puts configObj['secret'] | |
#puts configObj['atoken'] | |
#puts configObj['asecret'] | |
begin | |
oauth = Twitter::OAuth.new(configObj['token'], configObj['secret']) | |
oauth.authorize_from_access(configObj['atoken'], configObj['asecret']) | |
client = Twitter::Base.new(oauth) | |
client.update(tweet) | |
rescue | |
show("Tweet failed.") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment