Skip to content

Instantly share code, notes, and snippets.

@migrs
Last active April 19, 2018 05:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save migrs/458100dad03bca1e2edbfd1fca95e6a8 to your computer and use it in GitHub Desktop.
Save migrs/458100dad03bca1e2edbfd1fca95e6a8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# <bitbar.title>AKASHI Stamps</bitbar.title>
# <bitbar.version>v1.0.0</bitbar.version>
# <bitbar.author>mig</bitbar.author>
# <bitbar.author.github>migrs</bitbar.author.github>
# <bitbar.desc></bitbar.desc>
# <bitbar.image></bitbar.image>
# <bitbar.dependencies>ruby</bitbar.dependencies>
# <bitbar.abouturl>https://github.com/migrs</bitbar.abouturl>
require 'net/http'
require 'uri'
require 'json'
require 'date'
begin
require 'dotenv'
Dotenv.load(File.dirname(__FILE__) + '/.env')
rescue LoadError
end
TOKEN = ENV["AKASHI_TOKEN"] || 'PUT-YOUR-AKASHI-API-TOKEN'
DTFMT = '%Y%m%d%H%M%S'
STAMP_ICON = { 11 => ':office:', 12 => ':house:', 31 => ':coffee:', 32 => ':computer:' }
STAMP_NAME = { 11 => '出勤', 12 => '退勤', 31 => '休憩', 32 => '復帰' }
STAMP_STATUS = { 11 => '勤務中', 31 => '休憩中', 32 => '勤務中' }
WEEK = %w(日 月 火 水 木 金 土)
icon = nil
def akashi_api method, path, params = {}
headers = { 'Accept' => 'application/json', 'Content-Type' => 'application/json' }
params[:token] ||= TOKEN
uri = URI "https://atnd.ak4.jp/api/cooperation/#{path.sub(/@/, 'OPENLOGI')}"
res = case method
when 'GET'
uri.query = URI.encode_www_form params
Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |_| _.get uri.request_uri, headers }
when 'POST'
Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |_| _.post uri.path, JSON.generate(params), headers }
end
json = JSON.parse res.body, symbolize_names: true
unless json[:success]
error = "#{json[:errors].first[:code]}: #{json[:errors].first[:message]}"
notify error
raise error
end
json[:response]
end
def li text = '---', args = {}
args = args.dup
fold = args.delete(:fold) ? '--' : ''
args_set = (alt = args.delete(:alt)) ? [args, args.merge(alt).merge(alternate: true)] : [args]
args_set.each do |_|
puts fold + _.delete(:prepend).to_s + (_.delete(:text) || text).to_s + _.delete(:append).to_s +
(_.empty? ? '' : ' | ' + _.select {|k,v|!v.nil?}.map {|k,v| "#{k}=#{v}"}.join(' '))
end
end
def notify message
case RUBY_PLATFORM
when /darwin/
`osascript -e 'display notification "#{message.strip.gsub(/$/, '\r')}" with title "AKASHI" '`
when /linux/
`notify-send AKASHI "#{message.strip.gsub(/$/, '\r')}"`
end
end
if ARGV.empty?
now = Time.now
day = 24 * 60 * 60
begin
result = akashi_api 'GET', '@/stamps', start_date: (now - 8 * day).strftime(DTFMT), end_date: now.strftime(DTFMT)
current_type = result[:stamps].last[:type]
rescue
li 'AKASHI Error!', color: '#ECB935', image: icon, imageWidth: 18
li
li $!, refresh: true
end
li "#{STAMP_ICON[current_type]}#{STAMP_STATUS[current_type]}", image: icon, imageWidth: 18
li
li "#{now.strftime('%Y/%m/%d %H:%M')} 更新", color: '#aaaa99', refresh: true
li 'AKASHI 打刻', href: 'https://atnd.ak4.jp/mypage/punch', color: '#4078C0'
params = { bash: __FILE__, param1: 'stamp', terminal: false, refresh: true }
case current_type
when 11,32 # working
li "#{STAMP_ICON[31]} #{STAMP_NAME[31]}する", params.merge(param2: 31)
li "#{STAMP_ICON[12]} #{STAMP_NAME[12]}する", params.merge(param2: 12)
when 31
li "#{STAMP_ICON[32]} #{STAMP_NAME[32]}する", params.merge(param2: 32)
else
li "#{STAMP_ICON[11]} #{STAMP_NAME[11]}する", params.merge(param2: 11)
end
li
li '出勤簿', color: "#888888"
result[:stamps].reverse.take(16).group_by{|_|_[:stamped_at].split(' ').first}.each do |_, stamps|
date = Date.parse(_)
li "#{_} (#{WEEK[date.wday]})", color: '#aaaa99'
stamps.each do |_|
li "#{STAMP_ICON[_[:type]]} #{_[:stamped_at][/(\d{2}:\d{2}):\d{2}$/,1]} #{STAMP_NAME[_[:type]]}", href: "https://atnd.ak4.jp/attendance"
end
end
else
case ARGV[0]
when 'stamp'
type = ARGV[1].to_i
exit 2 unless [11,12,31,32].include? type
akashi_api 'POST', '@/stamps', type: type
notify "#{STAMP_NAME[type]}を受け付けました"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment