Created
December 7, 2015 01:24
-
-
Save jasonyork/97a9012917f7ec3b96ce to your computer and use it in GitHub Desktop.
Removing Old Slack Files (Ruby 1.9.3)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
require 'json' | |
require 'uri' | |
@token = '' | |
def list_files | |
ts_to = (Time.now - 60 * 24 * 60 * 60).to_i # 60 days ago | |
uri = URI.parse("https://slack.com/api/files.list?token=#{@token}&ts_to=#{ts_to}&count=1000") | |
response = Net::HTTP.start(uri.host, uri.port, | |
:use_ssl => uri.scheme == 'https') do |http| | |
request = Net::HTTP::Get.new uri.request_uri | |
response = http.request request # Net::HTTPResponse object | |
end | |
JSON.parse(response.body)['files'] | |
end | |
def delete_files(file_ids) | |
file_ids.each do |file_id| | |
uri = URI.parse("https://slack.com/api/files.delete?token=#{@token}&file=#{file_id}") | |
begin | |
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http| | |
request = Net::HTTP::Get.new uri.request_uri | |
response = http.request request # Net::HTTPResponse object | |
parsed_response = JSON.parse(response.body) | |
p "#{file_id}: #{parsed_response['ok']}, error: #{parsed_response['error']}" | |
end | |
rescue Exception => e | |
p e.message | |
end | |
end | |
end | |
files = list_files | |
p "Deleting #{files.length} files..." | |
file_ids = files.map { |f| f['id'] } | |
delete_files(file_ids) | |
p 'Done!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment