Last active
August 29, 2015 14:03
-
-
Save fancyremarker/b99e508f40e09e1bb66b to your computer and use it in GitHub Desktop.
check-unread-chats
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
#!/usr/bin/env ruby | |
require 'net/http' | |
require 'nokogiri' | |
require 'yaml' | |
def credential_pairs_from_yaml | |
YAML.load(File.open("#{ENV['HOME']}/.google-credentials.yml")) | |
rescue | |
{} | |
end | |
def credential_pairs | |
if ARGV[0] && ARGV[1] | |
{ ARGV[0] => ARGV[1] } | |
else | |
credential_pairs_from_yaml | |
end | |
end | |
def unread_chats(username, password) | |
http = Net::HTTP.new('mail.google.com', 443) | |
http.use_ssl = true | |
request = Net::HTTP::Get.new('/mail/feed/atom/chats') | |
request.basic_auth username, password | |
response = http.request(request) | |
xml = Nokogiri::XML(response.body) | |
xml.search('entry') | |
end | |
def notify(message) | |
`osascript -e 'display notification "#{message}" with title "Unread Chats"'` | |
end | |
begin | |
credential_pairs.each do |username, password| | |
unread = unread_chats(username, password) | |
if unread.any? | |
notify "#{unread.count} unread for #{username}" | |
end | |
end | |
rescue | |
notify 'An error occurred in check-unread-chats' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
./check-unread-chats.rb <username> <password>
Or, enter a hash of username-password pairs into
$HOME/.google-credentials.yml
and invoke with no arguments. The script will create a new notification if you have any missed chats.