Browse Source

Download emojis

master
Dylan Baker 4 years ago
parent
commit
9a9e2ad422
2 changed files with 81 additions and 5 deletions
  1. 53
    3
      lib/slack_mattermost_emoji/slack_client.rb
  2. 28
    2
      spec/slack_client_spec.rb

+ 53
- 3
lib/slack_mattermost_emoji/slack_client.rb View File

@@ -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

+ 28
- 2
spec/slack_client_spec.rb View File

@@ -6,7 +6,8 @@ RSpec.describe SlackMattermostEmoji::SlackClient do
6 6
     client = SlackMattermostEmoji::SlackClient.new(
7 7
       username: ENV['SLACK_USERNAME'],
8 8
       password: ENV['SLACK_PASSWORD'],
9
-      domain: ENV['SLACK_DOMAIN']
9
+      domain: ENV['SLACK_DOMAIN'],
10
+      log: false,
10 11
     )
11 12
     client.authenticate
12 13
 
@@ -17,8 +18,33 @@ RSpec.describe SlackMattermostEmoji::SlackClient do
17 18
     client = SlackMattermostEmoji::SlackClient.new(
18 19
       username: ENV['SLACK_USERNAME'],
19 20
       password: 'wrongpassword',
20
-      domain: ENV['SLACK_DOMAIN']
21
+      domain: ENV['SLACK_DOMAIN'],
22
+      log: false,
21 23
     )
22 24
     expect { client.authenticate }.to raise_error('Invalid credentials')
23 25
   end
26
+
27
+  it 'should download emojis from slack' do
28
+    download_path = File.expand_path('~/Desktop/slack-emoji')
29
+    FileUtils.rm_rf(download_path) if Dir.exist?(download_path)
30
+
31
+    client = SlackMattermostEmoji::SlackClient.new(
32
+      username: ENV['SLACK_USERNAME'],
33
+      password: ENV['SLACK_PASSWORD'],
34
+      domain: ENV['SLACK_DOMAIN'],
35
+      download_path: download_path,
36
+      log: false,
37
+    )
38
+    client.authenticate
39
+
40
+    emojis = client.download_emojis
41
+    emojis.each do |name, image_url|
42
+      next if URI(image_url).scheme == 'alias'
43
+
44
+      extension = File.extname(image_url)
45
+      file_name = "#{name}#{extension}"
46
+      file_path = File.join(download_path, file_name)
47
+      expect(File.exist?(file_path)).to be true
48
+    end
49
+  end
24 50
 end

Loading…
Cancel
Save