-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgoogle-drive-permission-search.rb
executable file
·244 lines (207 loc) · 5.84 KB
/
google-drive-permission-search.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env ruby
require 'optparse'
require 'google/api_client'
require 'google/api_client/client_secrets'
require 'google/api_client/auth/file_storage'
require 'google/api_client/auth/installed_app'
require 'active_support/all'
require 'spreadsheet'
# set your application name
APPLICATION_NAME = 'google-drive-permission-search'
APPLICATION_VERSION = '1.0.0'
API_VERSION = 'v2'
CACHED_API_FILE = "drive-#{API_VERSION}.cache"
CREDENTIAL_STORE_FILE = "#{$0}-oauth2.json"
debug = false
# #setup is originated by https://github.com/google/google-api-ruby-client-samples/tree/master/drive
def setup()
client = Google::APIClient.new(:application_name => APPLICATION_NAME,
:application_version => APPLICATION_VERSION)
file_storage = nil
begin
file_storage = Google::APIClient::FileStorage.new(CREDENTIAL_STORE_FILE)
rescue URI::InvalidURIError
File.delete CREDENTIAL_STORE_FILE
file_storage = Google::APIClient::FileStorage.new(CREDENTIAL_STORE_FILE)
end
if file_storage.authorization.nil?
client_secrets = Google::APIClient::ClientSecrets.load
flow = Google::APIClient::InstalledAppFlow.new(
:client_id => client_secrets.client_id,
:client_secret => client_secrets.client_secret,
:scope => ['https://www.googleapis.com/auth/drive']
)
client.authorization = flow.authorize(file_storage)
else
client.authorization = file_storage.authorization
end
drive = nil
if File.exists? CACHED_API_FILE
File.open(CACHED_API_FILE) do |file|
drive = Marshal.load(file)
end
else
drive = client.discovered_api('drive', API_VERSION)
File.open(CACHED_API_FILE, 'w') do |file|
Marshal.dump(drive, file)
end
end
return client, drive
end
def get_files(client, drive)
result = client.execute(
api_method: drive.files.list,
parameters: {
maxResults: 1000,
},
)
# jj result.data.to_hash
result
end
class OutputAdapter
attr_accessor :data
def initialize
@data = []
end
# object[row, col] = cel_value
def []=(row, col, val)
@data[row.to_i] = [] if @data[row.to_i].blank?
@data[row.to_i][col.to_i] = val
end
# abstract
def save
end
end
class ExcelOutputAdapter < OutputAdapter
attr_accessor :file_name
def initialize(file_name)
super()
@file_name = file_name
end
# Excelの場合は改行が入る
def []=(row, col, val)
@data[row.to_i] = [] if @data[row.to_i].blank?
if val.instance_of? Array
@data[row.to_i][col.to_i] = val.join("\n")
else
@data[row.to_i][col.to_i] = val.to_s
end
end
def save
Spreadsheet.client_encoding = 'UTF-8'
book = Spreadsheet::Workbook.new
sheet = book.create_worksheet
sheet.row(0).concat %w{title kind mimeType id owner permissions}
row_index = 1
puts data.inspect
@data.each do |row|
sheet[row_index, 0] = row[0]
sheet[row_index, 1] = row[1]
sheet[row_index, 2] = row[2]
sheet[row_index, 3] = row[3]
sheet[row_index, 4] = row[4]
sheet[row_index, 5] = row[5]
row_index += 1
end
book.write @file_name
end
end
class TsvOutputAdapter < OutputAdapter
attr_accessor :file_name
def initialize(file_name)
super()
@file_name = file_name
end
# TSVでは改行が入らないのでカンマ区切りで突っ込む
def []=(row, col, val)
@data[row.to_i] = [] if @data[row.to_i].blank?
if val.instance_of? Array
@data[row.to_i][col.to_i] = ''
val.each do |v|
@data[row.to_i][col.to_i] << v.gsub(/[\r\n]/, '')
@data[row.to_i][col.to_i] << ","
end
else
@data[row.to_i][col.to_i] = val.to_s
end
end
def save
File.open(@file_name, 'w') do |fp|
fp.write (%w{title kind mimeType id owner permissions}).join("\t") + "\n"
@data.each do |row|
fp.write row[0] + "\t"
fp.write row[1] + "\t"
fp.write row[2] + "\t"
fp.write row[3] + "\t"
fp.write row[4] + "\t"
fp.write row[5] + "\n"
end
end
end
end
# begin parse options
opt = OptionParser.new
opt.on('-v', '--verbose') do |v|
debug = true
end
file_name = nil
opt.on('-f FILENAME') do |name|
file_name = name
end
output = nil
opt.on('--type [TYPE]') do |type|
case type
when 'excel'
output = ExcelOutputAdapter.new(file_name.presence || 'result.xls')
when 'tsv'
output = TsvOutputAdapter.new(file_name.presence || 'result.tsv')
else
puts 'You must specify file type options ""--type (xls|tsv) "'
exit
end
end
filter_string = nil
opt.on('--only-includes NAME') do |name|
filter_string = name
end
opt.parse!(ARGV)
# end parse options
client, drive = setup()
row_index = 0
all_files_result = get_files(client, drive)
all_files_result.data.items.each do |file|
if debug
STDERR.puts "fetching id: #{file.id}, title: #{file.title}..."
end
# get owners
owners = []
file.owners.each do |owner|
owners << "#{owner.try(:display_name)} <#{owner.try(:email_address)}>"
end
# get permissions
permission_result = client.execute(
:api_method => drive.permissions.list,
:parameters => { 'fileId' => file.id }
)
permissions = []
permission_result.data.items.each do |permission|
permissions << "#{permission.role}:#{permission.name} <#{permission.try(:email_address)}>"
end
if filter_string.present?
available = false
[owners, permissions].flatten.each do |str|
if str.include?(filter_string)
available = true
end
end
next unless available
end
output[row_index, 0] = file.title
output[row_index, 1] = file.kind
output[row_index, 2] = file.mimeType
output[row_index, 3] = file.id
output[row_index, 4] = owners
output[row_index, 5] = permissions
row_index += 1
end
output.save