Created
April 20, 2012 09:15
-
-
Save Shinpeim/2427288 to your computer and use it in GitHub Desktop.
web antenna
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
# -*- coding: utf-8 -*- | |
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib')) | |
require "net/http"require "uri" | |
require 'webantenna' | |
def usage | |
warn "usage: ruby main.rb imkayac_username imkayac_password" | |
exit 1 | |
end | |
imkayac_user = ARGV.shift || usage | |
imkayac_password = ARGV.shift || usage | |
def post(uri_str, data) | |
uri = URI.parse(uri_str) | |
Net::HTTP.start(uri.host, uri.port){|http| | |
request = Net::HTTP::Post.new(uri.path) | |
request["user-agent"] = "Ruby/WebAntena" | |
request.set_form_data(data, "&") | |
http.request(request) | |
} | |
end | |
WebAntenna::Lader.new.run("http://hihumiyo.net/", File.join(File.dirname(__FILE__), 'data', 'hihumiyo_sha1')) do | |
uri = "http://im.kayac.com/api/post/#{imkayac_user}" | |
post(uri, { | |
'message' => 'hihumiyo.netの更新検出', | |
'password' => imkayac_password, | |
}) | |
end |
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 'digest/sha1' | |
require 'open-uri' | |
module WebAntenna | |
class Lader | |
def get_content(uri) | |
open(uri).read | |
end | |
def get_last_sha1(file_path) | |
return nil unless File.file?(file_path) | |
File.read(file_path) | |
end | |
def save_sha1(file_path, sha1) | |
File.open(file_path,"w").write(sha1) | |
end | |
def run(uri, data_file_path) | |
raise ArgumentsError, "block is required" unless block_given? | |
sha1 = Digest::SHA1::hexdigest get_content(uri) | |
last_sha1 = get_last_sha1(data_file_path) | |
if (sha1 != last_sha1) | |
save_sha1(data_file_path, sha1) | |
yield | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment