Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

取模意义是什么? #5

Closed
jcfangc opened this issue Feb 19, 2024 · 6 comments
Closed

取模意义是什么? #5

jcfangc opened this issue Feb 19, 2024 · 6 comments

Comments

@jcfangc
Copy link

jcfangc commented Feb 19, 2024

    def upload_blocks(self):
        with open(self.file_path, "rb") as f:
            f.seek(0)
            block_count = (self.file_size + self.block_size - 1) // self.block_size
            with ThreadPoolExecutor(max_workers=6) as executor:
                completed_threads = []
                with tqdm(total=block_count, unit="block") as progress_bar:
                    for i in range(block_count):
                        block_data = f.read(self.block_size)  # 要求读取的字节数
                        block_size = len(block_data)  # 实际读取的字节数
                        checksum = zlib.adler32(block_data) % (10**10)
                        upload_url = f"https://internal-api-space.feishu.cn/space/api/box/stream/upload/block?upload_id={self.upload_id}&seq={i}&size={block_size}&checksum={checksum}"
                        thread = executor.submit(
                            requests.post,
                            upload_url,
                            headers=self.headers,
                            proxies=proxies,
                            data=block_data,
                        )
                        completed_threads.append(thread)
                    for thread in as_completed(completed_threads):
                        progress_bar.update(1)

其中有这样一行代码:

checksum = zlib.adler32(block_data) % (10**10)

这里adler32函数返回值是一个32位无符号整数,为什么还要取模10的10次方呢?
2^32=4294967296,是小于10^10的,这样的取模结果始终为0。

@bingsanyu
Copy link
Owner

感谢你提出的问题。你的说法是对的。

这个取模操作是在调试中忘记删除的代码。在 飞书API文档 中,checksum的示例值是一个20位的数字,当时看的时候百思不得其解,试了几种方法均不知其所以然,遂恢复原始代码,跑通后忘记删除此处的取模。

再次感谢你的反馈。

祝好

@jcfangc
Copy link
Author

jcfangc commented Feb 19, 2024

谢谢你,你好有礼貌。

我还有想要请教你的,想请你教教我:
image
image

这是我在你提供的文档链接看到的内容,我发现你的请求体和cURL好像都和文档有些偏差,以下是你的请求体:

        json = {
            "upload_id": self.upload_id,
            "num_blocks": (self.file_size + self.block_size - 1) // self.block_size,
            "vhid": self.vhid,
            "risk_detection_extra": '{"source_terminal":1,"file_operate_usage":3,"locale":"zh_cn"}',
        }

以下是你的cRUL:

        complete_url1 = (
            f"https://internal-api-space.feishu.cn/space/api/box/upload/finish/"
        )

此外,还有一个请求,这次的cURL和文档也不一致,请求体依旧不同,我就不分开引用了:

        complete_url2 = f"https://meetings.feishu.cn/minutes/api/upload/finish"
        json = {
            "auto_transcribe": True,
            "language": "mixed",
            "num_blocks": (self.file_size + self.block_size - 1) // self.block_size,
            "upload_id": self.upload_id,
            "vhid": self.vhid,
            "upload_token": self.upload_token,
            "object_token": self.object_token,
        }

我想知道怎么查找这些信息,和文档的差异是因为文档更新导致的吗?

希望得到你的回复,谢谢。

@bingsanyu
Copy link
Owner

URL的差异是因为飞书文档中使用的API是公共API,需要通过自建应用获取token。而本项目使用的是内部API。

这要从鉴权方式说起。不过你可以简单理解为:

  • 自建应用是一个管家,它拿到钥匙后有权限进行各种操作。
  • 而本项目使用的cookie则是你的替身,由他来代替你进行各种操作。

为了简化使用流程,本项目选择使用cookie而非自建应用,因此不能直接使用文档中的API,而是抓取网络请求(包括请求行、请求头、请求体、响应体等),然后在代码中复现请求流程

具体而言,一个上传过程中的网络请求有(图中使用的是F12开发者工具):
image
image
image

诚然两种API在使用上有些许不同,但殊途同归,可以相互参考之。

如有语焉不详之处,欢迎继续交流。

@jcfangc
Copy link
Author

jcfangc commented Feb 19, 2024

感谢你的解答!我大开眼界!

另外我修改了一小段代码,表达更加简洁一点,是feishu_downloader.pyFeishuDownloader.check_minutes的代码:

        # 如果只下载会议妙记,则过滤掉自己上传的妙记
        if download_type == 0:
            need_download_minutes = [
                minutes
                for minutes in need_download_minutes
                if minutes["object_type"] == 0
            ]
        # 如果只下载自己上传的妙记,则过滤掉会议妙记
        elif download_type == 1:
            need_download_minutes = [
                minutes
                for minutes in need_download_minutes
                if minutes["object_type"] == 1
            ]

修改为:

        # 过滤需要下载的妙记,基于download_type确定是会议妙记还是自己上传的妙记
        need_download_minutes = [
            minutes
            for minutes in need_download_minutes
            if minutes["object_type"] == download_type
        ]

希望采纳~

bingsanyu pushed a commit that referenced this issue Feb 19, 2024
@bingsanyu
Copy link
Owner

已修改,感谢贡献!另外minutes["object_type"]的取值只有0和1,而download_type可能为2,因此需要加一条判断。

@jcfangc jcfangc closed this as completed Feb 19, 2024
@jcfangc jcfangc mentioned this issue Feb 20, 2024
@bingsanyu
Copy link
Owner

👋 两个相关的飞书API文档 分片上传文件(上传分片)上传文件 今日已修改checksum的示例值。
image
image

修改后:
image

感谢你的贡献 🥳!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants