Created
June 11, 2009 20:13
-
-
Save wjessop/128201 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/opt/ruby-enterprise-1.8.6-20090201/bin/ruby | |
require 'rubygems' | |
require 'hpricot' | |
require 'open-uri' | |
require 'uri' | |
require 'rmail' | |
require 'mime/types' | |
require 'base64' | |
require 'net/smtp' | |
class DilbertFetcher | |
DOMAIN = 'http://dilbert.com' | |
FILENAME_FORMAT = '/%Y/%m%d' | |
ROOT_DIR = File.join ENV['HOME'], 'dilbert' | |
USER_AGENT = 'Mozilla/5.0 (X11; U; Linux i686; chrome://navigator/locale/navigator.properties; rv:1.7.5) Gecko/20041107 Firefox/1.0' | |
FROM = '[email protected]' | |
attr_reader :sendto | |
attr_accessor :remote_image_uri | |
def initialize | |
@sendto = %w{[email protected] [email protected]} # real users | |
# @sendto = %w{[email protected]} # debug users | |
end | |
def process | |
get_image_url | |
ensure_storage_dir_exists | |
fetch_image | |
send_comic | |
end | |
def get_image_url | |
doc = Hpricot(open(DOMAIN, "User-Agent" => USER_AGENT)) | |
@remote_image_uri = URI.join DOMAIN, (doc/"div.STR_Content a img").first['src'] | |
end | |
def local_file_name | |
File.join ROOT_DIR, "#{Time.now.strftime(FILENAME_FORMAT)}#{File.extname(remote_image_uri.path)}" | |
end | |
def ensure_storage_dir_exists | |
FileUtils.mkdir_p(File.dirname(local_file_name)) | |
end | |
def fetch_image | |
File.open(local_file_name, 'w') do |f| | |
@remote_image_uri.open do |img| | |
f.write img.read | |
end | |
end | |
end | |
def send_comic | |
# Create the image part | |
mail = RMail::Message.new | |
img = Base64.encode64 File.read(local_file_name) | |
mail.body = img | |
mail.header.set 'Content-Type', MIME::Types.type_for(File.basename(local_file_name)).to_s | |
mail.header.set 'Content-Disposition', 'inline', 'filename' => File.basename(local_file_name) | |
mail.header.set 'Content-transfer-encoding', 'base64' | |
mail.header.set 'Content-Id', File.basename(local_file_name) | |
# Create a plain text part | |
text = RMail::Message.new | |
text.body = "The daily dilbert is attached, but you can see it here if you really want to: #{remote_image_uri}" | |
text.header.set 'Content-Type', 'text/plain' | |
# Crate the HTML part | |
html = RMail::Message.new | |
html.header.set 'Content-Type', 'text/html' | |
html.body = <<-eos | |
<html> | |
<body> | |
<p><img src="cid:#{File.basename(local_file_name)}"></p> | |
</body> | |
</html> | |
eos | |
# The wrapper message | |
cont = RMail::Message.new | |
cont.header.subject = "Daily Dilbert" | |
cont.header.set 'Content-Type', 'multipart/related', 'boundary' => '---Daily Dilbert---' | |
cont.header.from = 'Dilbert-o-gram <[email protected]>' | |
cont.header.to = '[email protected]' | |
cont.add_part html | |
cont.add_part mail | |
cont.add_part text | |
# Sendit | |
Net::SMTP.start('localhost', 25) do |smtp| | |
smtp.ready(FROM, sendto) do |p| | |
p.puts cont.to_s | |
end | |
end | |
end | |
end | |
DilbertFetcher.new.process |
This file contains hidden or 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
What's wrong with it? | |
--------------------- | |
- Ported form a Perl script, lacks some Ruby idioms | |
- email creation is fugly |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment