Archive

Posts Tagged ‘twitter’

bot source

4月 15th, 2010
RUBY:
  1. #!/usr/bin/env ruby
  2. require 'rubygems'
  3. require 'rss'
  4. require 'logger'
  5. require 'twitter'
  6.  
  7. log = Logger.new('feed.log')
  8. log.level = Logger::DEBUG
  9.  
  10.  
  11. def shorten(long_url)
  12.   id = 'YOUR_BITLY_ID'
  13.   api_key = 'BITLY_API_KEY'
  14.   version = '2.0.1'
  15.  
  16.   query = "version=#{version}&longUrl=#{long_url}&login=#{id}&apiKey=#{api_key}"
  17.   result = JSON.parse(Net::HTTP.get("api.bit.ly", "/shorten?#{query}"))
  18.   result['results'].each_pair {|long_url, value|
  19.     return value['shortUrl']
  20.   }
  21. end
  22.  
  23. feeds = []
  24. feeds <<{
  25.   'author' => 'yusukezzz',
  26.   'url' => 'http://yusukezzz.net/blog/feed'}
  27.  
  28. $KCODE = 'u'
  29. latest = 0
  30. httpauth = Twitter::HTTPAuth.new('twitter_account', 'password')
  31. client = Twitter::Base.new(httpauth)
  32. begin
  33.   File.open('latest.dat'){ |f| latest = f.gets }
  34. rescue => e
  35.   log.debug(e.message)
  36. end
  37. feeds.each do |feed|
  38.   rss = RSS::Parser.parse(feed['url'])
  39.   rss.channel.items.reverse.each do |i|
  40.     if latest.to_i <i.pubDate.to_i
  41.       post = "#{feed['author']}> #{i.title} #{shorten(i.link)}"
  42.       begin
  43.         client.update(post)
  44.       rescue => e
  45.         log.debug(e.message)
  46.       end
  47.       File.open('latest.dat', 'w') do |f|
  48.         f.puts i.pubDate.to_i.to_s
  49.         log.debug('update latest date')
  50.       end
  51.     end
  52.   end
  53. end

,

talklineのtwitter oauthとbitlyへの対応

1月 7th, 2010

卒論で3年次に作ったtalklineという奴も取り扱うことになったのでちょこっと修正しました
twitterにbitlyで短縮したURL付きのpostが出来るように

oauthについては参考サイト多数なので省略
良くわからなかったのはoauthの認証情報でどれを保存すれば良いのか?
保存するのはaccess_tokenとaccess_token_secretでした
この2つを保存しておけば以後consumerキーと組み合わせて即リクエスト出来ます

bitly対応では
Rubyでbit.lyのAPI経由で短縮URLを取得する - 黒川仁の文具堂ブログ三昧
APIを取得してこちらのshortenメソッドをコピペしました

例のごとく反映は遅れるかも

,

twitterに位置情報を埋め込む

12月 3rd, 2009

先日、twitterが正式に位置情報に対応しましたが、自分も試してみました
rubyのtwitterというgemでやってみました

RUBY:
  1. # access_token, access_token_secretはあらかじめ準備しているものとします
  2. twitter_oauth = Twitter::OAuth.new(CONSUMER_KEY, SECRET_KEY)
  3. twitter_oauth.authorize_from_access(
  4. access_token, access_token_secret)
  5. @twitter = Twitter::Base.new(twitter_oauth)
  6. @twitter.update('hoge', {:lat => latitude, :long => longtitude})

こんな感じでoptionを渡してやるだけです
twitter_gemのドキュメントにはoptionがin_reply_to_status_idしか書いてないのですが、ちゃんと投稿してくれます
ちなみに埋め込んだ座標をtweetdeckで表示するとこんな感じ

NY maps overlay on tweetdeck

NY maps overlay on tweetdeck

, ,

rubyのTwitter::Base.new().verify_credentialsのメソッド?一覧

11月 11th, 2009

verify_credentialsメソッドは認証したユーザについての情報を返す
Mashとかいうhashっぽいので定義されていて、何故かメソッドのごとく" . "でアクセス出来る
例)Twitter::Base.new(oauth).verify_credentials.screen_name

RUBY:
  1. created_at
  2. description
  3. favorited
  4. favourites_count
  5. followers_count
  6. following
  7. friends_count
  8. geo_enabled
  9. href
  10. id
  11. in_reply_to_screen_name
  12. in_reply_to_status_id
  13. in_reply_to_user_id
  14. location
  15. name
  16. notifications
  17. profile_background_color
  18. profile_background_image_url
  19. profile_background_tile
  20. profile_image_url
  21. profile_link_color
  22. profile_sidebar_border_color
  23. profile_sidebar_fill_color
  24. profile_text_color
  25. protected
  26. rel
  27. screen_name
  28. source
  29. status
  30. statuses_count
  31. text
  32. time_zone
  33. truncated
  34. url
  35. utc_offset
  36. verified

,

twitterのoauth認証について

10月 15th, 2009

#追記
原因は良く分からないけど、なんかいつの間にか出来るようになってた
#追記終了

サンプルを漁ると大体:oauth_callbackがスクリプト側で上書きされているけど、
最近は仕様が変わったのかそれだとUnauthorizedが返ってくるみたい
web上で行う設定とスクリプト側の指定を同じにする必要があるらしい
でもweb上の設定だとlocalhostとか指定出来ないし、一度どこかにアップロードしないとテストも出来なくて面倒
developmentモードとか用意して欲しいなー

#google.comとかも指定出来ない???
#なんか良くわからん…