Created
March 16, 2010 13:43
-
-
Save tatsuro-ueda/333968 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
require 'net/smtp' | |
require 'tlsmail' | |
class Gmail | |
# 使う準備をする | |
def initialize(mail_address, mail_pass) | |
@mail_address = mail_address | |
@mail_pass = mail_pass | |
@smtpserver = Net::SMTP.new("smtp.gmail.com", 587) | |
@smtpserver.enable_tls(OpenSSL::SSL::VERIFY_NONE) | |
end | |
# メールを送る | |
def send(to_address, subject = "", body = "") | |
puts "sending a mail..." | |
message = <<-EndOfMail | |
From: #{@mail_address} | |
To: #{to_address} | |
Subject: #{subject} | |
Date: #{Time::now.strftime("%a, %d %b %Y %X %z")} | |
#{body} | |
EndOfMail | |
@smtpserver.start('myserverdomain', @mail_address, @mail_pass, :login) do |smtp| | |
smtp.send_message message, @mail_address, to_address | |
end | |
end | |
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 'mechanize' | |
require 'time' | |
class Mixi | |
# mixiにログインし、最後に日記を調べた時刻を取得する | |
def initialize(username, password) | |
print "Logging in Mixi..." | |
@agent = WWW::Mechanize.new | |
page = @agent.get('http://mixi.jp/') | |
form = page.forms[0] | |
form.fields.find {|f| f.name == 'email'}.value = username | |
form.fields.find {|f| f.name == 'password'}.value = password | |
form.fields.find {|f| f.name == 'next_url'}.value = '/home.pl' | |
page = @agent.submit(form, form.buttons.first) | |
print "done\n" | |
if /url=([^"])"/ =~ page.body | |
redirect_url = 'http://mixi.jp' + $1.to_s | |
@agent.get(redirect_url) | |
end | |
# 最後に日記を調べた時刻を取得する | |
if File.exist? ".mixi_checker" | |
f = File.open(".mixi_checker", "r") | |
# timeライブラリのparseメソッドで文字列からTimeオブジェクトに変換する | |
@last_checked_time = Time.parse(f.read) | |
f.close # | |
f = File.open(".mixi_checker", "w") # | |
f.print Time.now # | |
f.close # | |
else | |
@last_checked_time = Time.local(2009, 1, 1, 0, 0) # | |
end | |
end | |
# linkが内部の新しい日記かどうか否かを調べる | |
def diary_internal_and_new?(link) | |
# 条件1:マイミク日記を参照していること | |
unless link.href =~ /view_diary\.pl.*/ | |
return false | |
else | |
diary_page = @agent.get(link.href) # リンク先のページを取得し | |
# タイトルに「|」があればmixi日記と判断する | |
unless diary_page.title =~ /\|/ | |
print "external.\n" | |
return false | |
else | |
# 新旧判定のため、日付文字列からTimeオブジェクトを作成する | |
if /(\d\d\d\d)年(\d\d)月(\d\d)日(\d\d):(\d\d)/ =~ diary_page.at('dd').inner_text | |
diary_time = Time.local $1, $2, $3, $4, $5 | |
end | |
# 条件2:新しい記事であること | |
if diary_time > @last_checked_time | |
puts "#{diary_time}: new one." # 報告 | |
return true | |
else | |
puts "#{diary_time}: past one." # 報告 | |
return false | |
end | |
end | |
end | |
end | |
# 内部の新しい日記記事のリストを返す | |
def get_new_internal_diaries | |
@new_internal_diaries = [] # 初期化 | |
page_home = @agent.get('http://mixi.jp/home.pl') # ホームページを取得し | |
page_home.links.each do |link| # そのページの全てのリンクを調べる | |
if diary_internal_and_new?(link) | |
@new_internal_diaries.push({:title => diary_page.title, | |
:text => diary_page.at('div#diary_body').inner_text}) | |
end | |
end | |
@new_internal_diaries | |
end | |
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
$KCODE = 'UTF-8' | |
require 'time' | |
### 初期設定ここから | |
# mixi設定 | |
USERNAME = '[email protected]' | |
PASSWORD = 'xxxxxxxx' | |
# Gmail設定 | |
MAIL_ADDRESS = "[email protected]" | |
MAIL_PASS = "xxxxxxxx" | |
TO_ADDRESS = MAIL_ADDRESS | |
### 初期設定ここまで | |
require 'mixi' | |
require 'gmail' | |
# mixiから取得した最近の日記記事のうち新しくて外部のブログを使っていないものについて | |
Mixi.new(USERNAME, PASSWORD).get_new_internal_diaries.each do |recent_diary| | |
# Gmailで送信する | |
Gmail.new(MAIL_ADDRESS, MAIL_PASS).send(TO_ADDRESS, recent_diary[:title], recent_diary[:text]) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment