Skip to content

Instantly share code, notes, and snippets.

@keichi
Last active April 11, 2019 06:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keichi/9e9013938ab712cbc5c006ac0bafa230 to your computer and use it in GitHub Desktop.
Save keichi/9e9013938ab712cbc5c006ac0bafa230 to your computer and use it in GitHub Desktop.
slackclient==1.3.0
ratelimit==2.2.1
import os
from datetime import datetime, timedelta
from urllib.request import urlopen, Request
from ratelimit import limits
from slackclient import SlackClient
# TODO エラーチェック
@limits(calls=50, period=60)
def download_file(sc, file_info):
headers = {"Authorization": "Bearer {}".format(sc.token)}
req = Request(file_info["url_private"], headers=headers)
with urlopen(req) as resp:
content = resp.read()
# file_typeのnameは重複する可能性があるので,idでprefixする
fname = "{}-{}".format(file_info["id"], file_info["name"])
with open(fname, mode="wb") as f:
f.write(content)
# TODO エラーチェック
@limits(calls=50, period=60)
def remove_file(sc, file_info):
sc.api_call("files.delete", file=file_info["id"])
def main():
# レガシAPIトークンを環境変数に設定しておく
slack_token = os.environ["SLACK_API_TOKEN"]
sc = SlackClient(slack_token)
page = 1
pages = 1
files = []
# files.listの結果はページネーションされている
while page <= pages:
print("Downloading page {}".format(page))
# 1年前以前にアップロードされたファイルを削除する
ts = int((datetime.now() - timedelta(days=365)).timestamp())
res = sc.api_call("files.list", page=page, ts_to=ts)
pages = res["paging"]["pages"]
files.extend(res["files"])
page += 1
total_files = 0
total_bytes = 0
for file_info in files:
# Slackにアップロードされているファイルのみ処理する
# (Google Driveへのリンク等は無視)
if file_info["mode"] != "hosted":
continue
print("Downloading and removing file {}".format(file_info["name"]))
# TODO ダウンロードが失敗したら削除しない
download_file(sc, file_info)
remove_file(sc, file_info)
total_files += 1
total_bytes += file_info["size"]
print("Removed {} files, total {} bytes".format(total_files, total_bytes))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment