2016-11-17 178 views
-2

我正在清理我们的Slack帐户,并希望在删除它们之前保存这些文件。附件是我从github获得的脚本。有人可以给我一个片段,我可以添加到脚本,所以我可以告诉Python将文件保存在指定的文件夹(root_folder)。请提供您的善意帮助。将Slack文件保存在文件夹中

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 
+1

是包含文件细节的API吗? – Juggernaut

+0

是你自己发布的任何代码,还是直接来自github?你是否要求我们在你自己没有完成任何工作时为你写代码?如果是这样,这是该网站的主题。 –

+0

是的,它包含我们Slack账户的​​详细信息。我已经补充了它。它与github不完全相同。我只想告诉Python将文件保存在指定的文件夹中。当我运行这个脚本时,它会返回一个将被保存的文件列表,我只需要添加一个片段来保存文件夹中的文件。 –

回答

1

这里是如何做到这一点的基本方法。

  1. 获取所有文件和它们的ID列表与files.list
  2. 遍历所有文件
  3. 每个文件的列表:使用files.sharedPublicUrl以获取文件的公开网址。使用脚本下载并保存。最后删除它files.delete

请注意,您的机器人/访问令牌将只能访问它/相应用户被邀请到的私人频道的文件。

还请注意,您的脚本需要遵守每秒1个请求的限制,否则它将不会运行。

相关问题