Skip to content

Instantly share code, notes, and snippets.

@atoulme
Created March 9, 2009 15:37
Show Gist options
  • Save atoulme/76346 to your computer and use it in GitHub Desktop.
Save atoulme/76346 to your computer and use it in GitHub Desktop.
# Copied from here http://intertwingly.net/stories/2009/03/06/twitter.rb
# Full story: http://intertwingly.net/blog/2009/03/06/Eat-Your-Broccoli
require 'rubygems'
require 'rexml/document'
require 'xmpp4r/client'
require 'net/http'
# configuration
searchquery=%w(something something_else)
ignores = %w(mytwittername someothername)
$source = 'rubys@rubix/twitter'
$password = 'password'
dest = 'rubys@rubix/Laptop'
$xmppid = 0
twitterdb = "#{File.dirname(__FILE__)}/twitter.seen"
feed = "http://search.twitter.com/search.atom?q=&ands=&phrase=&ors=#{searchquery.join("+")}"
# read database of ids of seen entries
seen = YAML.parse(open(twitterdb).read).value.map {|n| n.value} rescue []
# lazily create a client
def client
return $client if $client
$client = Jabber::Client.new(Jabber::JID.new($source))
$client.connect.auth($password)
$client
end
# extract text from XML (for non-xhtmlim clients)
def text(node)
return node.to_s unless node.respond_to?(:children)
node.children.map{|child| text(child)}.join
end
# fetch and process feed
doc = REXML::Document.new(Net::HTTP.get(URI.parse(feed)))
doc.elements.each('//entry') do |entry|
id = entry.elements['id'].text
next if seen.include? id
# extract author, skipping the entry if it is me
author = entry.elements['author']
a = REXML::Element.new('a')
a.add_attribute('href', author.elements['uri'].text)
a.text = author.elements['name'].text.split(' (').first
next if ignores.include? a.text
# convert content to xhtml, with fallback to the title
content=entry.elements['content']
if content.attributes['type'] == 'html'
begin
content="<content type='xhtml'>#{content.text}</content>"
content=REXML::Document.new(content).root
rescue
content=entry.elements['title']
end
end
# format xhtmlim message thus: "#{a}: #{content.children}"
html = REXML::Element.new('html')
html.add_namespace('http://jabber.org/protocol/xhtml-im')
body = REXML::Element.new('body', html)
body.add_namespace('http://www.w3.org/1999/xhtml')
body.add(a)
body.add_text(': ')
content.children.each {|node| body.add(node)}
# build xmpp message to me
msg = Jabber::Message.new(dest, text(content))
msg.set_type(:normal).set_id(($xmppid+=1).to_s)
msg.add_element(html)
# send message
client.send(msg)
seen.unshift id
end
# update database of ids of seen entries
open(twitterdb,'w').write(seen.to_yaml)
# shut down client
(sleep 2; $client.close) if $client
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment