Skip to content

Instantly share code, notes, and snippets.

@jrgns
Created July 28, 2016 11:08
Show Gist options
  • Save jrgns/28a326cd14b5bd0685a0fe2c63139c6d to your computer and use it in GitHub Desktop.
Save jrgns/28a326cd14b5bd0685a0fe2c63139c6d to your computer and use it in GitHub Desktop.
Simple import of Slack export into Elasticsearch
require 'date'
require 'json'
require 'logger'
require 'elasticsearch'
class SlackToElasticsearch
attr_reader :folder
def initialize(folder)
@folder = folder
end
def process_subfolder(subfolder)
Dir.foreach("#{folder}/#{subfolder}") do |file|
next if (file == '.' || file == '..')
logger.info "Importing File #{folder}/#{subfolder}/#{file}"
index = 'zadevs-' + file.gsub('-', '.').sub('.json', '')
items = JSON.parse(File.read("#{folder}/#{subfolder}/#{file}"))
items.each do |item|
item['date_time'] = DateTime.strptime(item['ts'], '%s')
client.index index: index, type: subfolder, body: item
end
end
end
def process
logger.info "Importing from #{folder}"
Dir.foreach(folder) do |subfolder|
next if (subfolder == '.' || subfolder == '..')
next unless File.directory?("#{folder}/#{subfolder}")
logger.info "Importing Channel #{subfolder}"
process_subfolder(subfolder)
end
end
def client
@client ||= Elasticsearch::Client.new host: 'http://localhost:9200'
end
def logger
@logger ||= Logger.new($stdout)
end
end
if __FILE__ == $0 then
SlackToElasticsearch.new('./slack_export').process
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment