Skip to content

Instantly share code, notes, and snippets.

@namusyaka
Created January 26, 2011 13:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save namusyaka/796679 to your computer and use it in GitHub Desktop.
Save namusyaka/796679 to your computer and use it in GitHub Desktop.
HatenaDiary-rssのchannel部分だけ取得&キャッシュ
#!/usr/local/bin/ruby
require 'net/http'
require 'rss'
require 'time'
require 'lib/simple_cgi.rb'
print "Content-Type:text/html\n\n"
query = SimpleCGI::query
log = File::open("./src/cache.dat").read
if !(log == "") && Time::parse(log.split("\n")[0]) + 1800 > Time.new
puts "#{query["callback"]}#{log.split("\n")[1]}"
else
src = ""
array = []
Net::HTTP.version_1_2
Net::HTTP.start("d.hatena.ne.jp", 80) do |h|
src = RSS::Parser.parse(h.get("/namusyaka/rss").body, true).channel.to_s.split("\n")
end
src.each do |m|
c = m.scan(/<(title|link|description|dc:creator|dc:date)>([^<]*?)<\/([^>]*?)>/)
array << "\"#{c[0][0]}\" : \"#{c[0][1]}\"" if c[0]
end
begin
Dir::mkdir("b")
begin
File::open("./src/cache.dat", "w").write("#{Time.new}\n({#{array.join(",")}})")
rescue
end
Dir::unlink("b")
rescue
end
puts "#{query["callback"]}({#{array.join(",")}})"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment