Dylan Baker преди 4 години
родител
ревизия
9a9e2ad422
променени са 2 файла, в които са добавени 81 реда и са изтрити 5 реда
  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 Целия файл

1
+require 'httparty'
2
+require 'json'
1
 require 'selenium-webdriver'
3
 require 'selenium-webdriver'
2
 
4
 
3
 module SlackMattermostEmoji
5
 module SlackMattermostEmoji
4
   class SlackClient
6
   class SlackClient
5
-    def initialize(username:, password:, domain:)
7
+    def initialize(username:, password:, domain:, download_path: nil, headless: true, log: true)
6
       @username = username
8
       @username = username
7
       @password = password
9
       @password = password
8
       @domain = domain
10
       @domain = domain
11
+      @download_path = File.expand_path(download_path || './slack-emojis')
12
+      @log = log
9
       @authenticated = false
13
       @authenticated = false
10
 
14
 
15
+      Dir.mkdir(@download_path) unless Dir.exist?(@download_path)
16
+
11
       options = Selenium::WebDriver::Chrome::Options.new
17
       options = Selenium::WebDriver::Chrome::Options.new
12
-      options.headless!
18
+      options.headless! unless headless == false
13
       @driver = Selenium::WebDriver.for :chrome, options: options
19
       @driver = Selenium::WebDriver.for :chrome, options: options
14
 
20
 
15
-
16
       if [@username, @password, @domain].any? { |el| el.nil? || el.empty? }
21
       if [@username, @password, @domain].any? { |el| el.nil? || el.empty? }
17
         raise 'Slack username, password, and domain are all required'
22
         raise 'Slack username, password, and domain are all required'
18
       end
23
       end
19
     end
24
     end
20
 
25
 
21
     def authenticate
26
     def authenticate
27
+      print "Logging into Slack at #{@domain} as #{@username}... " if @log
22
       @driver.navigate.to "https://#{@domain}"
28
       @driver.navigate.to "https://#{@domain}"
23
       @driver.find_element(name: 'email').send_keys(@username)
29
       @driver.find_element(name: 'email').send_keys(@username)
24
       @driver.find_element(name: 'password').send_keys(@password)
30
       @driver.find_element(name: 'password').send_keys(@password)
31
         nil
37
         nil
32
       end
38
       end
33
 
39
 
40
+      puts 'success' if @log
41
+
34
       @authenticated = true
42
       @authenticated = true
35
     end
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
     def authenticated?
87
     def authenticated?
38
       @authenticated
88
       @authenticated
39
     end
89
     end

+ 28
- 2
spec/slack_client_spec.rb Целия файл

6
     client = SlackMattermostEmoji::SlackClient.new(
6
     client = SlackMattermostEmoji::SlackClient.new(
7
       username: ENV['SLACK_USERNAME'],
7
       username: ENV['SLACK_USERNAME'],
8
       password: ENV['SLACK_PASSWORD'],
8
       password: ENV['SLACK_PASSWORD'],
9
-      domain: ENV['SLACK_DOMAIN']
9
+      domain: ENV['SLACK_DOMAIN'],
10
+      log: false,
10
     )
11
     )
11
     client.authenticate
12
     client.authenticate
12
 
13
 
17
     client = SlackMattermostEmoji::SlackClient.new(
18
     client = SlackMattermostEmoji::SlackClient.new(
18
       username: ENV['SLACK_USERNAME'],
19
       username: ENV['SLACK_USERNAME'],
19
       password: 'wrongpassword',
20
       password: 'wrongpassword',
20
-      domain: ENV['SLACK_DOMAIN']
21
+      domain: ENV['SLACK_DOMAIN'],
22
+      log: false,
21
     )
23
     )
22
     expect { client.authenticate }.to raise_error('Invalid credentials')
24
     expect { client.authenticate }.to raise_error('Invalid credentials')
23
   end
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
 end
50
 end

Loading…
Отказ
Запис