我正在清理我们的Slack帐户,并希望在删除之前保存文件。附件是我从github获得的脚本。有人可以给我一个我可以添加到脚本的片段,这样我就可以告诉Python将文件保存在指定的文件夹(root_folder)中。请提供您的帮助。
from slacker import *
import sys
import time
import os
from datetime import timedelta, datetime
root_folder = 'Z:\Slack_Files'
def main(token, weeks=4):
slack = Slacker(token)
# Get list of all files available for the user of the token
total = slack.files.list(count=1).body['paging']['total']
num_pages = int(total/1000.00 + 1)
print("{} files to be processed, across {} pages".format(total, num_pages))
# Get Data about files
files_to_save = []
ids = [] # For checking that the API doesn't return duplicate files
count = 1
for page in range(num_pages):
print ("Pulling page number {}".format(page + 1))
files = slack.files.list(count=1000, page=page+1).body['files']
for file in files:
print("Checking file number {}".format(count))
# Checking for duplicates
if file['id'] not in ids:
ids.append(file['id'])
if datetime.fromtimestamp(file['timestamp']) < datetime.now() - timedelta(weeks=weeks):
files_to_save.append(file)
print("File No. {} will be saved".format(count))
else:
print("File No. {} will not be saved".format(count))
count+=1
print("All files saved\nProceeding to save files")
print("{} files will be saved!".format(len(files_to_save)))
count = 1
for file in files_to_save:
print("Saving file {} of {} - {}".format(count, len(files_to_save), file["name"]))
print(file["name"])
count+=1
return count-1
答案 0 :(得分:1)
以下是如何操作的基本方法。
请注意,您的机器人/访问令牌只能访问来自相应用户的私人频道中的文件。
进一步注意,您的脚本需要遵守每秒1个请求的限制,否则它将无法运行。