Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
BiliDriveEx
提交
9ffb3624
B
BiliDriveEx
项目概览
OpenDocCN
/
BiliDriveEx
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
B
BiliDriveEx
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
9ffb3624
编写于
3月 06, 2020
作者:
W
wizardforcel
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
优化 cookie 加载流程
上级
43f05463
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
25 addition
and
28 deletion
+25
-28
BiliDriveEx/__main__.py
BiliDriveEx/__main__.py
+5
-10
BiliDriveEx/bilibili.py
BiliDriveEx/bilibili.py
+20
-18
未找到文件。
BiliDriveEx/__main__.py
浏览文件 @
9ffb3624
...
...
@@ -47,8 +47,7 @@ def login_handle(args):
info
=
api
.
get_user_info
()
if
info
:
log_info
(
info
)
else
:
log
(
"用户信息获取失败"
)
with
open
(
os
.
path
.
join
(
bundle_dir
,
"cookies.json"
),
"w"
,
encoding
=
"utf-8"
)
as
f
:
f
.
write
(
json
.
dumps
(
api
.
get_cookies
(),
ensure_ascii
=
False
,
indent
=
2
))
def
upload_handle
(
args
):
def
core
(
index
,
block
):
...
...
@@ -69,7 +68,7 @@ def upload_handle(args):
for
_
in
range
(
10
):
if
terminate_flag
.
is_set
():
return
response
=
api
.
image_upload
(
full_block
,
cookies
)
response
=
api
.
image_upload
(
full_block
)
if
response
:
if
response
[
'code'
]
==
0
:
url
=
response
[
'data'
][
'image_url'
]
...
...
@@ -109,12 +108,8 @@ def upload_handle(args):
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
)
}
"
)
return
url
try
:
with
open
(
os
.
path
.
join
(
bundle_dir
,
"cookies.json"
),
"r"
,
encoding
=
"utf-8"
)
as
f
:
cookies
=
json
.
loads
(
f
.
read
())
except
:
log
(
"Cookies加载失败, 请先登录"
)
return
None
# TODO: 判断 Cookie 是否有效
log
(
f
"线程数:
{
args
.
thread
}
"
)
done_flag
=
threading
.
Semaphore
(
0
)
terminate_flag
=
threading
.
Event
()
...
...
@@ -145,7 +140,7 @@ def upload_handle(args):
meta
=
json
.
dumps
(
meta_dict
,
ensure_ascii
=
False
).
encode
(
"utf-8"
)
full_meta
=
encoder
.
encode
(
meta
)
for
_
in
range
(
10
):
response
=
api
.
image_upload
(
full_meta
,
cookies
)
response
=
api
.
image_upload
(
full_meta
)
if
response
and
response
[
'code'
]
==
0
:
url
=
response
[
'data'
][
'image_url'
]
log
(
"元数据上传完毕"
)
...
...
BiliDriveEx/bilibili.py
浏览文件 @
9ffb3624
...
...
@@ -20,20 +20,10 @@ class Bilibili:
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
get_cookies
=
lambda
self
:
self
.
cookies
get_uid
=
lambda
self
:
self
.
get_cookies
().
get
(
"DedeUserID"
,
""
)
def
__init__
(
self
):
self
.
cookies
=
{}
self
.
_session
=
requests
.
Session
()
self
.
_session
.
headers
.
update
({
'User-Agent'
:
"Mozilla/5.0 BiliDroid/5.51.1 (bbcallen@gmail.com)"
})
def
_requests
(
self
,
method
,
url
,
decode_level
=
0
,
retry
=
0
,
timeout
=
10
,
**
kwargs
):
for
_
in
range
(
retry
+
1
):
try
:
response
=
getattr
(
self
.
_session
,
method
.
lower
())(
url
,
timeout
=
timeout
,
**
kwargs
)
return
response
.
json
()
if
decode_level
==
2
else
response
.
content
if
decode_level
==
1
else
response
except
:
pass
self
.
load_cookies
()
def
_solve_captcha
(
self
,
image
):
url
=
"https://bili.dev:2233/captcha"
...
...
@@ -102,7 +92,7 @@ class Bilibili:
captcha
=
None
while
True
:
response
=
self
.
login_once
(
username
,
password
,
captcha
)
print
(
response
,
self
.
cookies
)
#
print(response, self.cookies)
if
not
response
or
'code'
not
in
response
:
log
(
f
"当前IP登录过于频繁, 1分钟后重试"
)
...
...
@@ -127,6 +117,7 @@ class Bilibili:
for
cookie
in
response
[
'data'
][
'cookie_info'
][
'cookies'
]:
self
.
cookies
[
cookie
[
'name'
]]
=
cookie
[
'value'
]
log
(
"登录成功"
)
self
.
save_cookies
()
return
True
log
(
f
"登录失败
{
response
}
"
)
...
...
@@ -137,8 +128,7 @@ class Bilibili:
def
get_user_info
(
self
):
url
=
f
"https://api.bilibili.com/x/space/myinfo?jsonp=jsonp"
headers
=
{
'Host'
:
"api.bilibili.com"
,
'Referer'
:
f
"https://space.bilibili.com/
{
self
.
get_uid
()
}
/"
,
'Referer'
:
f
"https://space.bilibili.com"
,
}
response
=
request_retry
(
"get"
,
url
,
headers
=
headers
,
...
...
@@ -147,7 +137,6 @@ class Bilibili:
if
not
response
or
response
.
get
(
"code"
)
!=
0
:
return
False
print
(
response
)
info
=
{
'ban'
:
False
,
...
...
@@ -171,7 +160,16 @@ class Bilibili:
info
[
'uid'
]
=
response
[
'data'
][
'mid'
]
return
info
def
save_cookies
(
self
):
with
open
(
os
.
path
.
join
(
bundle_dir
,
"cookies.json"
),
"w"
,
encoding
=
"utf-8"
)
as
f
:
f
.
write
(
json
.
dumps
(
self
.
cookies
,
ensure_ascii
=
False
,
indent
=
2
))
def
load_cookies
(
self
):
try
:
with
open
(
os
.
path
.
join
(
bundle_dir
,
"cookies.json"
),
"r"
,
encoding
=
"utf-8"
)
as
f
:
self
.
cookies
=
json
.
loads
(
f
.
read
())
except
:
pass
def
exist
(
self
,
sha1
):
try
:
...
...
@@ -186,7 +184,7 @@ class Bilibili:
return
None
def
image_upload
(
self
,
data
,
cookies
):
def
image_upload
(
self
,
data
):
sha1
=
calc_sha1
(
data
)
url
=
self
.
exist
(
sha1
)
if
url
:
return
{
'code'
:
0
,
'data'
:
{
'image_url'
:
url
}}
...
...
@@ -205,7 +203,11 @@ class Bilibili:
'category'
:
"daily"
,
}
try
:
response
=
requests
.
post
(
url
,
data
=
data
,
headers
=
headers
,
cookies
=
cookies
,
files
=
files
,
timeout
=
300
).
json
()
response
=
requests
.
post
(
url
,
data
=
data
,
headers
=
headers
,
cookies
=
self
.
cookies
,
files
=
files
,
timeout
=
300
).
json
()
except
:
response
=
None
return
response
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录