Skip to content

Instantly share code, notes, and snippets.

@akitaonrails
Last active January 26, 2016 15:53
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save akitaonrails/38cbbc2c37a7c646fe27 to your computer and use it in GitHub Desktop.
Slack Deleter
source 'https://rubygems.org'
gem 'faraday'
gem 'typhoeus'
gem 'oj'
gem 'parallel'
GEM
remote: https://rubygems.org/
specs:
ethon (0.7.3)
ffi (>= 1.3.0)
faraday (0.9.1)
multipart-post (>= 1.2, < 3)
ffi (1.9.8)
multipart-post (2.0.0)
oj (2.12.7)
parallel (1.4.1)
typhoeus (0.7.1)
ethon (>= 0.7.1)
PLATFORMS
ruby
DEPENDENCIES
faraday
oj
parallel
typhoeus
require 'rubygems'
require 'bundler/setup'
Bundler.require(:default)
require 'typhoeus/adapters/faraday'
# documentations:
# https://api.slack.com/methods/files.list
# https://api.slack.com/methods/files.delete
api_token = ENV['SLACK_API_TOKEN'] or abort "no api token"
per_page = 500
uri = "/api/files.list?token=%s&ts_from=0&ts_to=now&types=all&count=%s&page=%s"
conn = Faraday.new(:url => 'https://slack.com/') do |faraday|
faraday.request :url_encoded # form-encode POST params
faraday.response :logger # log requests to STDOUT
faraday.adapter :typhoeus
end
response = conn.get( uri % [api_token, per_page, 1] )
json = Oj.load(response.body)
total_pages = json["paging"]["pages"].to_i
responses = []
conn.in_parallel do
(1..total_pages).each do |page|
responses << conn.get( uri % [api_token, per_page, page] )
end
end
files_ids = []
errors = []
Parallel.each(responses, in_threads: 10) do |response|
begin
json = Oj.load(response.body)
Parallel.each(json["files"], in_threads: 10) do |file|
files_ids << file["id"]
end
rescue => e
errors << e
end
end
uri_delete = "https://slack.com/api/files.delete?token=%s&file=%s"
conn.in_parallel do
files_ids.each do |file_id|
responses << conn.get( uri_delete % [api_token, file_id] )
end
end
puts responses.map do |response|
Oj.load(response.body)["ok"]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment