Skip to content

Instantly share code, notes, and snippets.

@repeatedly
Created October 26, 2008 18:30
Show Gist options
  • Select an option

  • Save repeatedly/19926 to your computer and use it in GitHub Desktop.

Select an option

Save repeatedly/19926 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/ruby -Ku
# -*- coding: utf-8 -*-
$KCODE = 'u' if RUBY_VERSION < '1.9.0'
%w[cgi time rss/2.0 net/https].each do |lib|
require lib
end
class OneDay
DEFAULT = {
:domain => 'twitter.com',
:port => 443,
:path => "/statuses/user_timeline.rss",
:ssl => true
}
def initialize(config = {})
@config = DEFAULT.merge(config)
@start = @config[:id].length + 1
@rotate = true
default_span
end
attr_accessor :to, :from
def exec
count = 1
result = []
connect = create_connection
response = nil
while @rotate
connect.start { |conn|
response = conn.request(create_request(count))
}
result.unshift(parse_rss(RSS::Parser.parse(response.body)))
count += 1
end
result.flatten
end
def parse_rss(rss)
contents = []
rss.items.reverse.each do |item|
break if item.date.day >= @from.day
if item.date.day < @to.day
@rotate = false
next
end
content = ''
content << "[#{item.link}:title=#{item.date.strftime('%H:%M')}]"
content << format_tweet(item.title)
content << "\n"
contents << content
end
contents
end
private
def default_span
from = Time.now.to_a
from[0] = from[1] = from[2] = 0
@from = Time.local(*from)
@to = @from - (60 * 60 * 24)
end
def create_connection
connection = Net::HTTP.new(@config[:domain], @config[:port])
if @config[:ssl]
connection.use_ssl = true
connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
connection
end
def create_request(page)
request = Net::HTTP::Get.new(@config[:path] + "?page=" + page.to_s)
request.basic_auth(@config[:id], @config[:pass])
request
end
def format_tweet(tweet)
text = tweet[@start..-1].dup
text = text.gsub(/http(\S+)/) { |link| "[#{link}:title=#{link}]" }
text = text.gsub(/@(\S+)/) { "[http://#{@config[:domain]}/#{$1}:title=@#{$1}]" }
text
end
end
if $0 == __FILE__
raise if ARGV.size < 2
puts OneDay.new({:id => ARGV[0], :pass => ARGV[1]}).exec
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment