slackup 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 227551c418ddd3f0a6eeabe2fd4695302f336bad
4
- data.tar.gz: d1df8e777fd3f2a3056cb52bee0e81c671851455
3
+ metadata.gz: e065eb49b0187dd348830f1c73a3f0548e7a2906
4
+ data.tar.gz: ff1f53897bb4911be878785dbdc2d954abec5596
5
5
  SHA512:
6
- metadata.gz: 6195c397e38e9c35938c59b83d36befd34ed4953d2eed8ff0c7e1a5636379294c7aad8e3da94a5b2b6c3c0c4b99ddd4fd2cc2c58829fc3f55f4141d4d8b22733
7
- data.tar.gz: 255e5b4e693b5717f3533eeb5b97c6bb6d40af1782f8948f50444b3f5c595b130361ffba1f4ab5db6f2f3ec08e6deba492010dcb0a2efb8d724b1c0896ecd351
6
+ metadata.gz: 686a4114820035ef6a59ca866811ab93ce4f3ed72d1206f08c3c3213dd7c77d03e6b5d3175d39ddef57878729472443f93b4e405e94d372859955de56c03e939
7
+ data.tar.gz: 3e02401f1c62c5613d8faf0f9af2685a59f49d25b7be35f28bc89e3e351fcc45f97396182b829832f1339626820f84626b7e63905c3c753f54853e3c40b85ea1
@@ -0,0 +1,26 @@
1
+ class Slackup::Channels < Slackup
2
+
3
+ def list
4
+ @list ||= client.channels_list["channels"]
5
+ end
6
+ alias channels list
7
+
8
+ def write!
9
+ channels.each do |channel|
10
+ write_messages(channel)
11
+ end
12
+ end
13
+
14
+ def messages(channel)
15
+ client.channels_history(channel: channel["id"], count: "1000")
16
+ end
17
+
18
+ def write_messages(channel)
19
+ with_messages channel["name_normalized"], messages(channel) do |messages|
20
+ File.open(backup_filename(channel["name"]), "w") do |f|
21
+ formatted_messages = format_messages(messages)
22
+ f.write serialize(formatted_messages)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,61 @@
1
+ class Slackup::Groups < Slackup
2
+
3
+ # {
4
+ # "ok": true,
5
+ # "groups": [
6
+ # {
7
+ # "id": "G0ABC",
8
+ # "name": "some-group",
9
+ # "is_group": true,
10
+ # "created": 1436923155,
11
+ # "creator": "UABC",
12
+ # "is_archived": false,
13
+ # "members": [
14
+ # ],
15
+ # "topic": {
16
+ # "value": "",
17
+ # "creator": "",
18
+ # "last_set": 0
19
+ # },
20
+ # "purpose": {
21
+ # "value": "Some group",
22
+ # "creator": "UABC",
23
+ # "last_set": 1437105751
24
+ # }
25
+ # }
26
+ # ]
27
+ # }
28
+ def list
29
+ @list ||= client.groups_list["groups"]
30
+ end
31
+ alias groups list
32
+
33
+ def write!
34
+ Dir.chdir(groups_dir) do
35
+ groups.each do |group|
36
+ write_messages(group)
37
+ end
38
+ end
39
+ end
40
+
41
+ def messages(group)
42
+ client.groups_history(channel: group["id"], count: "1000")
43
+ end
44
+
45
+ # https://api.slack.com/methods/groups.history
46
+ def write_messages(group)
47
+ with_messages group, messages(group) do |messages|
48
+ File.open(backup_filename(group["name"]), "w") do |f|
49
+ formatted_messages = format_messages(messages)
50
+ f.write serialize(formatted_messages)
51
+ end
52
+ end
53
+ end
54
+
55
+ def groups_dir
56
+ @groups_dir ||= "groups"
57
+ FileUtils.mkdir_p(@groups_dir)
58
+ @groups_dir
59
+ end
60
+
61
+ end
@@ -0,0 +1,66 @@
1
+ class Slackup::Ims < Slackup
2
+ Im = Struct.new(:im_hash) do
3
+ def id; im_hash["id"]; end
4
+
5
+ def user; im_hash["user"]; end
6
+ end
7
+ # @return [Hash]
8
+ # @example
9
+ # {
10
+ # "ok"=>true,
11
+ # "ims"=>[
12
+ # {"id"=>"D1234567890", "is_im"=>true, "user"=>"USLACKBOT", "created"=>1372105335, "is_user_deleted"=>false},
13
+ # ]
14
+ # }
15
+ def list
16
+ @list ||= client.im_list["ims"].map { |im| Im.new(im) }
17
+ end
18
+ alias ims list
19
+
20
+ def write!
21
+ Dir.chdir(ims_dir) do
22
+ ims.each do |im|
23
+ write_messages(im)
24
+ end
25
+ end
26
+ end
27
+
28
+ # @param im_id [String] is the 'channel' of the im, e.g. "D1234567890"
29
+ # @return [Hash]
30
+ # @example return
31
+ # {
32
+ # "ok": true,
33
+ # "latest": "1358547726.000003",
34
+ # "messages": [
35
+ # {
36
+ # "type": "message",
37
+ # "ts": "1358546515.000008",
38
+ # "user": "U2147483896",
39
+ # "text": "<@U0453RHGQ> has some thoughts on that kind of stuff"
40
+ # },
41
+ # ]
42
+ # "has_more": false
43
+ def messages(im_id)
44
+ client.im_history(channel: im_id)
45
+ end
46
+
47
+ def write_messages(im)
48
+ im_username = format_username(im.user)
49
+ with_messages im_username, messages(im.id) do |messages|
50
+ formatted_messages = format_messages(messages)
51
+ File.open(backup_filename(im_username), "w") do |f|
52
+ f.write serialize(formatted_messages)
53
+ end unless formatted_messages.empty?
54
+ end
55
+ end
56
+
57
+ def format_username(user)
58
+ user_name(user).downcase.gsub(/\s+/, "-")
59
+ end
60
+
61
+ def ims_dir
62
+ @ims_dir ||= "ims"
63
+ FileUtils.mkdir_p(@ims_dir)
64
+ @ims_dir
65
+ end
66
+ end
@@ -0,0 +1,15 @@
1
+ class Slackup::Stars < Slackup
2
+
3
+ def list
4
+ client.stars_list(count: "1000", page: "1")
5
+ end
6
+ alias stars list
7
+
8
+ def write!
9
+ with_messages "stars", list do |messages|
10
+ File.open(backup_filename("stars"), "w") do |f|
11
+ f.write(serialize(messages))
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,71 @@
1
+ class Slackup::Users < Slackup
2
+ User = Struct.new(:user_hash) do
3
+ def id; user_hash["id"]; end
4
+
5
+ def name; user_hash["name"]; end
6
+
7
+ def deleted; user_hash["deleted"]; end
8
+
9
+ def color; user_hash["color"]; end
10
+
11
+ def profile; user_hash["profile"]; end
12
+
13
+ def admin?; user_hash["is_admin"]; end
14
+
15
+ def owner?; user_hash["is_owner"]; end
16
+
17
+ def has_2fa?; user_hash["has_2fa"]; end
18
+
19
+ def has_files?; user_hash["has_files"]; end
20
+
21
+ def to_hash; user_hash; end
22
+ end
23
+ # {
24
+ # "ok": true,
25
+ # "members": [
26
+ # {
27
+ # "id": "U023BECGF",
28
+ # "name": "bobby",
29
+ # "deleted": false,
30
+ # "color": "9f69e7",
31
+ # "profile": {
32
+ # "first_name": "Bobby",
33
+ # "last_name": "Tables",
34
+ # "real_name": "Bobby Tables",
35
+ # "email": "bobby@slack.com",
36
+ # "skype": "my-skype-name",
37
+ # "phone": "+1 (123) 456 7890",
38
+ # "image_24": "https:\/\/...",
39
+ # "image_32": "https:\/\/...",
40
+ # "image_48": "https:\/\/...",
41
+ # "image_72": "https:\/\/...",
42
+ # "image_192": "https:\/\/..."
43
+ # },
44
+ # "is_admin": true,
45
+ # "is_owner": true,
46
+ # "has_2fa": false,
47
+ # "has_files": true
48
+ # },
49
+ # ]
50
+ # }
51
+ def list
52
+ @list ||= client.users_list["members"].map { |member| User.new(member) }
53
+ end
54
+ alias users list
55
+
56
+ def write!
57
+ File.open(backup_filename("users"), "w") do |f|
58
+ f.write(serialize(users.map(&:to_hash)))
59
+ end
60
+ end
61
+
62
+ # gets user name for an id, if mapping is known, else returns the input
63
+ def user_name(user_id)
64
+ @user_names ||= users.each_with_object({}) {|user, lookup|
65
+ lookup[user.id] = user.name
66
+ }
67
+ @user_names.fetch(user_id) {
68
+ user_id
69
+ }
70
+ end
71
+ end
@@ -1,3 +1,3 @@
1
- module Slackup
2
- VERSION = "0.0.6"
1
+ class Slackup
2
+ VERSION = "0.0.7"
3
3
  end
data/lib/slackup.rb CHANGED
@@ -3,10 +3,15 @@
3
3
  require "pathname"
4
4
  require "yaml"
5
5
  require "fileutils"
6
- gem "slack-api", "~> 1.1", ">= 1.1.3"
6
+ gem "slack-api", "~> 1.6", ">= 1.6.0"
7
7
  require "slack"
8
-
9
8
  class Slackup
9
+ require_relative 'slackup/users'
10
+ require_relative 'slackup/channels'
11
+ require_relative 'slackup/groups'
12
+ require_relative 'slackup/ims'
13
+ require_relative 'slackup/stars'
14
+
10
15
  Error = Class.new(StandardError)
11
16
  RUN_ROOT = Pathname Dir.pwd
12
17
  def self.run_root
@@ -14,12 +19,6 @@ class Slackup
14
19
  end
15
20
 
16
21
  SEMAPHORE = Mutex.new
17
- attr_reader :name
18
- def initialize(name, token)
19
- @name = name
20
- @token = token
21
- FileUtils.mkdir_p(name)
22
- end
23
22
 
24
23
  def self.team_token_pairs_file
25
24
  Pathname.glob(run_root.join("slack_teams.{json,yml,yaml}")).first
@@ -33,42 +32,53 @@ class Slackup
33
32
  end
34
33
  end
35
34
 
35
+ def self.configure_client(token)
36
+ client = nil
37
+ SEMAPHORE.synchronize do
38
+ Slack.configure do |config|
39
+ config.token = token
40
+ end
41
+ client = Slack.client
42
+ end
43
+ client
44
+ end
45
+
36
46
  def self.backup(team_token_pairs = team_token_pairs())
37
47
  team_token_pairs.each do |name, token|
38
- new(name, token).execute
48
+ fork do
49
+ client = configure_client(token)
50
+ new(name, client).execute
51
+ end
39
52
  end
53
+ p Process.waitall
54
+ end
55
+
56
+ attr_reader :name, :client
57
+ alias dirname name
58
+ def initialize(name, client)
59
+ @name = name
60
+ @client = client
61
+ FileUtils.mkdir_p(name)
40
62
  end
41
63
 
42
64
  def execute
43
- SEMAPHORE.synchronize do
44
- authorize! &&
45
- Dir.chdir(name) do
46
- channels.each do |channel|
47
- write_channel_messages(channel)
48
- end
49
- Dir.chdir(groups_dir) do
50
- groups.each do |group|
51
- write_group_messages(group)
52
- end
53
- end
54
- write_stars
55
- write_users
56
- Dir.chdir(ims_dir) do
57
- im_list.each do |im|
58
- write_im_messages(im)
59
- end
60
- end
61
- end
65
+ authorize! && write!
66
+ end
67
+
68
+ def write!
69
+ Dir.chdir(dirname) do
70
+ Channels.new(name, client).write!
71
+ Groups.new(name, client).write!
72
+ Stars.new(name, client).write!
73
+ user_client.write!
74
+ Ims.new(name, client).write!
62
75
  end
63
76
  end
64
77
 
65
78
  private
66
79
 
67
80
  def authorize!
68
- Slack.configure do |config|
69
- config.token = @token
70
- end
71
- auth_test = Slack.auth_test
81
+ auth_test = client.auth_test
72
82
  if auth_test["ok"] === true
73
83
  p [name]
74
84
  true
@@ -78,146 +88,23 @@ class Slackup
78
88
  end
79
89
  end
80
90
 
81
- User = Struct.new(:user_hash) do
82
- def id; user_hash["id"]; end
83
-
84
- def name; user_hash["name"]; end
85
-
86
- def deleted; user_hash["deleted"]; end
87
-
88
- def color; user_hash["color"]; end
89
-
90
- def profile; user_hash["profile"]; end
91
-
92
- def admin?; user_hash["is_admin"]; end
93
-
94
- def owner?; user_hash["is_owner"]; end
95
-
96
- def has_2fa?; user_hash["has_2fa"]; end
97
-
98
- def has_files?; user_hash["has_files"]; end
99
-
100
- def to_hash; user_hash; end
101
- end
102
- # {
103
- # "ok": true,
104
- # "members": [
105
- # {
106
- # "id": "U023BECGF",
107
- # "name": "bobby",
108
- # "deleted": false,
109
- # "color": "9f69e7",
110
- # "profile": {
111
- # "first_name": "Bobby",
112
- # "last_name": "Tables",
113
- # "real_name": "Bobby Tables",
114
- # "email": "bobby@slack.com",
115
- # "skype": "my-skype-name",
116
- # "phone": "+1 (123) 456 7890",
117
- # "image_24": "https:\/\/...",
118
- # "image_32": "https:\/\/...",
119
- # "image_48": "https:\/\/...",
120
- # "image_72": "https:\/\/...",
121
- # "image_192": "https:\/\/..."
122
- # },
123
- # "is_admin": true,
124
- # "is_owner": true,
125
- # "has_2fa": false,
126
- # "has_files": true
127
- # },
128
- # ]
129
- # }
130
- def users
131
- @users ||= Slack.users_list["members"].map { |member| User.new(member) }
132
- end
133
-
134
- def channels
135
- @channels ||= Slack.channels_list["channels"]
136
- end
137
-
138
- # {
139
- # "ok": true,
140
- # "groups": [
141
- # {
142
- # "id": "G0ABC",
143
- # "name": "some-group",
144
- # "is_group": true,
145
- # "created": 1436923155,
146
- # "creator": "UABC",
147
- # "is_archived": false,
148
- # "members": [
149
- # ],
150
- # "topic": {
151
- # "value": "",
152
- # "creator": "",
153
- # "last_set": 0
154
- # },
155
- # "purpose": {
156
- # "value": "Some group",
157
- # "creator": "UABC",
158
- # "last_set": 1437105751
159
- # }
160
- # }
161
- # ]
162
- # }
163
- def groups
164
- @groups ||= Slack.groups_list["groups"]
165
- end
166
-
167
- Im = Struct.new(:im_hash) do
168
- def id; im_hash["id"]; end
169
-
170
- def user; im_hash["user"]; end
171
- end
172
- # @return [Hash]
173
- # @example
174
- # {
175
- # "ok"=>true,
176
- # "ims"=>[
177
- # {"id"=>"D1234567890", "is_im"=>true, "user"=>"USLACKBOT", "created"=>1372105335, "is_user_deleted"=>false},
178
- # ]
179
- # }
180
- def im_list
181
- @im_list ||= Slack.im_list["ims"].map { |im| Im.new(im) }
182
- end
183
-
184
- # @param im_id [String] is the 'channel' of the im, e.g. "D1234567890"
185
- # @return [Hash]
186
- # @example return
187
- # {
188
- # "ok": true,
189
- # "latest": "1358547726.000003",
190
- # "messages": [
191
- # {
192
- # "type": "message",
193
- # "ts": "1358546515.000008",
194
- # "user": "U2147483896",
195
- # "text": "<@U0453RHGQ> has some thoughts on that kind of stuff"
196
- # },
197
- # ]
198
- # "has_more": false
199
- def im_history(im_id)
200
- Slack.im_history(channel: im_id)
201
- end
202
-
203
- def write_channel_messages(channel)
204
- messages = Slack.channels_history(channel: channel["id"], count: "1000")["messages"]
205
- File.open(backup_filename(channel["name"]), "w") do |f|
206
- formatted_messages = format_channel_messages(messages)
207
- f.write serialize(formatted_messages)
208
- end
209
- end
210
-
211
- # https://api.slack.com/methods/groups.history
212
- def write_group_messages(group)
213
- messages = Slack.groups_history(channel: group["id"], count: "1000")["messages"]
214
- File.open(backup_filename(group["name"]), "w") do |f|
215
- formatted_messages = format_group_messages(messages)
216
- f.write serialize(formatted_messages)
91
+ # {"ok"=>false, "error"=>"ratelimited"}
92
+ # {"ok"=>false, "error"=>"token_revoked"
93
+ def with_messages(name, query_result)
94
+ if query_result["ok"]
95
+ yield query_result["messages"]
96
+ else
97
+ error = query_result["error"]
98
+ $stderr.puts "#{name}, error: #{error}"
99
+ exit 1 if error =~ /ratelimited/
217
100
  end
218
101
  end
219
102
 
220
103
  def format_messages(messages)
104
+ if messages.nil?
105
+ $stderr.puts "Messages nil #{caller[0..1]}"
106
+ return []
107
+ end
221
108
  messages.reverse.map { |msg|
222
109
  if msg.has_key?("text") && msg.has_key?("user")
223
110
  msg["user"] = user_name(msg["user"])
@@ -232,59 +119,22 @@ class Slackup
232
119
  }.compact
233
120
  end
234
121
 
235
- alias_method :format_channel_messages, :format_messages
236
- alias_method :format_group_messages, :format_messages
237
- alias_method :format_im_messages, :format_messages
238
-
239
- # gets user name for an id, if mapping is known, else returns the input
240
- def user_name(user_id)
241
- @user_names ||= users.each_with_object({}) {|user, lookup|
242
- lookup[user.id] = user.name
243
- }
244
- @user_names.fetch(user_id) {
245
- user_id
246
- }
122
+ def user_client
123
+ @user_client ||= Users.new(name, client)
247
124
  end
248
125
 
249
- def write_stars
250
- File.open(backup_filename("stars"), "w") do |f|
251
- stars = Slack.stars_list(count: "1000", page: "1")
252
- f.write(serialize(stars))
253
- end
126
+ def users
127
+ user_client.users
254
128
  end
255
129
 
256
- def write_users
257
- File.open(backup_filename("users"), "w") do |f|
258
- f.write(serialize(users.map(&:to_hash)))
259
- end
130
+ def user_name(user_id)
131
+ user_client.user_name(user_id)
260
132
  end
261
133
 
262
134
  def serialize(obj)
263
135
  obj.to_yaml
264
136
  end
265
137
 
266
- def write_im_messages(im)
267
- messages = im_history(im.id)["messages"]
268
- im_username = user_name(im.user).downcase.gsub(/\s+/, "-")
269
- formatted_messages = format_im_messages(messages)
270
- return if formatted_messages.empty?
271
- File.open(backup_filename(im_username), "w") do |f|
272
- f.write serialize(formatted_messages)
273
- end
274
- end
275
-
276
- def ims_dir
277
- @ims_dir ||= "ims"
278
- FileUtils.mkdir_p(@ims_dir)
279
- @ims_dir
280
- end
281
-
282
- def groups_dir
283
- @groups_dir ||= "groups"
284
- FileUtils.mkdir_p(@groups_dir)
285
- @groups_dir
286
- end
287
-
288
138
  def backup_filename(name)
289
139
  "#{name}.yml"
290
140
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slackup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Fleischer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-17 00:00:00.000000000 Z
11
+ date: 2017-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slack-api
@@ -74,6 +74,11 @@ files:
74
74
  - Rakefile
75
75
  - exe/slackup
76
76
  - lib/slackup.rb
77
+ - lib/slackup/channels.rb
78
+ - lib/slackup/groups.rb
79
+ - lib/slackup/ims.rb
80
+ - lib/slackup/stars.rb
81
+ - lib/slackup/users.rb
77
82
  - lib/slackup/version.rb
78
83
  - slackup.gemspec
79
84
  homepage: https://github.com/bf4/slackup
@@ -96,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
101
  version: '0'
97
102
  requirements: []
98
103
  rubyforge_project:
99
- rubygems_version: 2.4.6
104
+ rubygems_version: 2.4.5.1
100
105
  signing_key:
101
106
  specification_version: 4
102
107
  summary: A simple tool to backup slack channels