Created
June 6, 2012 14:43
-
-
Save shunsugai/2882312 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
#tweetから記事リンクがあるものだけを取得する。 | |
#coding utf-8 | |
require 'twitter' | |
require 'net/http' | |
require 'uri' | |
class LinkGrabber | |
def initialize(user) | |
@user = user | |
end | |
def timeline | |
Twitter.user_timeline(@user) | |
end | |
def expand_url(url) | |
begin | |
uri = url.kind_of?(URI) ? url : URI.parse(url) | |
rescue URI::InvalidURIError => e | |
puts e | |
return nil | |
end | |
begin | |
res = Net::HTTP.start(uri.host, uri.port) do |io| | |
io.head(uri.path)['Location'] | |
end | |
rescue EOFError | |
return uri.to_s | |
rescue Errno::ECONNRESET => e | |
puts e | |
retry | |
end | |
return uri.to_s if res == nil | |
expand_url(res) | |
end | |
def links | |
links = [] | |
texts = timeline.map {|item| item['text']} | |
texts.each do |text| | |
links << expand_url($1) if text =~ %r{(http://.*?)(\s|\z|[^A-Za-z0-9./#?_-])} | |
end | |
links | |
end | |
end | |
if __FILE__ == $0 | |
lg = LinkGrabber.new('user_name') | |
p lg.links | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment