Created
November 19, 2008 20:17
-
-
Save singpolyma/26680 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'hpricot' | |
require 'open-uri' | |
require 'uri' | |
require 'cgi' | |
def get_doc(uri) | |
if uri.to_s =~ /explore\.twitter\.com/ | |
uri = URI.parse('http://twitter.com/' + uri.to_s.scan(/.+\/(.+)$/)[0][0]) | |
end | |
begin | |
page = open(uri.to_s).read | |
rescue Exception | |
return nil | |
end | |
if page == '' | |
return nil | |
end | |
doc = Hpricot.parse(page) | |
doc.search('a').each do |a| | |
a_uri = URI.parse(a.attributes['href'].gsub(' ','%20')).normalize rescue URI.parse('') | |
a_uri.scheme = uri.scheme if a_uri.scheme.nil? | |
if a_uri.scheme =~ /https?|ftp/ | |
a_uri.host = uri.host if a_uri.host.nil? | |
a_uri.path = "#{uri.path}#{a_uri.path}" if a_uri.path.nil? || a_uri.path[0..0] != '/' | |
end | |
a.set_attribute 'href', a_uri.to_s | |
end | |
doc.search('img').each do |a| | |
a_uri = URI.parse(a.attributes['src'].gsub(' ','%20')).normalize rescue URI.parse('') | |
a_uri.scheme = uri.scheme if a_uri.scheme.nil? | |
if a_uri.scheme =~ /https?|ftp/ | |
a_uri.host = uri.host if a_uri.host.nil? | |
a_uri.path = "#{uri.path}#{a_uri.path}" if a_uri.path.nil? || a_uri.path[0..0] != '/' | |
end | |
a.set_attribute 'src', a_uri.to_s | |
end | |
doc | |
end | |
def representative_hcard(doc, url) | |
uri = URI.parse(url.strip).normalize | |
url = uri.to_s | |
cards = doc.search('.vcard') | |
return cards[0] if cards.size == 1 | |
cards.each do |vcard| | |
uid = vcard.at('.uid') | |
unless uid.nil? | |
begin | |
if uid.attributes['href'].reverse[0..0] == '/' && url.reverse[0..0] != '/' | |
iz = (uid.name == 'a' && uid.attributes['href'] == "#{url}/") | |
else | |
iz = (uid.name == 'a' && uid.attributes['href'] == url) | |
end | |
if iz || (URI.parse(uid.text_content).normalize.to_s == url) | |
return vcard | |
end | |
rescue Exception | |
end | |
end | |
uid = vcard.at('a[@rel~=me]') | |
unless uid.nil? | |
return vcard | |
end | |
end | |
nil | |
end | |
doc = get_doc(URI.parse(ARGV[0])) | |
hcard = representative_hcard(doc, ARGV[0]) | |
nick = doc.at('.screen-name') unless hcard | |
nick = hcard.at('.nickname') unless nick | |
nick = hcard.at('.fn') unless nick | |
nick = nick.html.gsub(/<[^>]+>/,'').gsub(/[^a-zA-Z0-9_\-]+/,'.') | |
latest = doc.at('*[@rel~=bookmark"]').attributes['href'] rescue nil | |
latest = ARGV[0] unless latest | |
puts '<span class="vcard reply"> @<a href="' + CGI::escapeHTML(ARGV[0]) + '" class="fn nickname url">' + CGI::escapeHTML(nick) + '</a> </span>' | |
puts '<a rel="reply" href="' + CGI::escapeHTML(latest) + '">in reply to...</a>' if latest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment