【问题标题】:Save Slack files in the folder将 Slack 文件保存在文件夹中
【发布时间】:2016-11-17 18:32:11
【问题描述】:

我正在清理我们的 Slack 帐户并希望在删除文件之前保存这些文件。附件是我从github获得的脚本。有人可以给我一个可以添加到脚本中的 sn-p,这样我就可以告诉 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

【问题讨论】:

  • API 是否包含来自文件的详细信息?
  • 您发布的任何代码是您自己的,还是直接来自 github?当您自己没有完成任何工作时,您是否要求我们为您编写代码?如果是这样,那是本网站的题外话。
  • 是的,它包括来自我们 Slack 帐户的详细信息。我已经对其进行了补充。它与 github 并不完全相同。我只想告诉 Python 将文件保存在指定的文件夹中。当我运行脚本时,它会返回一个将要保存的文件列表,我只需要添加一个 sn -p 即可将文件保存在文件夹中。
  • Stackoverflow 不是编码服务。如果您有特定问题,我相信人们会很乐意帮助您。但不要指望别人为你编写代码。

标签: python python-2.7 slack-api


【解决方案1】:

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

  1. files.list获取所有文件的列表及其ID
  2. 遍历所有文件的列表
  3. 对于每个文件:使用files.sharedPublicUrl 获取文件的公共 url。下载并与您的脚本一起保存。最后用files.delete删除它

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

进一步注意,您的脚本需要遵守每秒 1 个请求的限制,否则它将无法运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    相关资源
    最近更新 更多