Skip to content

Instantly share code, notes, and snippets.

@tenforward
Last active October 12, 2015 08:28
Show Gist options
  • Save tenforward/3999434 to your computer and use it in GitHub Desktop.
Save tenforward/3999434 to your computer and use it in GitHub Desktop.
NTLM認証のサイボウズガルーンから予定をCSVで取得するサンプルスクリプト
#!/usr/bin/ruby
# -*- coding: utf-8 -*-
require 'rubygems'
require 'ntlm/http'
require 'kconv'
require 'date'
require 'csv'
class Garoon
# ホスト名
CYBOZU_HOST='garoon_host'
# CSV 取得用画面パス
CYBOZU_PATH1='/scripts/cbgrn/grn.exe/schedule/personal/export1?'
# CSV パス
CYBOZU_PATH2='/scripts/cbgrn/grn.exe/schedule/personal/command_export1/-/schedules.csv?&.csv'
def initialize(user, pass, domain)
@user = user
@pass = pass
@domain = domain
end
def getCsv(year, month, start_day, end_day)
# CSV 取得の画面へ一度アクセス
res1 = self.get
# セッション ID 取得
cookie = res1['Set-Cookie']
/(CBSESSID=\w+); .*/ =~ cookie
sessid = $1
# CSRF 対策のチケットを取得
/<input type=\"hidden\" name=\"csrf_ticket\" value=\"(.*)\">/ =~ res1.body
csrf_ticket = $1
req = Net::HTTP::Post.new(CYBOZU_PATH2)
req.ntlm_auth(@user, @domain, @pass)
req.add_field("Cookie", cookie)
req.add_field("Referer", "http://" + CYBOZU_HOST + CYBOZU_PATH1)
req.add_field("Origin", "http://" + CYBOZU_HOST)
req.set_form_data({'csrf_ticket' => csrf_ticket,
'start_year' => year,
'start_month' => month,
'start_day' => start_day,
'end_year' => year,
'end_month' => month,
'end_day' => end_day,
'charset' => 'UTF-8',
'item_name' => '0'})
@http.request(req).body
end
def get
@http = Net::HTTP.new(CYBOZU_HOST)
req = Net::HTTP::Get.new(CYBOZU_PATH1)
req.ntlm_auth(@user, @domain, @pass)
return @http.request(req)
end
end
# クラス作成
garoon = Garoon.new("user", "pass", "DOMAIN")
# 今日と月末の取得
today = Date.today
eom = Date.new(today.year, today.month, -1)
# CSV 取得
csv = garoon.getCsv(today.year, today.month, "1", eom.day.to_s).body
opt = {:col_sep => ",", :row_sep => "\r\n", :quote_char => "\"", :encoding => "UTF-8"}
CSV.parse(csv, opt) {|row|
row.each {|f|
print f, " "
}
print "\n"
}
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment