Skip to content

Instantly share code, notes, and snippets.

@Ljzd-PRO
Last active July 1, 2025 09:24
Show Gist options
  • Save Ljzd-PRO/50f37f350b78fd1835e7066d7487cd61 to your computer and use it in GitHub Desktop.
Save Ljzd-PRO/50f37f350b78fd1835e7066d7487cd61 to your computer and use it in GitHub Desktop.
Extract All Files in PikPak Directory Online
"""
## License
BSD 3-Clause License
Copyright (c) 2024, Ljzd-PRO, https://github.com/Ljzd-PRO
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
## 介绍
PikPak 批量在线解压
## 使用说明
1. 安装依赖 ``httpx``, ``loguru``
2. 修改参数 Params 部分,创建 ``files.json`` 并填入数据
3. 启动脚本
## 改进建议
- 用 pydantic 模型化 PikPak 列出目录文件 API 的返回数据
- 省去手动抓包目录文件列表,自动获取目标目录下的文件列表
- 用 tqdm 展示解压进度
- 更直观地统计解压任务结果
"""
import asyncio
import json
from pathlib import Path
import httpx
from loguru import logger
# Params
FILES_JSON_LOCATION = Path("files.json")
"""
需要包含 PikPak 列出目录文件 API 的返回数据,可进入浏览器开发者模式抓包获取
列出目录文件 API: ``/drive/v1/files``
"""
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0"
"""浏览器 ``User-Agent``"""
AUTHORIZATION = "..."
"""Headers 中的 ``Authorization``,可进入浏览器开发者模式抓包获取"""
GCID = "02AC0C3F92743D637CFEAC834D06EE806731B5B7"
"""解压 API ``/decompress/v1/decompress`` 负载中的 ``gcid``,可进入浏览器开发者模式抓包获取"""
EXECUTORS = 5
"""最多同时进行的解压任务数"""
TIMEOUT = 10
"""网络请求超时时间"""
MAX_ERROR_TIMES = 10
"""查询进度时允许请求出错最多的次数"""
PROGRESS_INTERVAL = 2
"""查询进度时间间隔"""
EXTRACT_WAIT_TIME = 2
"""解压文件的网络请求之间的等待时间间隔"""
FILE_EXTENSIONS = {".zip", ".rar", ".7z"}
"""目标文件类型"""
# Global Variables
client = httpx.AsyncClient(
headers={
"User-Agent": USER_AGENT,
"Authorization": AUTHORIZATION
},
timeout=TIMEOUT
)
task_queue: asyncio.Queue[dict] = asyncio.Queue()
def load_files():
return json.load(FILES_JSON_LOCATION.open(encoding="utf-8"))
async def unzip(file: dict):
file_id: str = file["id"]
file_name: str = file["name"]
try:
res = await client.post(
"https://api-drive.mypikpak.com/decompress/v1/decompress",
json={
"gcid": GCID,
"password": "",
"file_id": file_id,
"files": [],
"default_parent": True
}
)
except httpx.TransportError:
logger.error(f"file: {file_name}, status: request failed")
return False
if res.status_code == 200:
res_json = res.json()
if res_json.get("status") == "OK":
task_id: str = res_json.get("task_id")
error_times = MAX_ERROR_TIMES
while error_times:
await asyncio.sleep(PROGRESS_INTERVAL)
try:
res = await client.get(
"https://api-drive.mypikpak.com/decompress/v1/progress",
params={
"task_id": task_id
}
)
except httpx.TransportError:
error_times -= 1
else:
if res.status_code == 200 and (progress := res.json().get("progress")) is not None:
logger.info(f"file: {file_name}, progress: {progress}%")
if progress == 100:
return True
else:
error_times -= 1
logger.error(f"file: {file_name}, status: failed, response: {res.text}")
return False
async def executor():
while not task_queue.empty():
file = await task_queue.get()
await unzip(file)
await asyncio.sleep(EXTRACT_WAIT_TIME)
def main():
files = load_files()["files"]
logger.info(f"Total files: {len(files)}")
for file in files:
if file["file_extension"] in FILE_EXTENSIONS:
task_queue.put_nowait(file)
asyncio.get_event_loop().run_until_complete(
asyncio.gather(
*[executor() for _ in range(EXECUTORS)]
)
)
logger.success("All tasks done (Some may failed)")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment