Skip to content

Instantly share code, notes, and snippets.

@ZeroDragon
Last active September 28, 2016 14:32
Show Gist options
  • Save ZeroDragon/3a8f3efbe949fa8569911e1a73e3100f to your computer and use it in GitHub Desktop.
Save ZeroDragon/3a8f3efbe949fa8569911e1a73e3100f to your computer and use it in GitHub Desktop.
Slack File Deleter
###*
* Confugure HERE
###
token = "Slack-Token" #Slack token
domain = "myTeamName" #Your team name
maxCool = 10 #Max retries to get files list
days = 7 #Keep this days of files
###*
* Stop editing
###
request = require 'request'
async = require 'async'
cool = 1
dateTo = new Date()
dateTo.setDate(dateTo.getDate()-days)
dateTo = ~~(dateTo.getTime()/1000)
qq = async.queue (doIt, cb)->
request.post "https://slack.com/api/files.list?t=#{new Date().getTime()}", {
form:{
token:token,
ts_to:dateTo
ts_from:0
}
,json:true
}, (err,data,body)->
ids = body.files.map (e)-> e.id
q = async.queue (task,callback)->
request.post "https://#{domain}.slack.com/api/files.delete?t=#{new Date().getTime()}", {
form:{
token : token
file : task
},
json:true
},(err2,data2,body2)->
throw err2 if err2
console.log "#{task} File status: #{body2.ok}"
callback()
,10
q.drain = ->
if q.running() + q.length() is 0
cb()
cool = 1 if ids.length > 0
q.push ids
,1
qq.drain = ->
if qq.running()+qq.length() is 0
if cool >= maxCool
console.log 'Waited enough cooling time, bye'
process.exit 0
else
console.log 'Cooling down 5 seconds'
setTimeout ->
qq.push true
,5000
cool++
qq.push true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment