|
@@ -1,24 +1,30 @@
|
|
1
|
+require 'httparty'
|
|
2
|
+require 'json'
|
1
|
3
|
require 'selenium-webdriver'
|
2
|
4
|
|
3
|
5
|
module SlackMattermostEmoji
|
4
|
6
|
class SlackClient
|
5
|
|
- def initialize(username:, password:, domain:)
|
|
7
|
+ def initialize(username:, password:, domain:, download_path: nil, headless: true, log: true)
|
6
|
8
|
@username = username
|
7
|
9
|
@password = password
|
8
|
10
|
@domain = domain
|
|
11
|
+ @download_path = File.expand_path(download_path || './slack-emojis')
|
|
12
|
+ @log = log
|
9
|
13
|
@authenticated = false
|
10
|
14
|
|
|
15
|
+ Dir.mkdir(@download_path) unless Dir.exist?(@download_path)
|
|
16
|
+
|
11
|
17
|
options = Selenium::WebDriver::Chrome::Options.new
|
12
|
|
- options.headless!
|
|
18
|
+ options.headless! unless headless == false
|
13
|
19
|
@driver = Selenium::WebDriver.for :chrome, options: options
|
14
|
20
|
|
15
|
|
-
|
16
|
21
|
if [@username, @password, @domain].any? { |el| el.nil? || el.empty? }
|
17
|
22
|
raise 'Slack username, password, and domain are all required'
|
18
|
23
|
end
|
19
|
24
|
end
|
20
|
25
|
|
21
|
26
|
def authenticate
|
|
27
|
+ print "Logging into Slack at #{@domain} as #{@username}... " if @log
|
22
|
28
|
@driver.navigate.to "https://#{@domain}"
|
23
|
29
|
@driver.find_element(name: 'email').send_keys(@username)
|
24
|
30
|
@driver.find_element(name: 'password').send_keys(@password)
|
|
@@ -31,9 +37,53 @@ module SlackMattermostEmoji
|
31
|
37
|
nil
|
32
|
38
|
end
|
33
|
39
|
|
|
40
|
+ puts 'success' if @log
|
|
41
|
+
|
34
|
42
|
@authenticated = true
|
35
|
43
|
end
|
36
|
44
|
|
|
45
|
+ def download_emojis
|
|
46
|
+ print "Finding emojis... " if @log
|
|
47
|
+
|
|
48
|
+ token = @driver.execute_script(<<~JS)
|
|
49
|
+ const config = JSON.parse(window.localStorage.localConfig_v2);
|
|
50
|
+ const teams = Object.values(config.teams);
|
|
51
|
+ const team = teams.find((team) => team.url.match('#{@domain}'));
|
|
52
|
+ if (!team) {
|
|
53
|
+ return null;
|
|
54
|
+ } else {
|
|
55
|
+ return team.token;
|
|
56
|
+ }
|
|
57
|
+ JS
|
|
58
|
+
|
|
59
|
+ @driver.navigate.to("https://#{@domain}/api/emoji.list?token=#{token}")
|
|
60
|
+ body = @driver.find_element(tag_name: 'body')
|
|
61
|
+ data = JSON.parse(body.text)
|
|
62
|
+ emoji_count = data['emoji'].size
|
|
63
|
+
|
|
64
|
+ puts "found #{emoji_count} emojis" if @log
|
|
65
|
+
|
|
66
|
+ data['emoji'].each_with_index do |emoji, index|
|
|
67
|
+ name, image_url = emoji
|
|
68
|
+
|
|
69
|
+ digits = emoji_count.to_s.size
|
|
70
|
+ puts "#{(index + 1).to_s.rjust(digits, ' ')}/#{emoji_count} :#{name}:" if @log
|
|
71
|
+
|
|
72
|
+ next if URI(image_url).scheme == 'alias'
|
|
73
|
+
|
|
74
|
+ extension = File.extname(image_url)
|
|
75
|
+ file_name = "#{name}#{extension}"
|
|
76
|
+ file_path = File.join(@download_path, file_name)
|
|
77
|
+
|
|
78
|
+ File.open(file_path, "wb") do |f|
|
|
79
|
+ image = HTTParty.get(image_url)
|
|
80
|
+ f.write(image.body)
|
|
81
|
+ end
|
|
82
|
+ end
|
|
83
|
+
|
|
84
|
+ data['emoji']
|
|
85
|
+ end
|
|
86
|
+
|
37
|
87
|
def authenticated?
|
38
|
88
|
@authenticated
|
39
|
89
|
end
|