Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
BiliDriveEx
提交
43f05463
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 搜索 >>
提交
43f05463
编写于
3月 06, 2020
作者:
W
wizardforcel
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
优化登录逻辑
上级
8fd0bd3e
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
64 addition
and
41 deletion
+64
-41
BiliDriveEx/__main__.py
BiliDriveEx/__main__.py
+3
-1
BiliDriveEx/bilibili.py
BiliDriveEx/bilibili.py
+57
-39
BiliDriveEx/util.py
BiliDriveEx/util.py
+4
-1
未找到文件。
BiliDriveEx/__main__.py
浏览文件 @
43f05463
...
@@ -44,7 +44,9 @@ def fetch_meta(s):
...
@@ -44,7 +44,9 @@ def fetch_meta(s):
def
login_handle
(
args
):
def
login_handle
(
args
):
if
api
.
login
(
username
=
args
.
username
,
password
=
args
.
password
):
if
api
.
login
(
username
=
args
.
username
,
password
=
args
.
password
):
api
.
get_user_info
()
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
:
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
))
f
.
write
(
json
.
dumps
(
api
.
get_cookies
(),
ensure_ascii
=
False
,
indent
=
2
))
...
...
BiliDriveEx/bilibili.py
浏览文件 @
43f05463
...
@@ -19,26 +19,13 @@ class Bilibili:
...
@@ -19,26 +19,13 @@ class Bilibili:
default_url
=
lambda
self
,
sha1
:
f
"http://i0.hdslb.com/bfs/album/
{
sha1
}
.png"
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
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
.
_session
.
cookies
.
get_dict
(
domain
=
".bilibili.com"
)
get_cookies
=
lambda
self
:
self
.
cookies
get_uid
=
lambda
self
:
self
.
get_cookies
().
get
(
"DedeUserID"
,
""
)
get_uid
=
lambda
self
:
self
.
get_cookies
().
get
(
"DedeUserID"
,
""
)
def
__init__
(
self
):
def
__init__
(
self
):
self
.
cookie
=
{}
self
.
cookie
s
=
{}
self
.
_session
=
requests
.
Session
()
self
.
_session
=
requests
.
Session
()
self
.
_session
.
headers
.
update
({
'User-Agent'
:
"Mozilla/5.0 BiliDroid/5.51.1 (bbcallen@gmail.com)"
})
self
.
_session
.
headers
.
update
({
'User-Agent'
:
"Mozilla/5.0 BiliDroid/5.51.1 (bbcallen@gmail.com)"
})
self
.
info
=
{
'ban'
:
False
,
'coins'
:
0
,
'experience'
:
{
'current'
:
0
,
'next'
:
0
,
},
'face'
:
""
,
'level'
:
0
,
'nickname'
:
""
,
}
def
_requests
(
self
,
method
,
url
,
decode_level
=
0
,
retry
=
0
,
timeout
=
10
,
**
kwargs
):
def
_requests
(
self
,
method
,
url
,
decode_level
=
0
,
retry
=
0
,
timeout
=
10
,
**
kwargs
):
for
_
in
range
(
retry
+
1
):
for
_
in
range
(
retry
+
1
):
...
@@ -51,7 +38,10 @@ class Bilibili:
...
@@ -51,7 +38,10 @@ class Bilibili:
def
_solve_captcha
(
self
,
image
):
def
_solve_captcha
(
self
,
image
):
url
=
"https://bili.dev:2233/captcha"
url
=
"https://bili.dev:2233/captcha"
payload
=
{
'image'
:
base64
.
b64encode
(
image
).
decode
(
"utf-8"
)}
payload
=
{
'image'
:
base64
.
b64encode
(
image
).
decode
(
"utf-8"
)}
response
=
self
.
_requests
(
"post"
,
url
,
json
=
payload
).
json
()
response
=
request_retry
(
"post"
,
url
,
headers
=
Bilibili
.
default_hdrs
,
json
=
payload
).
json
()
return
response
[
'message'
]
if
response
and
response
.
get
(
"code"
)
==
0
else
None
return
response
[
'message'
]
if
response
and
response
.
get
(
"code"
)
==
0
else
None
@
staticmethod
@
staticmethod
...
@@ -68,11 +58,16 @@ class Bilibili:
...
@@ -68,11 +58,16 @@ class Bilibili:
'appkey'
:
Bilibili
.
app_key
,
'appkey'
:
Bilibili
.
app_key
,
'sign'
:
self
.
calc_sign
(
f
"appkey=
{
Bilibili
.
app_key
}
"
),
'sign'
:
self
.
calc_sign
(
f
"appkey=
{
Bilibili
.
app_key
}
"
),
}
}
res
=
self
.
_requests
(
"post"
,
url
,
data
=
payload
,
retry
=
999999
).
json
()
r
=
request_retry
(
"post"
,
url
,
data
=
payload
,
if
res
and
res
[
'code'
]
==
0
:
headers
=
Bilibili
.
default_hdrs
,
cookies
=
self
.
cookies
,
retry
=
999999
)
for
k
,
v
in
r
.
cookies
.
items
():
self
.
cookies
[
k
]
=
v
j
=
r
.
json
()
if
j
and
j
[
'code'
]
==
0
:
return
{
return
{
'key_hash'
:
res
[
'data'
][
'hash'
],
'key_hash'
:
j
[
'data'
][
'hash'
],
'pub_key'
:
rsa
.
PublicKey
.
load_pkcs1_openssl_pem
(
res
[
'data'
][
'key'
].
encode
()),
'pub_key'
:
rsa
.
PublicKey
.
load_pkcs1_openssl_pem
(
j
[
'data'
][
'key'
].
encode
()),
}
}
def
login_once
(
self
,
username
,
password
,
captcha
=
None
):
def
login_once
(
self
,
username
,
password
,
captcha
=
None
):
...
@@ -87,14 +82,19 @@ class Bilibili:
...
@@ -87,14 +82,19 @@ class Bilibili:
payload
=
f
"
{
param
}
&sign=
{
self
.
calc_sign
(
param
)
}
"
payload
=
f
"
{
param
}
&sign=
{
self
.
calc_sign
(
param
)
}
"
headers
=
Bilibili
.
default_hdrs
.
copy
()
headers
=
Bilibili
.
default_hdrs
.
copy
()
headers
.
update
({
'Content-type'
:
"application/x-www-form-urlencoded"
})
headers
.
update
({
'Content-type'
:
"application/x-www-form-urlencoded"
})
res
=
self
.
_requests
(
"POST"
,
url
,
data
=
payload
,
headers
=
headers
).
json
()
j
=
request_retry
(
"POST"
,
url
,
data
=
payload
,
return
res
headers
=
headers
,
cookies
=
self
.
cookies
).
json
()
return
j
def
get_captcha
(
self
):
def
get_captcha
(
self
):
url
=
f
"https://passport.bilibili.com/captcha"
url
=
f
"https://passport.bilibili.com/captcha"
headers
=
{
'Host'
:
"passport.bilibili.com"
}
data
=
request_retry
(
'GET'
,
url
,
res
=
self
.
_requests
(
'GET'
,
url
,
headers
=
headers
).
content
headers
=
Bilibili
.
default_hdrs
,
return
res
cookies
=
self
.
cookies
).
content
return
data
# 登录
# 登录
def
login
(
self
,
username
,
password
):
def
login
(
self
,
username
,
password
):
...
@@ -102,6 +102,7 @@ class Bilibili:
...
@@ -102,6 +102,7 @@ class Bilibili:
captcha
=
None
captcha
=
None
while
True
:
while
True
:
response
=
self
.
login_once
(
username
,
password
,
captcha
)
response
=
self
.
login_once
(
username
,
password
,
captcha
)
print
(
response
,
self
.
cookies
)
if
not
response
or
'code'
not
in
response
:
if
not
response
or
'code'
not
in
response
:
log
(
f
"当前IP登录过于频繁, 1分钟后重试"
)
log
(
f
"当前IP登录过于频繁, 1分钟后重试"
)
...
@@ -124,7 +125,7 @@ class Bilibili:
...
@@ -124,7 +125,7 @@ class Bilibili:
if
response
[
'code'
]
==
0
and
response
[
'data'
][
'status'
]
==
0
:
if
response
[
'code'
]
==
0
and
response
[
'data'
][
'status'
]
==
0
:
for
cookie
in
response
[
'data'
][
'cookie_info'
][
'cookies'
]:
for
cookie
in
response
[
'data'
][
'cookie_info'
][
'cookies'
]:
self
.
_session
.
cookies
.
set
(
cookie
[
'name'
],
cookie
[
'value'
],
domain
=
".bilibili.com"
)
self
.
cookies
[
cookie
[
'name'
]]
=
cookie
[
'value'
]
log
(
"登录成功"
)
log
(
"登录成功"
)
return
True
return
True
...
@@ -139,20 +140,37 @@ class Bilibili:
...
@@ -139,20 +140,37 @@ class Bilibili:
'Host'
:
"api.bilibili.com"
,
'Host'
:
"api.bilibili.com"
,
'Referer'
:
f
"https://space.bilibili.com/
{
self
.
get_uid
()
}
/"
,
'Referer'
:
f
"https://space.bilibili.com/
{
self
.
get_uid
()
}
/"
,
}
}
response
=
self
.
_requests
(
"get"
,
url
,
headers
=
headers
).
json
()
response
=
request_retry
(
"get"
,
url
,
if
response
and
response
.
get
(
"code"
)
==
0
:
headers
=
headers
,
self
.
info
[
'ban'
]
=
bool
(
response
[
'data'
][
'silence'
])
cookies
=
self
.
cookies
self
.
info
[
'coins'
]
=
response
[
'data'
][
'coins'
]
).
json
()
self
.
info
[
'experience'
][
'current'
]
=
response
[
'data'
][
'level_exp'
][
'current_exp'
]
self
.
info
[
'experience'
][
'next'
]
=
response
[
'data'
][
'level_exp'
][
'next_exp'
]
if
not
response
or
response
.
get
(
"code"
)
!=
0
:
self
.
info
[
'face'
]
=
response
[
'data'
][
'face'
]
self
.
info
[
'level'
]
=
response
[
'data'
][
'level'
]
self
.
info
[
'nickname'
]
=
response
[
'data'
][
'name'
]
log
(
f
"
{
self
.
info
[
'nickname'
]
}
(UID=
{
self
.
get_uid
()
}
), Lv.
{
self
.
info
[
'level'
]
}
(
{
self
.
info
[
'experience'
][
'current'
]
}
/
{
self
.
info
[
'experience'
][
'next'
]
}
), 拥有
{
self
.
info
[
'coins'
]
}
枚硬币, 账号
{
'状态正常'
if
not
self
.
info
[
'ban'
]
else
'被封禁'
}
"
)
return
True
else
:
log
(
"用户信息获取失败"
)
return
False
return
False
print
(
response
)
info
=
{
'ban'
:
False
,
'coins'
:
0
,
'experience'
:
{
'current'
:
0
,
'next'
:
0
,
},
'face'
:
""
,
'level'
:
0
,
'nickname'
:
""
,
'uid'
:
0
,
}
info
[
'ban'
]
=
bool
(
response
[
'data'
][
'silence'
])
info
[
'coins'
]
=
response
[
'data'
][
'coins'
]
info
[
'experience'
][
'current'
]
=
response
[
'data'
][
'level_exp'
][
'current_exp'
]
info
[
'experience'
][
'next'
]
=
response
[
'data'
][
'level_exp'
][
'next_exp'
]
info
[
'face'
]
=
response
[
'data'
][
'face'
]
info
[
'level'
]
=
response
[
'data'
][
'level'
]
info
[
'nickname'
]
=
response
[
'data'
][
'name'
]
info
[
'uid'
]
=
response
[
'data'
][
'mid'
]
return
info
def
exist
(
self
,
sha1
):
def
exist
(
self
,
sha1
):
...
...
BiliDriveEx/util.py
浏览文件 @
43f05463
...
@@ -79,4 +79,7 @@ def request_retry(method, url, retry=5, **kwargs):
...
@@ -79,4 +79,7 @@ def request_retry(method, url, retry=5, **kwargs):
if
i
==
retry
-
1
:
raise
ex
if
i
==
retry
-
1
:
raise
ex
get_retry
=
lambda
url
,
retry
=
5
,
**
kwargs
:
request_retry
(
'GET'
,
url
,
retry
,
**
kwargs
)
get_retry
=
lambda
url
,
retry
=
5
,
**
kwargs
:
request_retry
(
'GET'
,
url
,
retry
,
**
kwargs
)
post_retry
=
lambda
url
,
retry
=
5
,
**
kwargs
:
request_retry
(
'POST'
,
url
,
retry
,
**
kwargs
)
post_retry
=
lambda
url
,
retry
=
5
,
**
kwargs
:
request_retry
(
'POST'
,
url
,
retry
,
**
kwargs
)
\ No newline at end of file
def
log_info
(
info
):
log
(
f
"
{
info
[
'nickname'
]
}
(UID=
{
info
[
'uid'
]
}
), Lv.
{
info
[
'level'
]
}
(
{
info
[
'experience'
][
'current'
]
}
/
{
info
[
'experience'
][
'next'
]
}
), 拥有
{
info
[
'coins'
]
}
枚硬币, 账号
{
'状态正常'
if
not
info
[
'ban'
]
else
'被封禁'
}
"
)
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录