X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;ds=inline;f=data%2Frbot%2Fplugins%2Ftwitter.rb;h=8725ac2cd21de783cbad33c30709de7849aa580b;hb=fc0c682cbf7a68b8ccd458ac776770fccf9e59f4;hp=1c8f61dd0556b7064e3e21a2b1a9a26659bbd527;hpb=9ed8ad84aa87a414319ffea22b4ffc3cee5e53bf;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/twitter.rb b/data/rbot/plugins/twitter.rb index 1c8f61dd..8725ac2c 100644 --- a/data/rbot/plugins/twitter.rb +++ b/data/rbot/plugins/twitter.rb @@ -39,6 +39,29 @@ class TwitterPlugin < Plugin :default => 3, :validate => Proc.new { |v| v > 0 && v <= 10}, :desc => "Maximum number of status updates shown by 'twitter friends status'") + def twitter_filter(s) + loc = Utils.check_location(s, Regexp.new('twitter\.com/#!/.*/status/\d+')) + return nil unless loc + id = loc.first.match(/\/status\/(\d+)/)[1] + xml = @bot.httputil.get('http://api.twitter.com/1/statuses/show.xml?id=' + id) + return nil unless xml + root = REXML::Document.new(xml).root + status = { + :date => (Time.parse(root.elements["created_at"].text) rescue ""), + :id => (root.elements["id"].text rescue ""), + :text => (root.elements["text"].text.ircify_html rescue ""), + :source => (root.elements["source"].text rescue ""), + :user => (root.elements["user/name"].text rescue ""), + :user_nick => (root.elements["user/screen_name"] rescue "") + # TODO other entries + } + status[:nicedate] = String === status[:date] ? status[:date] : Utils.timeago(status[:date]) + return { + :title => "#{status[:user]}/#{status[:id]}", + :content => "#{status[:text]} (#{status[:nicedate]} via #{status[:source]})" + } + end + def initialize super @@ -52,6 +75,8 @@ class TwitterPlugin < Plugin val end end + + @bot.register_filter(:twitter, :htmlinfo) { |s| twitter_filter(s) } end def report_oauth_missing(m, failed_action) @@ -90,21 +115,17 @@ class TwitterPlugin < Plugin return false end - count = @bot.config['twitter.friends_status_count'] + count = friends ? @bot.config['twitter.friends_status_count'] : @bot.config['twitter.status_count'] user = URI.escape(nick) + # receive the public timeline per default (this works even without an access_token) + uri = "https://api.twitter.com/1/statuses/user_timeline.xml?screen_name=#{user}&count=#{count}&include_rts=true" if @has_oauth and @registry.has_key?(m.sourcenick + "_access_token") if friends #no change to count variable - uri = "https://api.twitter.com/1/statuses/friends_timeline.xml?count=#{count}" - response = @access_token.get(uri).body - else - count = @bot.config['twitter.status_count'] - uri = "https://api.twitter.com/1/statuses/user_timeline.xml?screen_name=#{user}&count=#{count}" - response = @access_token.get(uri).body + uri = "https://api.twitter.com/1/statuses/friends_timeline.xml?count=#{count}&include_rts=true" end + response = @access_token.get(uri).body else - #unauthorized user, will try to get from public timeline the old way - uri = "http://twitter.com/statuses/user_timeline/#{user}.xml?count=#{count}" response = @bot.httputil.get(uri, :cache => false) end debug response @@ -192,14 +213,19 @@ class TwitterPlugin < Plugin return false end - @consumer = OAuth::Consumer.new(key, secret, { + @consumer = OAuth::Consumer.new(key, secret, { :site => "https://api.twitter.com", :request_token_path => "/oauth/request_token", :access_token_path => "/oauth/access_token", :authorize_path => "/oauth/authorize" - } ) - @request_token = @consumer.get_request_token - @registry[m.sourcenick + "_request_token"] = YAML::dump(@request_token) + } ) + begin + @request_token = @consumer.get_request_token + rescue OAuth::Unauthorized + m.reply _("My authorization failed! Did you block me? Or is my Twitter Consumer Key/Secret pair incorrect?") + return false + end + @registry[m.sourcenick + "_request_token"] = YAML::dump(@request_token) m.reply "Go to this URL to get your authorization PIN, then use 'twitter pin ' to finish authorization: " + @request_token.authorize_url end