Created
December 14, 2010 06:51
-
-
Save kdmsnr/740086 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
# -*- coding: utf-8 -*- | |
# from: http://weed.cocolog-nifty.com/wzero3es/2010/03/090918-mixigmai.html | |
$KCODE = 'UTF-8' | |
require 'rubygems' | |
require 'mechanize' | |
require 'time' | |
class Mixi | |
def initialize(username, password) | |
@agent = Mechanize.new | |
page = @agent.get('https://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" | |
`touch timestamp` | |
File.open("timestamp", "r+") do |f| | |
if (content = f.read).empty? | |
@last_checked_time = Time.local(2009, 1, 1, 0, 0) | |
else | |
@last_checked_time = Time.parse(content) | |
end | |
f.print Time.now | |
end | |
end | |
def get_recent_diaries | |
@recent_diaries = [] | |
page = @agent.get('http://mixi.jp/new_friend_diary.pl') | |
page.links.each do |link| | |
url = link.href | |
next unless /view_diary.pl\?id\=/ =~ url | |
diary_page = @agent.get(url) | |
diary_title = diary_page.title | |
diary_time_text = diary_page.at('dd').inner_text | |
if /(\d\d\d\d)年(\d\d)月(\d\d)日(\d\d):(\d\d)/ =~ diary_time_text | |
diary_time = Time.local $1, $2, $3, $4, $5 | |
end | |
if @last_checked_time < diary_time | |
puts "#{diary_time}: A new entry is found." | |
@recent_diaries.push({:title => diary_title, | |
:text => url + "\n" + | |
diary_page.at('div#diary_body').inner_text | |
}) | |
end | |
end | |
@recent_diaries | |
end | |
end | |
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 | |
require 'yaml' | |
config = YAML.load(File.open("config.yaml"){|f| f.read }) | |
# mixi | |
USERNAME = config["mixi_username"] | |
PASSWORD = config["mixi_password"] | |
# Gmail | |
MAIL_ADDRESS = config["gmail_email"] | |
MAIL_PASS = config["gmail_password"] | |
TO_ADDRESS = config["gmail_to"] | |
Mixi.new(USERNAME, PASSWORD).get_recent_diaries.each do |recent_diary| | |
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