Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# I'm here: ~/src/github.com/decomoji/slack-reaction-decomoji/scripts/
require 'kimurai'
require 'highline/import'
require 'retryable'
require 'open-uri'
require 'json'
class Spider < Kimurai::Base
@name = "slack_spider"
@engine = :selenium_chrome
@start_urls = ["https://slack.com"]
@config = {
user_agent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36",
before_request: { delay: 4..7 }
}
def parse(response, url:, data: {})
# Ask team name
Retryable.retryable do
@team_name = ask('Your slack team name(subdomain): ')
@url = "https://#{@team_name}.slack.com"
browser.visit(@url)
response = browser.current_response
## TODO: Need to handle error
# unless response.css("#signin_form")
# logger.error "Not found workspace. Please try again."
# raise
# end
end
# Login
Retryable.retryable do
## TODO: Need to handle 2FA
@email = ask('Login email: ')
@password = ask('Login password(hidden): ') { |q| q.echo = false }
browser.fill_in "email", with: @email
browser.fill_in "password", with: @password
browser.click_on "signin_btn"
response = browser.current_response
## TODO: Need to handle error
# if response.css("#signin_btn")
# logger.error "You entered an incorrect email address or password."
# raise
# end
end
# Go to emoji page
@emoji_url = @url + "/customize/emoji"
browser.visit(@emoji_url)
response = browser.current_response
# Upload decomojis
@img_dir = File.expand_path(File.dirname(__FILE__) + "/../decomoji/extra")
files = Dir.glob(@img_dir + "/*.png")
len = files.length
emojis = list_emojis
files.each.with_index(1) do |path, i|
basename = File.basename(path, '.*')
logger.info "(#{i}/#{len}) importing #{basename}..."
# skip if already exists
if emojis.include?(basename)
logger.info "(#{i}/#{len}) #{basename} already exists, skip"
next
end
begin
browser.find(:css,
'button[data-qa="customize_emoji_add_button"]',
wait: 0.1).click
browser.attach_file "emojiimg", path
browser.fill_in "emojiname", with: basename
browser.find(:css,
'button[data-qa="customize_emoji_add_dialog_go"]',
wait: 0.1).click
rescue => ex
## TODO: Need to handle error more smartly
logger.warn "Fails to add."
browser.find(:css,
'button[data-qa="sk_close_modal_button"]').click
next
end
# refresh
response = browser.current_response
end
end
def list_emojis
## Create App on Slack -> Bot Token Scopes : "emoji:read"
token = ask('Token:')
emoji_url = "https://slack.com/api/emoji.list?token=#{token}"
JSON.parse(URI.open(emoji_url).read)["emoji"]
end
end
Spider.crawl!
@yujiorama

This comment has been minimized.

Copy link

@yujiorama yujiorama commented May 7, 2020

It's a good script.
A runtime error occurred because there are undefined variables.
The modified parts are as follows.

diff --git a/add-slack-decomojis.rb b/add-slack-decomojis.rb
index 27c6bf4..65d2647 100644
--- a/add-slack-decomojis.rb
+++ b/add-slack-decomojis.rb
@@ -56,6 +56,7 @@ class Spider < Kimurai::Base
     @img_dir = File.expand_path(File.dirname(__FILE__) + "/../decomoji/extra")
     files = Dir.glob(@img_dir + "/*.png")
     len = files.length
+    emojis = list_emojis
     files.each.with_index(1) do |path, i|
       basename = File.basename(path, '.*')
       logger.info "(#{i}/#{len}) importing #{basename}..."
@kdmsnr

This comment has been minimized.

Copy link
Owner Author

@kdmsnr kdmsnr commented May 7, 2020

Thx! Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment