#!/usr/bin/env ruby
require 'rubygems'
require 'rss'
require 'logger'
require 'twitter'
log = Logger.new('feed.log')
log.level = Logger::DEBUG
def shorten(long_url)
id = 'YOUR_BITLY_ID'
api_key = 'BITLY_API_KEY'
version = '2.0.1'
query = "version=#{version}&longUrl=#{long_url}&login=#{id}&apiKey=#{api_key}"
result = JSON.parse(Net::HTTP.get("api.bit.ly", "/shorten?#{query}"))
result['results'].each_pair {|long_url, value|
return value['shortUrl']
}
end
feeds = []
feeds << {
'author' => 'yusukezzz',
'url' => 'http://yusukezzz.net/blog/feed'}
$KCODE = 'u'
latest = 0
httpauth = Twitter::HTTPAuth.new('twitter_account', 'password')
client = Twitter::Base.new(httpauth)
begin
File.open('latest.dat'){ |f| latest = f.gets }
rescue => e
log.debug(e.message)
end
feeds.each do |feed|
rss = RSS::Parser.parse(feed['url'])
rss.channel.items.reverse.each do |i|
if latest.to_i < i.pubDate.to_i
post = "#{feed['author']} > #{i.title} #{shorten(i.link)}"
begin
client.update(post)
rescue => e
log.debug(e.message)
end
File.open('latest.dat', 'w') do |f|
f.puts i.pubDate.to_i.to_s
log.debug('update latest date')
end
end
end
end
卒論で3年次に作ったtalklineという奴も取り扱うことになったのでちょこっと修正しました
twitterにbitlyで短縮したURL付きのpostが出来るように
oauthについては参考サイト多数なので省略
良くわからなかったのはoauthの認証情報でどれを保存すれば良いのか?
保存するのはaccess_tokenとaccess_token_secretでした
この2つを保存しておけば以後consumerキーと組み合わせて即リクエスト出来ます
bitly対応では
Rubyでbit.lyのAPI経由で短縮URLを取得する – 黒川仁の文具堂ブログ三昧
APIを取得してこちらのshortenメソッドをコピペしました
例のごとく反映は遅れるかも
先日、twitterが正式に位置情報に対応しましたが、自分も試してみました
rubyのtwitterというgemでやってみました
# access_token, access_token_secretはあらかじめ準備しているものとします
twitter_oauth = Twitter::OAuth.new(CONSUMER_KEY, SECRET_KEY)
twitter_oauth.authorize_from_access(
access_token, access_token_secret)
@twitter = Twitter::Base.new(twitter_oauth)
@twitter.update('hoge', {:lat => latitude, :long => longtitude})
こんな感じでoptionを渡してやるだけです
twitter_gemのドキュメントにはoptionがin_reply_to_status_idしか書いてないのですが、ちゃんと投稿してくれます
ちなみに埋め込んだ座標をtweetdeckで表示するとこんな感じ

NY maps overlay on tweetdeck
verify_credentialsメソッドは認証したユーザについての情報を返す
Mashとかいうhashっぽいので定義されていて、何故かメソッドのごとく” . “でアクセス出来る
例)Twitter::Base.new(oauth).verify_credentials.screen_name
created_at
description
favorited
favourites_count
followers_count
following
friends_count
geo_enabled
href
id
in_reply_to_screen_name
in_reply_to_status_id
in_reply_to_user_id
location
name
notifications
profile_background_color
profile_background_image_url
profile_background_tile
profile_image_url
profile_link_color
profile_sidebar_border_color
profile_sidebar_fill_color
profile_text_color
protected
rel
screen_name
source
status
statuses_count
text
time_zone
truncated
url
utc_offset
verified
#追記
原因は良く分からないけど、なんかいつの間にか出来るようになってた
#追記終了
サンプルを漁ると大体:oauth_callbackがスクリプト側で上書きされているけど、
最近は仕様が変わったのかそれだとUnauthorizedが返ってくるみたい
web上で行う設定とスクリプト側の指定を同じにする必要があるらしい
でもweb上の設定だとlocalhostとか指定出来ないし、一度どこかにアップロードしないとテストも出来なくて面倒
developmentモードとか用意して欲しいなー
#google.comとかも指定出来ない???
#なんか良くわからん…