Skip to content

Instantly share code, notes, and snippets.

@igjit
Last active October 17, 2019 10:07
Show Gist options
  • Save igjit/65c71077f590382673977d1d43eb99b9 to your computer and use it in GitHub Desktop.
Save igjit/65c71077f590382673977d1d43eb99b9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'net/http'
require 'uri'
require 'json'
PROFILES = {
coding: {
status_text: 'コード書いてる',
status_emoji: ':desktop_computer:'
},
terminal: {
status_text: '黒い画面でなんかやってる',
status_emoji: ':desktop_computer:'
},
reviewing: {
status_text: 'コードレビューしてる',
status_emoji: ':octocat:'
},
slack: {
status_text: 'Slack見てる',
status_emoji: ':slack_new:'
},
pivotal: {
status_text: 'Pivotal見てる',
status_emoji: ':ballot_box_with_check:'
},
confluence: {
status_text: 'コンフル見てる',
status_emoji: ':confluencce:'
},
emacs_config: {
status_text: 'エディタの設定してる',
status_emoji: ':gnu_emacs:'
},
r: {
status_text: 'R書いてる',
status_emoji: ':r:'
},
active: {
status_text: 'PCの前にいる',
status_emoji: ':raising_hand:'
},
away: {
status_text: 'PCの前にいない',
status_emoji: ':double_vertical_bar:'
},
halt: {
status_text: 'おやすみー',
status_emoji: ':sleeping:'
}
}.freeze
def active_window_name
script = <<~EOS
tell application "System Events"
set frontApp to name of first application process whose frontmost is true
end tell
tell application frontApp
if the (count of windows) is not 0 then
set window_name to name of front window
end if
end tell
return {frontApp, window_name}
EOS
`osascript -e '#{script}'`.strip
end
def locked?
script = 'import Quartz; print Quartz.CGSessionCopyCurrentDictionary()'
`/usr/bin/python -c '#{script}'`.match? /CGSSessionScreenIsLocked = 1/
end
def status_for(window_name)
case window_name
when /\A(?:Mozilla Firefox|Chromium|Google Chrome), (.+)\z/
browser_status Regexp.last_match(1)
when /\ATerminal, /
:terminal
when /\AEmacs, (.+)\z/
emacs_status Regexp.last_match(1)
when /vim/i
:coding
else
:active
end
end
def browser_status(title)
case title
when /\APull Requests · /
:reviewing
when / · Pull Request #\d+/, /\Aプルリクエスト#\d+/
:reviewing
when /\A\S+ \| .+ Slack\z/
:slack
when /- Pivotal Tracker\z/
:pivotal
when /- Confluence\z/
:confluence
else
:active
end
end
def emacs_status(title)
case title
when /.+\.el\z/
:emacs_config
when /.+\.R\z/i, /\A\*R:/
:r
else
:coding
end
end
def current_status
if locked?
:away
else
status_for active_window_name
end
end
def post_profile!(token, status = nil)
status ||= current_status
profile = PROFILES[status.to_sym]
raise 'Unknown status' unless profile
uri = URI.parse 'https://slack.com/api/users.profile.set'
Net::HTTP.post_form(uri, token: token, profile: profile.to_json)
end
def main
token = ENV['SLACK_API_TOKEN']
raise 'SLACK_API_TOKEN is not specified' unless token
status, = ARGV
res = post_profile!(token, status)
exit res.is_a? Net::HTTPOK
end
if __FILE__ == $0
main
end
#!/bin/bash
set -e
dir=.
cd "$dir"
source .env
export SLACK_API_TOKEN="$SLACK_API_TOKEN"
watch -n 20 ./slack_status.rb && ./slack_status.rb halt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment