Instantly share code, notes, and snippets.
Last activeJuly 1, 2025 09:24
Save Ljzd-PRO/50f37f350b78fd1835e7066d7487cd61 to your computer and use it in GitHub Desktop.
Extract All Files in PikPak Directory Online
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
""" | |
## 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 展示解压进度 | |
- 更直观地统计解压任务结果 | |
""" | |
importasyncio | |
importjson | |
frompathlibimportPath | |
importhttpx | |
fromloguruimportlogger | |
# 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() | |
defload_files(): | |
returnjson.load(FILES_JSON_LOCATION.open(encoding="utf-8")) | |
asyncdefunzip(file:dict): | |
file_id:str=file["id"] | |
file_name:str=file["name"] | |
try: | |
res=awaitclient.post( | |
"https://api-drive.mypikpak.com/decompress/v1/decompress", | |
json={ | |
"gcid":GCID, | |
"password":"", | |
"file_id":file_id, | |
"files": [], | |
"default_parent":True | |
} | |
) | |
excepthttpx.TransportError: | |
logger.error(f"file:{file_name}, status: request failed") | |
returnFalse | |
ifres.status_code==200: | |
res_json=res.json() | |
ifres_json.get("status")=="OK": | |
task_id:str=res_json.get("task_id") | |
error_times=MAX_ERROR_TIMES | |
whileerror_times: | |
awaitasyncio.sleep(PROGRESS_INTERVAL) | |
try: | |
res=awaitclient.get( | |
"https://api-drive.mypikpak.com/decompress/v1/progress", | |
params={ | |
"task_id":task_id | |
} | |
) | |
excepthttpx.TransportError: | |
error_times-=1 | |
else: | |
ifres.status_code==200and (progress:=res.json().get("progress"))isnotNone: | |
logger.info(f"file:{file_name}, progress:{progress}%") | |
ifprogress==100: | |
returnTrue | |
else: | |
error_times-=1 | |
logger.error(f"file:{file_name}, status: failed, response:{res.text}") | |
returnFalse | |
asyncdefexecutor(): | |
whilenottask_queue.empty(): | |
file=awaittask_queue.get() | |
awaitunzip(file) | |
awaitasyncio.sleep(EXTRACT_WAIT_TIME) | |
defmain(): | |
files=load_files()["files"] | |
logger.info(f"Total files:{len(files)}") | |
forfileinfiles: | |
iffile["file_extension"]inFILE_EXTENSIONS: | |
task_queue.put_nowait(file) | |
asyncio.get_event_loop().run_until_complete( | |
asyncio.gather( | |
*[executor()for_inrange(EXECUTORS)] | |
) | |
) | |
logger.success("All tasks done (Some may failed)") | |
main() |
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment