X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;ds=sidebyside;f=data%2Frbot%2Fplugins%2Ftwitter.rb;h=8725ac2cd21de783cbad33c30709de7849aa580b;hb=fc0c682cbf7a68b8ccd458ac776770fccf9e59f4;hp=045b6172ff39a0fdbc6cb19b13e29e2a0793da1a;hpb=915b8ecffc4b19877f36ddb36fa85f3a4cd53286;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/twitter.rb b/data/rbot/plugins/twitter.rb index 045b6172..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,12 +75,18 @@ class TwitterPlugin < Plugin val end end + + @bot.register_filter(:twitter, :htmlinfo) { |s| twitter_filter(s) } end def report_oauth_missing(m, failed_action) m.reply [failed_action, "I cannot authenticate to Twitter (OAuth not available)"].join(' because ') end + def report_key_missing(m, failed_action) + m.reply [failed_action, "no Twitter Consumer Key/Secret is defined"].join(' because ') + end + def help(plugin, topic="") return "twitter status [nick] => show nick's (or your) status, use 'twitter friends status [nick]' to also show the friends' timeline | twitter update [status] => updates your status on twitter | twitter authorize => Generates an authorization URL which will give you a PIN to authorize the bot to use your twitter account. | twitter pin [pin] => Finishes bot authorization using the PIN provided by the URL from twitter authorize. | twitter deauthorize => Makes the bot forget your Twitter account. | twitter actions [on|off] => enable/disable twitting of actions (/me does ...)" end @@ -86,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 @@ -167,8 +192,9 @@ class TwitterPlugin < Plugin end def authorize(m, params) + failed_action = "we can't complete the authorization process" unless @has_oauth - report_oauth_missing(m, "we can't complete the authorization process") + report_oauth_missing(m, failed_action) return false end @@ -182,14 +208,24 @@ class TwitterPlugin < Plugin key = @bot.config['twitter.key'] secret = @bot.config['twitter.secret'] - @consumer = OAuth::Consumer.new(key, secret, { + if key.empty? or secret.empty? + report_key_missing(m, failed_action) + return false + end + + @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