提交 f830b4ef 编写于 作者: W wizardforcel

建立接口规范

上级 afcaad60
......@@ -25,22 +25,13 @@ encoder = Encoder()
api = Bilibili()
def fetch_meta(s):
if re.match(r"^bdex://[a-fA-F0-9]{40}$", s):
full_meta = image_download(api.default_url(re.findall(r"[a-fA-F0-9]{40}", s)[0]))
elif re.match(r"^bdrive://[a-fA-F0-9]{40}$", s):
full_meta = image_download(
api.default_url(re.findall(r"[a-fA-F0-9]{40}", s)[0]).replace('png', 'x-ms-bmp')
)
elif s.startswith("http://") or s.startswith("https://"):
full_meta = image_download(s)
else:
return
try:
meta_dict = json.loads(encoder.decode(full_meta).decode("utf-8"))
return meta_dict
except:
return
url = api.meta2real(s)
if not url: return None
print(url)
full_meta = image_download(url)
if not full_meta: return None
meta_dict = json.loads(encoder.decode(full_meta).decode("utf-8"))
return meta_dict
def login_handle(args):
r = api.login(args.username, args.password)
......@@ -119,7 +110,7 @@ def upload_handle(args):
if first_4mb_sha1 in history:
url = history[first_4mb_sha1]['url']
log(f"文件已于{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(history[first_4mb_sha1]['time']))}上传, 共有{len(history[first_4mb_sha1]['block'])}个分块")
log(f"META URL -> {api.meta_string(url)}")
log(f"META URL -> {api.real2meta(url)}")
return url
if not api.get_user_info():
......@@ -161,7 +152,7 @@ def upload_handle(args):
url = response['data']['image_url']
log("元数据上传完毕")
log(f"{meta_dict['filename']} ({size_string(meta_dict['size'])}) 上传完毕, 用时{time.time() - start_time:.1f}秒, 平均速度{size_string(meta_dict['size'] / (time.time() - start_time))}/s")
log(f"META URL -> {api.meta_string(url)}")
log(f"META URL -> {api.real2meta(url)}")
write_history(first_4mb_sha1, meta_dict, url)
return url
log(f"元数据第{_ + 1}次上传失败")
......@@ -268,7 +259,7 @@ def info_handle(args):
if meta_dict:
print_meta(meta_dict)
else:
print("元数据解析失败")
log("元数据解析失败")
def history_handle(args):
history = read_history()
......@@ -276,7 +267,7 @@ def history_handle(args):
for index, meta_dict in enumerate(history.values()):
prefix = f"[{index + 1}]"
print(f"{prefix} {meta_dict['filename']} ({size_string(meta_dict['size'])}), 共有{len(meta_dict['block'])}个分块, 上传于{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(meta_dict['time']))}")
print(f"{' ' * len(prefix)} META URL -> {api.meta_string(meta_dict['url'])}")
print(f"{' ' * len(prefix)} META URL -> {api.real2meta(meta_dict['url'])}")
else:
print(f"暂无历史记录")
......
......@@ -17,13 +17,28 @@ class Bilibili:
default_hdrs = {'User-Agent': "Mozilla/5.0 BiliDroid/5.51.1 (bbcallen@gmail.com)"}
default_url = lambda self, sha1: f"http://i0.hdslb.com/bfs/album/{sha1}.png"
meta_string = lambda self, url: ("bdex://" + re.findall(r"[a-fA-F0-9]{40}", url)[0]) if re.match(r"^http(s?)://i0.hdslb.com/bfs/album/[a-fA-F0-9]{40}.png$", url) else url
extract_hash = lambda self, s: re.findall(r"[a-fA-F0-9]{40}", s)[0]
get_cookies = lambda self: self.cookies
def __init__(self):
self.cookies = {}
self.load_cookies()
def meta2real(self, url):
if re.match(r"^bdex://[a-fA-F0-9]{40}$", url):
return self.default_url(self.extract_hash(url))
elif re.match(r"^bdrive://[a-fA-F0-9]{40}$", url):
return self.default_url(self.extract_hash(url)) \
.replace('.png', '.x-ms-bmp')
else:
return None
def real2meta(self, url):
if re.match(r"^https?://i0.hdslb.com/bfs/album/[a-fA-F0-9]{40}.png$", url):
return "bdex://" + self.extract_hash(url)
else:
return None
def set_cookies(self, cookie_str):
self.cookies = {}
......
......@@ -27,7 +27,6 @@ def calc_sha1(data, hex=True):
def image_download(url):
headers = {
'Referer': "http://t.bilibili.com/",
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36",
}
content = []
......@@ -38,9 +37,9 @@ def image_download(url):
return
content.append(chunk)
last_chunk_time = time.time()
return b"".join(content)
except:
return
return b"".join(content)
def read_history():
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册