slack_export 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: de3d078aca9ed5e72062c99b8ac19a96b558b5b3
4
+ data.tar.gz: 7dda1f57a02bf122ace9cf49c1fdd685113fdeed
5
+ SHA512:
6
+ metadata.gz: 4f0d10460a96ad98bfa50caabe50b967c80700cb7f24dfa45acc57d048f766063ffe41c84d7496b1f24ecef5df31f283b768f8cfc0517679a409761c0de5a281
7
+ data.tar.gz: 02e8510665d65140155c7c311544a6f6537a058c70aff4b2d18f97cc0f316c2966a552faeb5222361e34b76e988d9db17d1f05e06ade00119e45abf027a57455
data/bin/slack_export ADDED
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "optparse"
4
+ require "slack_export"
5
+
6
+ options = {}
7
+
8
+ optparse = OptionParser.new do |opts|
9
+ opts.banner = "Usage: slack_export --key=your-slack-api-key --channel=private-channel-name --path=folder-to-create-export"
10
+
11
+ opts.on("-h", "--help", "Display this screen") do
12
+ puts opts
13
+ exit
14
+ end
15
+
16
+ opts.on("-k", "--key KEY", "Slack API Key (required)") do |key|
17
+ options[:key] = key
18
+ end
19
+
20
+ opts.on("-c", "--channel CHANNEL", "Private Channel name (required)") do |channel|
21
+ options[:channel] = channel
22
+ end
23
+
24
+ opts.on("-p", "--path PATH", "Local folder path to write export file (required)") do |path|
25
+ options[:path] = path
26
+ end
27
+ end
28
+ optparse.parse!
29
+
30
+ [:key, :channel, :path].each do |arg|
31
+ unless options[arg]
32
+ puts optparse.help
33
+ exit -1
34
+ end
35
+ end
36
+
37
+ exporter = SlackExport::Exporter.new(options[:key], options[:channel], options[:path])
38
+ exporter.logger = -> (message) { puts message }
39
+
40
+ begin
41
+ exporter.export
42
+ rescue => e
43
+ puts e.message
44
+ exit -1
45
+ end
@@ -0,0 +1,95 @@
1
+ require "slack_export/slack_client"
2
+ require "json"
3
+ require "zip"
4
+
5
+ module SlackExport
6
+ class Exporter
7
+
8
+ attr_reader :client, :channel, :base_path
9
+ attr_accessor :logger
10
+
11
+ def initialize(api_key, channel, base_path, logger=nil)
12
+ @client = SlackClient.new(api_key, channel)
13
+ @channel = channel
14
+ @base_path = base_path
15
+ self.logger = logger
16
+ end
17
+
18
+ def export(new_channel_name=nil)
19
+ raise StandardError("Directory #{base_path} does not exist") unless Dir.exist?(base_path)
20
+ log "Exporting #{channel} to folder #{base_path}"
21
+
22
+ # CHANNELS
23
+ channels = client.get_channels.select {|c| c["name"] == channel}
24
+ log "Exporting #{channels.count} channels"
25
+ File.open(channels_path, "w") {|f| f.write(channels.to_json)}
26
+
27
+ # MESSAGES
28
+ messages = client.get_messages
29
+ log "Exporting #{messages.count} messages"
30
+ Dir.mkdir(messages_base_path) unless Dir.exist?(messages_base_path)
31
+ File.open(messages_path, "w") do |file|
32
+ file.write(messages.to_json)
33
+ end
34
+
35
+ # USERS
36
+ users = client.get_users
37
+ log "Exporting #{users.count} users"
38
+ users = users.select do |u|
39
+ messages.any? {|m| m["user"] == u["id"]}
40
+ end
41
+ File.open(users_path, "w") {|f| f.write(users.to_json)}
42
+
43
+ # BUNDLE TO ZIP
44
+ log "Bundling export file to #{export_path}"
45
+ Zip::File.open(export_path, Zip::File::CREATE) do |zip|
46
+ zip.add(users_filename, users_path)
47
+ zip.add(channels_filename, channels_path)
48
+ zip.add(messages_sub_path, messages_path)
49
+ end
50
+ end
51
+
52
+ def export_path
53
+ File.join(base_path, "#{channel}.zip")
54
+ end
55
+
56
+ private
57
+
58
+ def users_filename
59
+ "users.json"
60
+ end
61
+
62
+ def users_path
63
+ File.join(base_path, users_filename)
64
+ end
65
+
66
+ def channels_filename
67
+ "channels.json"
68
+ end
69
+
70
+ def channels_path
71
+ File.join(base_path, channels_filename)
72
+ end
73
+
74
+ def messages_filename
75
+ "messages.json"
76
+ end
77
+
78
+ def messages_base_path
79
+ File.join(base_path, channel)
80
+ end
81
+
82
+ def messages_sub_path
83
+ File.join(channel, messages_filename)
84
+ end
85
+
86
+ def messages_path
87
+ File.join(base_path, messages_sub_path)
88
+ end
89
+
90
+ def log(message)
91
+ logger.call message if logger
92
+ end
93
+
94
+ end
95
+ end
@@ -0,0 +1,59 @@
1
+ require "rest-client"
2
+
3
+ module SlackExport
4
+ class SlackClient
5
+
6
+ BASE_URL = "https://slack.com/api/"
7
+ LIST_USERS = "users.list"
8
+ LIST_CHANNELS = "groups.list"
9
+ LIST_MESSAGES = "groups.history"
10
+ CHANNEL_INFO = "groups.info"
11
+
12
+ attr_accessor :token, :channel
13
+
14
+ def initialize(api_key, channel_name)
15
+ self.token = api_key
16
+ self.channel = get_channel_id(channel_name)
17
+ end
18
+
19
+ def get_users
20
+ response = post_form(LIST_USERS)
21
+ response["members"]
22
+ end
23
+
24
+ def get_channels
25
+ response = post_form(LIST_CHANNELS)
26
+ response["groups"]
27
+ end
28
+
29
+ def get_messages()
30
+ responses = []
31
+ latest = latest.to_i #pages backward, so keep track of most recent message pulled
32
+ oldest = oldest.to_i
33
+ loop do
34
+ responses << post_form(LIST_MESSAGES, { channel: channel, latest: latest, oldest: oldest, count: 1000})
35
+ latest = responses.last["messages"].last["ts"]
36
+ break unless responses.last["has_more"]
37
+ end
38
+
39
+ responses.map {|r| r["messages"]}.flatten
40
+ end
41
+
42
+ private
43
+
44
+ def get_channel_id(channel_name)
45
+ get_channels.select {|g| g["name"] == channel_name}.first["id"]
46
+ end
47
+
48
+ def post_form(action, form_values={})
49
+ response = RestClient::Request.execute(
50
+ url: "#{BASE_URL}#{action}",
51
+ method: :post,
52
+ payload: form_values.merge(token: self.token)
53
+ )
54
+
55
+ JSON.parse(response)
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module SlackExport
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ module SlackExport
2
+ end
3
+
4
+ require "slack_export/version"
5
+ require "slack_export/slack_client"
6
+ require "slack_export/exporter"
@@ -0,0 +1,9 @@
1
+ require "slack_export/slack_client"
2
+
3
+ module SlackExport
4
+ describe SlackClient do
5
+
6
+ subject { SlackClient.new() }
7
+
8
+ end
9
+ end
@@ -0,0 +1,96 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ RSpec.configure do |config|
20
+ # rspec-expectations config goes here. You can use an alternate
21
+ # assertion/expectation library such as wrong or the stdlib/minitest
22
+ # assertions if you prefer.
23
+ config.expect_with :rspec do |expectations|
24
+ # This option will default to `true` in RSpec 4. It makes the `description`
25
+ # and `failure_message` of custom matchers include text for helper methods
26
+ # defined using `chain`, e.g.:
27
+ # be_bigger_than(2).and_smaller_than(4).description
28
+ # # => "be bigger than 2 and smaller than 4"
29
+ # ...rather than:
30
+ # # => "be bigger than 2"
31
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32
+ end
33
+
34
+ # rspec-mocks config goes here. You can use an alternate test double
35
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
36
+ config.mock_with :rspec do |mocks|
37
+ # Prevents you from mocking or stubbing a method that does not exist on
38
+ # a real object. This is generally recommended, and will default to
39
+ # `true` in RSpec 4.
40
+ mocks.verify_partial_doubles = true
41
+ end
42
+
43
+ # The settings below are suggested to provide a good initial experience
44
+ # with RSpec, but feel free to customize to your heart's content.
45
+ =begin
46
+ # These two settings work together to allow you to limit a spec run
47
+ # to individual examples or groups you care about by tagging them with
48
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
49
+ # get run.
50
+ config.filter_run :focus
51
+ config.run_all_when_everything_filtered = true
52
+
53
+ # Allows RSpec to persist some state between runs in order to support
54
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
55
+ # you configure your source control system to ignore this file.
56
+ config.example_status_persistence_file_path = "spec/examples.txt"
57
+
58
+ # Limits the available syntax to the non-monkey patched syntax that is
59
+ # recommended. For more details, see:
60
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
61
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
62
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
63
+ config.disable_monkey_patching!
64
+
65
+ # This setting enables warnings. It's recommended, but in some cases may
66
+ # be too noisy due to issues in dependencies.
67
+ config.warnings = true
68
+
69
+ # Many RSpec users commonly either run the entire suite or an individual
70
+ # file, and it's useful to allow more verbose output when running an
71
+ # individual spec file.
72
+ if config.files_to_run.one?
73
+ # Use the documentation formatter for detailed output,
74
+ # unless a formatter has already been configured
75
+ # (e.g. via a command-line flag).
76
+ config.default_formatter = 'doc'
77
+ end
78
+
79
+ # Print the 10 slowest examples and example groups at the
80
+ # end of the spec run, to help surface which specs are running
81
+ # particularly slow.
82
+ config.profile_examples = 10
83
+
84
+ # Run specs in random order to surface order dependencies. If you find an
85
+ # order dependency and want to debug it, you can fix the order by providing
86
+ # the seed, which is printed after each run.
87
+ # --seed 1234
88
+ config.order = :random
89
+
90
+ # Seed global randomization in this process using the `--seed` CLI option.
91
+ # Setting this allows you to use `--seed` to deterministically reproduce
92
+ # test failures related to randomization by passing the same `--seed` value
93
+ # as the one that triggered the failure.
94
+ Kernel.srand config.seed
95
+ =end
96
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slack_export
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Tucker
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubyzip
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: vcr
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.9'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.9'
97
+ description: Export private Slack channels in the standard Slack export format
98
+ email:
99
+ - mtucker@godaddy.com
100
+ executables:
101
+ - slack_export
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - bin/slack_export
106
+ - lib/slack_export.rb
107
+ - lib/slack_export/exporter.rb
108
+ - lib/slack_export/slack_client.rb
109
+ - lib/slack_export/version.rb
110
+ - spec/slack_export/slack_client_spec.rb
111
+ - spec/spec_helper.rb
112
+ homepage: https://github.com/mtuckergd/slack_export
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.4.8
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: Export private Slack channels
136
+ test_files:
137
+ - spec/slack_export/slack_client_spec.rb
138
+ - spec/spec_helper.rb