3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

gmail に送ったURL付きメールのURLをブラウザから開く

Last updated at Posted at 2013-01-16

前提

  • Mac OS を利用している

概要

Twitter にiPhoneからアクセスしていて、気になった情報をメモ代わりにgmailに送ってはPCであとから開く、といったことをしていた。

今回、PCで開く作業を簡単にだが自動化してみる。

手順

おおまかな手順としては

  1. gmailにアクセスし、自分が送った未読メールを取得
  2. メールの中からURL(t.coで短縮されたもの)を抜き出す
  3. URLをWebブラウザ(Google Chrome)に渡す

ターミナルからアプリケーションを起動

open コマンドを使う

-aオプションで関連付けられていないアプリケーションを指定してファイルやディレクトリ、URIを開くことが出来る。

open -a '/Applications/Google Chrome.app' http://google.com/

RubyからGoogle Chromeを起動

systemメソッドを利用する。

open_chrome.rb
URI = 'http://google.com/'
APPLICATION = '/Applications/Google\ Chrome.app' # バックスラッシュはスペースのエスケープ

system "open -a #{APPLICATION} #{URI}"

Ruby でメールチェック

ruby を使ってメール機能を扱うにはいくつか方法があるが、gemを利用するのが楽でいいです。

POP通信

標準ライブラリnet/popを利用する。

mail_check.rb
require 'net/pop'

SERVER = 'mail.example.com'
ACCOUNT = 'example@example.com'
PASSWORD = 'password'
PORT = Net::POP3.default_port
# PORT = Net::POP3.default_pop3s_port

pop = Net::POP3.new SERVER
pop.start ACCOUNT, PASSWORD

# save to file
i = 0
if pop.mails.empty?
  puts 'No mail.'
else
  puts "You have #{pop.mail.size} new messages."
  pop.mails.each do |m|
    File.open('/Users/JohnDoe/Desktop/inbox/#{i}.eml', 'w'){ |f|
      m.pop f
    }
    i += 1
  end
end

IMAP 通信

標準ライブラリnet/imapを利用する。

ログインあれこれ

imap_mailChecker.rb
require 'net/imap'

SERVER   = 'imap.example.com'
ACCOUNT  = 'example@example.com'
PASSWORD = 'password'
PORT     = 993
USE_SSL  = true

# Net::IMAP.new(host, port = 143, usessl = false, certs = nil, verify = false)
imap = Net::IMAP.new SERVER, PORT, USE_SSL

imap.authenticate 'LOGIN', ACCOUNT, PASSWORD
=begin
imap.authenticate(auth_type, user, password)
auth_type:
  'LOGIN'
  'PLAIN'
  'CRAM-MD5'
  'DIGEST-MD5'
=end

=begin
暗号化とか使わない:
imap = Net::IMAP.new SERVER
imap.login(ACCOUNT, PASSWORD)
=end

ただ、接続先がGmail の場合、Rubyの標準ライブラリで用意されている認証方式はGmailが対応している認証方式と一致しない。


Gem Gmail を利用してGmailにアクセスする。

gmail-imap_mailChecker.rb
require 'gmail'

ACCOUNT  = 'example@example.com'
PASSWORD = 'password'

gmail = Gmail.connect(ACCOUNT, PASSWORD)

取得メールからURIを解析

URI を正規表現で取り出す

自分のケータイから送られてきたTwitterのツイートからURI(t.co から始まるアドレス)を抜き出す。

regexp = /http:\/\/t\.co\/\w+/
text = 'i lov it! pls check it out: http://t.co/example'

regexp.match text #=> http://t.co/example

解析したアドレスを勝手に開く

先ほどのsystem メソッドを使って、Google Chrome にURL(t.co)を渡してあげる

メールの操作とWebブラウザの起動を組み合わせて

mailUriOpener.rb
require 'gmail'

APPLICATION = '/Applications/Google\ Chrome.app'

ACCOUNT  = 'example@example.com'
PASSWORD = 'password'

MAIL_FROM = 'example2@example.com'

regexp = /http:\/\/t\.co\/\w+/

gmail = Gmail.connect ACCOUNT, PASSWORD

gmail.inbox.emails(:unread, from: MAIL_FROM).map do |mail|
  text = mail.body
  uri = regexp.match(text.to_s) if text =~ regexp
  unless uri.nil?
    mail.delete!
    system "open -a #{APPLICATION} #{uri}"
  end
  sleep 6
end

リンク

3
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?