ã³ãã³ãã©ã¤ã³ã§ãã¤ã¼ãããRubyã¹ã¯ãªããï¼ã¤ã¥ãï¼
先日ãã³ãã³ãã©ã¤ã³ã§ãã¤ã¼ãããRubyã¹ã¯ãªãããä½ã£ã¦ã¿ã¾ããããå¼æ°ã®æå¾ãURLã ã£ãå ´åãGoogle URL Shortener APIã使ã£ã¦ç縮ããããã«ãã¦ã¿ã¾ããã
ãã®ã¾ã¾ã§ãåãã¾ãããGoogleã¯Access Keyã®åå¾ãæ¨å¥¨ããè¨å®ããã¨Anonymousç¶æ ã¨æ¯ã¹ã¦å¶é*1ãæ¡å¤§ããã¾ããå æ¥ãå ¬éããã¹ã¯ãªãããæ¢ã«ã使ãã§ãAccess Keyãè¨å®ãããå ´åã¯ããææ°ã§ããã~/.twrcããã¡ã¤ã«ã«ãgoogle_api_access_keyãã追è¨ãã¦ãã ããã
google_api_access_key: <YOUR ACCESS KEY>
ã½ã¼ã¹
#!/usr/bin/env ruby $KCODE = 'u' require 'fileutils' require 'yaml' require 'rubygems' require 'oauth' require 'json' class Tw CONFIG_FILE = '.twrc' # Twitter API KEY = 'consumer_key' SECRET = 'consumer_secret' TOKEN = 'access_token' TOKEN_SECRET = 'access_token_secret' MAX_LENGTH = 140 TWITTER_API = 'http://api.twitter.com' UPDATE_URL = 'https://api.twitter.com/1/statuses/update.xml' # Google URL Shortener API ACCESS_KEY = 'google_api_access_key' GOOGLE_APIS = 'www.googleapis.com' SHORTENER_PATH = '/urlshortener/v1/url' attr_accessor :config def initialize config_file = File.join(File.expand_path('~'), CONFIG_FILE) begin @config = YAML.load(File.read(config_file)) rescue @config = { KEY => prompt(KEY), SECRET => prompt(SECRET), TOKEN => prompt(TOKEN), TOKEN_SECRET => prompt(TOKEN_SECRET), ACCESS_KEY => prompt(ACCESS_KEY) } File.open(config_file, 'w') {|f| f.write(@config.to_yaml)} FileUtils.chmod(0600, config_file) puts "saved to #{config_file}" end end def get_token consumer = OAuth::Consumer.new(@config[KEY], @config[SECRET], :site => TWITTER_API) token_hash = { :oauth_token => @config[TOKEN], :oauth_token_secret => @config[TOKEN_SECRET] } access_token = OAuth::AccessToken.from_hash(consumer, token_hash) end def shorten_url(long_url) params = {'longUrl' => long_url} params.store('key', @config[ACCESS_KEY]) if @config[ACCESS_KEY] && !@config[ACCESS_KEY].empty? response = '' http = Net::HTTP.new(GOOGLE_APIS, 443) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE response = http.post(SHORTENER_PATH, params.to_json, {'Content-Type' => 'application/json'}) return Net::HTTPOK == response.class ? (JSON.parse(response.body))['id'] : '' end private def prompt(string) print string.gsub('_', ' ').capitalize + ":" gets.strip end end URL_REGEXP = /(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)/ix tw = Tw.new if URL_REGEXP =~ (ARGV.length > 0 ? ARGV[ARGV.length - 1] : '') status = (ARGV[0, ARGV.length - 1].join(' ') + ' ' + tw.shorten_url($1)).strip else status = ARGV.join(' ') end unless status.empty? && status.split('').length > Tw::MAX_LENGTH response = tw.get_token.post(Tw::UPDATE_URL, {:status => status}) exit(Net::HTTPOK == response) else exit(false) end
è¨å®
ã¢ããªãTwitter Applicationsã§ç»é²ãã¦ãã ãããã¢ããªã±ã¼ã·ã§ã³ã®ç¨®é¡ã¯ããã¯ã©ã¤ã¢ã³ãã¢ããªã±ã¼ã·ã§ã³ãã§ãã
Access Tokenã¨Access Token Secretã¯ãã¢ããªãç»é²ããå¾ãå³ãµã¤ãã¡ãã¥ã¼ã«ãããMy Access Tokenããã¯ãªãã¯ããã¨åå¾ã§ãã¾ãã
Access Keyã¯ãGoogle APIs Consoleã§åå¾ã§ãã¾ãã
å¼æ°ãªãã§èµ·åããConsumer KeyãConsumer SecretãAccess TokenãAccess Token SecretãGoogle Api Access Keyãå ¥åãã¦ãã ãããAccess Keyã¯çç¥ããå ´åãåã«ã¨ã³ã¿ã¼ãã¼ã§ã¹ããããã¦ãã ããã
使ãæ¹
Twitterã«æ稿ãããã¡ãã»ã¼ã¸ãå¼æ°ã¨ãã¦ãèµ·åãã¦ãã ãããè¤æ°ã®å¼æ°ã¯ãåè§ã®ç©ºç½ã§é£çµãã¾ãããã ããåè§æ¬å¼§ãæä¸ã«ããå ´åã¯ãæç« å ¨ä½ãããã«ã¯ã©ã¼ãã¼ã·ã§ã³ã§å²ã£ã¦ãã ããã
注æç¹
å½ããåã®ãã¨ã§ããããAS ISã*2ã§ãã
ãã¬ã³ã¿ã¤ã³ãã§ã³æ¬²ããï¼*3
欲ãããã¬ã¼ã³ãã¯â¦
Apple iPod touch 64GB MC547J/A
- åºç社/ã¡ã¼ã«ã¼: Apple Computer
- çºå£²æ¥: 2010/09/02
- ã¡ãã£ã¢: ã¨ã¬ã¯ãããã¯ã¹
- è³¼å ¥: 12人 ã¯ãªãã¯: 580å
- ãã®ååãå«ãããã° (101件) ãè¦ã
*1:queries/dayãä¸æ¥ã«ä½åãåãåãã(ç縮å)ãGoogleãåãä»ãããã
*3:ã¯ã¦ãªãã¼ã¯ã¼ãã®ãã£ã³ãã¼ã³ã«å¿åãã¦ã¿ã¾ããã