earthquakeのつぶやきをGrowlで通知する

Posted by @kachina_t on Wed, Feb 11, 2015
In Development
Tags development

earthquakeにストリームされるつぶやきをGrowlで通知する方法です。

1. Growlのインストール

App Storeから Growl をインストール

2. growlnotifyのインストール

$ brew install Caskroom/cask/growlnotify

3. スクリプトの設置

~/.earthquake/plugin/growlnotify.rb に以下のファイルを作成

require "fileutils"
require "digest/md5"

Earthquake.init do
  dir = File.join(File.dirname(__FILE__), "userimage")
  output do |item|
    next if item.nil? || item["user"].nil? || item["_stream"].nil?
    begin
      image_url = item["user"]["profile_image_url"]
      next unless image_url
      name = Digest::MD5.hexdigest image_url
      path = File.join(dir, name.chars.first, name)
      FileUtils.mkdir_p(File.dirname(path))
      arg = {
        "-t" => "#{item["user"]["screen_name"]}(earthquake.gem)",
        "-m" => item["text"],
        "--image" => path,
      }
      EM.defer(
        lambda {
          return path if File.exists?(path)
          open(path, "w"){|f|
            f.print open(image_url).read
          }
          path
        },
        lambda {|path|
          system('growlnotify', *arg.to_a.flatten)
        }
      )
    end
  end
end

4. Growlの設定

デフォルトでは OS X NotificationsON になっています。 これだとユーザーアイコンが表示されなかったので OFF にします。

Growl Preferences

以上で、earthquakeのストリームがGrowlで通知されるようになりました。 OS X Notifications でアイコンも表示できるようにできるか調べてみよう。