WEBサイトの更新情報をメールで教えてほしい、そんなプログラミングです。
ライブラリには「mechanize」と「mail」を使ってます。
require "mechanize"
require "mail"
agent = Mechanize.new
agent.user_agent_alias = "Mac Safari 4"
url = "https://news.yahoo.co.jp/"
page = agent.get(url)
#yahoo TOPニュース
now_yahoo = page.search('//*[@id="uamods-topics"]')
#以前のyahoo TOPニュース
local_yahoo = nil
if File.exist?("yahoo.txt") then
file = File.open("yahoo.txt")
local_yahoo = file.read
file.close
end
#今回取得したTOPニュースをファイルに書き込み
file = File.open("yahoo.txt","w")
file.print(now_yahoo)
file.close
#TOPニュースに更新があればメールを送る
if now_yahoo == local_yahoo then
puts "更新してないよ"
else
puts "更新してますよー"
mail = Mail.new do
from "sumple@foo.com"
to "bar@bar.com"
subject "更新されました"
body "確認してね"
end
mail.delivery_method(:smtp,
address: "anything",
port: 3000,
domain: "gmail.com",
authentication: :login,
user_name: "foo",
password: "bar"
)
mail.deliver
end