2016-11-17 180 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個請求的限制,否則它將不會運行。

相關問題