X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;ds=sidebyside;f=data%2Frbot%2Fplugins%2Ftwitter.rb;h=3ed8e62c1c432ea3e9303550ed2adc8fb4ca3b45;hb=531bc63bd14c6bc3fd98f7fbcfa2f9a01fe21eb0;hp=e661e63326a76daa4b4a071dc75ab6649256844a;hpb=41fe156959823cf02179bc2c8a478038d98a846c;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/twitter.rb b/data/rbot/plugins/twitter.rb index e661e633..3ed8e62c 100644 --- a/data/rbot/plugins/twitter.rb +++ b/data/rbot/plugins/twitter.rb @@ -38,26 +38,35 @@ class TwitterPlugin < Plugin :default => 3, :validate => Proc.new { |v| v > 0 && v <= 10}, :desc => "Maximum number of status updates shown by 'twitter [home|mentions|retweets] status'") + URL_PATTERN = %r{twitter\.com/([^/]+)(?:/status/(\d+))?} + def twitter_filter(s) - loc = Utils.check_location(s, Regexp.new('twitter\.com/#!/.*/status/\d+')) + loc = Utils.check_location(s, URL_PATTERN) return nil unless loc - id = loc.first.match(/\/status\/(\d+)/)[1] - - response = @app_access_token.get('/1.1/statuses/show/'+id).body + matches = loc.first.match URL_PATTERN + if matches[2] # status id matched + id = matches[2] + url = '/1.1/statuses/show/%s.json' % id + else # no status id, get the latest status of that user + user = matches[1] + url = '/1.1/statuses/user_timeline.json?screen_name=%s&count=1&include_rts=true' % user + end + response = @app_access_token.get(url).body begin - tweet = JSON.parse(response).first + tweet = JSON.parse(response) + tweet = tweet.first if tweet.instance_of? Array status = { :date => (Time.parse(tweet["created_at"]) rescue ""), - :id => (tweet["id"].text rescue ""), + :id => (tweet["id_str"] rescue ""), :text => (tweet["text"].ircify_html rescue ""), - :source => (tweet["source"].text rescue ""), + :source => (tweet["source"].ircify_html rescue ""), :user => (tweet["user"]["name"] rescue ""), :user_nick => (tweet["user"]["screen_name"] rescue "") # TODO other entries } status[:nicedate] = String === status[:date] ? status[:date] : Utils.timeago(status[:date]) return { - :title => "#{status[:user]}/#{status[:id]}", + :title => "@#{status[:user_nick]}", :content => "#{status[:text]} (#{status[:nicedate]} via #{status[:source]})" } rescue