該 GIF 圖來自于官網,文末有給出鏈接。
描述
依托于百度網盤巨大的的云存儲空間,絕大數人會習慣性的將一些資料什么的存儲到上面,但是有的私密鏈接需要提取碼,但是讓每個想下載私密資源的人記住每一個提取碼顯然是不現實的。這個時候,云盤萬能鑰匙 誕生了,我們通過安裝相應的瀏覽器插件就可以自動獲獲取相應鏈接的提取碼。我在 Github 上看了一下,有 Web JS 版的, python 版的貌似還沒有找到,所以我參照了JS 版本和官網的請求接口寫了兩種方式的獲取腳本。
實現
下述兩種方式的具體實現就不做代碼解釋了,思路都是一樣,通過請求接口,拿到數據,然后返回即可。
V1
"""
Author:hippieZhou
Date:20190608
Description: Get BaiDuYun shared link's Code
"""
import argparse
import re
import requests
import json
import time
VERSION = "VERSION 1.0.0"
def checkUrl(url: str) -> str:
m1 = re.match(
"https?:\/\/pan\.baidu\.com\/s\/1([a-zA-Z0-9_\-]{5,22})", url)
m2 = re.match(
"https?:\/\/pan\.baidu\.com\/share\/init\?surl=([a-zA-Z0-9_\-]{5,22})", url)
if not m1 and not m2:
print("參數不合法")
return False
else:
return True
def getKey(url: str) -> bool:
if checkUrl(url):
try:
req = requests.get(f"https://node.pnote.net/public/pan?url={url}")
code = req.status_code
if code == 200:
data = dict(json.loads(req.text))
status = data.get("status", False)
if status:
return data.get("access_code", "未能查詢到該鏈接的提取碼,可能原因是:該鏈接不需要提取碼或已過期")
else:
return data.get("messages", "為能查詢到提取碼")
elif code == 404:
return "不存在該鏈接的記錄"
except Exception as e:
return f"請求服務器失敗,錯誤代碼:{code}"
def get_parser():
parser = argparse.ArgumentParser()
parser.description = "百度網盤提取碼一鍵獲取器"
parser.add_argument('urls', metavar="urls", type=str, nargs="*",
help='設置要獲取提取碼的鏈接(多個鏈接請用空格分隔)')
parser.add_argument('-v', '--version', action='store_true',
help='版本號')
return parser
def command_line_runner():
parser = get_parser()
args = vars(parser.parse_args())
if args['version']:
print(VERSION)
return
s_time = time.time()
if len(args['urls']) > 1:
for item in args["urls"][1:]:
print(f"{item}:\r\n\t{getKey(item)}")
e_time = time.time()
print(f"\n\n操作完畢,總耗時:{e_time-s_time} 秒")
def main():
command_line_runner()
if __name__ == "__main__":
main()
運行效果如下圖所示:
v2
"""
Author:hippieZhou
Date:20190608
Description: Get BaiDuYun shared link's Code
"""
import argparse
import time
import re
import requests
from datetime import datetime
import json
accessKey = "4fxNbkKKJX2pAm3b8AEu2zT5d2MbqGbD"
clientVersion = "web-client"
def getPid(url: str) -> str:
matches = re.match(
"https?:\/\/pan\.baidu\.com\/s\/1([a-zA-Z0-9_\-]{5,22})", url)
return matches[1] if matches else None
def getUuid(pid: str) -> str:
return f"BDY-{pid}"
def getKey(url: str) -> str:
pid = getPid(url)
uuid = getUuid(pid)
headers = {
"type": "GET",
"data": '',
"dataType": "json"
}
url = f"http://ypsuperkey.meek.com.cn/api/items/{uuid}?access_key={accessKey}&client_version={clientVersion}&{datetime.utcnow()}"
try:
req = requests.get(url, headers=headers)
code = req.status_code
if code == 200:
data = json.loads(req.text)
accessCode = data.get("access_code", None)
return "沒找到提取密碼,o(╥﹏╥)o" if (accessCode == "undefined" or accessCode == None or accessCode == "") else accessCode
elif code == 400:
return " 服務器不理解請求的語法"
elif code == 404:
return "不存在該鏈接的記錄"
else:
return f"請求服務器失敗,錯誤代碼:{code}"
except Exception as e:
return e
def get_parser():
parser = argparse.ArgumentParser()
parser.description = "百度網盤提取碼一鍵獲取器"
parser.add_argument('urls', metavar="urls", type=str, nargs="*",
help='設置要獲取提取碼的鏈接(多個鏈接請用空格分隔)')
parser.add_argument('-v', '--version', action='store_true',
help='版本號')
return parser
def command_line_runner():
parser = get_parser()
args = vars(parser.parse_args())
if args['version']:
print(VERSION)
return
s_time = time.time()
if len(args['urls']) > 1:
for item in args["urls"][1:]:
print(f"{item}:\r\n\t{getKey(item)}")
e_time = time.time()
print(f"\n\n操作完畢,總耗時:{e_time-s_time} 秒")
def main():
command_line_runner()
if __name__ == "__main__":
main()
運行效果如下圖所示:
總結
v1 版本和 v2 版本是通過請求不同的接口方式來實現的, v2 接口的數據要相對更準確一些。具體可查閱具體的代碼實現。
如果你覺得上述代碼不錯的話,歡迎訪問對應的倉庫地址: baidupankey 進行 star 、fork 和 follow。
相關參考
原文:https://www.cnblogs.com/hippieZhou/p/10990237.html