Skip to content

Commit 59cedea

Browse files
committed
mix(feat, fix): modify something important
- 新增 排除 Tag 筛选选项 - 新增 自动清理搜索缓存 - 修复 登录持久化问题 - 修复 可能的登录失败问题 - 修复 其他细节问题
1 parent 45ac622 commit 59cedea

File tree

19 files changed

+200
-196
lines changed

19 files changed

+200
-196
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Created by https://www.gitignore.io/api/linux,macos,python,windows
22

3-
cache/
3+
### Biu ###
4+
usr/.token
5+
usr/cache/
46

57
### Linux ###
68
*~

app/config/biu_default.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ biu.search.loadCacheFirst: true
4747
# 搜索时优先加载本地缓存
4848
# true 为开启;false 为关闭
4949

50+
biu.search.maxCacheSizeMiB: 512
51+
# 搜索缓存的最大容量(MB),达到后程序会自动清理
52+
5053

5154
## 下载相关 ##
5255
biu.download.mode: "dl-single"
@@ -112,7 +115,7 @@ biu.download.whatsUgoira: "webp"
112115
# 可选 webp, gif
113116

114117
biu.download.imageHost: ""
115-
# 下载时使用的 Pixiv 图片服务器
118+
# 后端程序下载时使用的 Pixiv 图片服务器
116119
# 留空则程序自动判断
117120
# 可参考地址
118121
# - https://i.pximg.net 官方图片服务器(需代理)

app/config/language/en.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ app.core.biu:
1515
press_to_use_old: "Press any key to continue using this old version..."
1616
network:
1717
hint_in_check: "Checking network status..."
18-
fail_pixiv_and_use_bypass: "Cannot connect to Pixiv, enable bypassSNI route"
18+
fail_pixiv_and_use_bypass: "Cannot connect to Pixiv, enable bypassSNI route (may not be available temporarily)"
1919
loginHelper:
2020
hint_token_only: "Since Pixiv disables password login, you can only use Token to login for now"
2121
hint_before_start: "Network detection is about to start, this process can reduce the probability of not being able to use PixivBiu due to network problems"
2222
fail_to_get_token_due_to_network: "Your network is not working properly for Pixiv Token login, please adjust it and try again."
2323
fail_to_get_token_anyway: "There was an error getting the Token, please try again."
24+
fail_by_cloudflare_captcha: "Encountered Cloudflare protection when logging in, it may be caused by network environment issues. You can try to get the token again, or wait a while and log in again"
2425
is_need_to_get_token: "Continue? (y / n): "
2526
ready:
2627
hint_run: "Run"
@@ -32,7 +33,7 @@ app.core.biu:
3233
hint_program_path: "Program Directory"
3334
done_init: "Initialization completed"
3435
others:
35-
hint_in_update_token: "Update token"
36+
hint_in_update_token: "Trying to update token"
3637

3738
app.common.loginHelper:
3839
network:

app/config/language/zh.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ app.core.biu:
1515
press_to_use_old: "按任意键以继续使用旧版本..."
1616
network:
1717
hint_in_check: "检测网络状态..."
18-
fail_pixiv_and_use_bypass: "无法访问 Pixiv,启用 bypassSNI 线路"
18+
fail_pixiv_and_use_bypass: "无法访问 Pixiv,启用 bypassSNI 线路 (暂时可能无法正常使用)"
1919
loginHelper:
2020
hint_token_only: "由于 Pixiv 禁止了账号密码登陆方式,暂时只能使用 Token 进行登录"
2121
hint_before_start: "即将开始进行网络检测,此过程可以减少因网络问题导致的无法正常使用 PixivBiu 的概率"
2222
fail_to_get_token_due_to_network: "您的网络无法正常进行 Pixiv Token 登陆,请调整后重试。"
2323
fail_to_get_token_anyway: "获取 Token 时出错,请重试。"
24+
fail_by_cloudflare_captcha: "登录时遇到 Cloudflare 保护,可能是网络环境问题导致。您可以尝试重新获取 Token,或稍等片刻后再次登录"
2425
is_need_to_get_token: "是否继续? (y / n): "
2526
ready:
2627
hint_run: "运行"
@@ -32,7 +33,7 @@ app.core.biu:
3233
hint_program_path: "程序目录"
3334
done_init: "初始化完成"
3435
others:
35-
hint_in_update_token: "更新 Token"
36+
hint_in_update_token: "尝试 Token 更新"
3637

3738
app.common.loginHelper:
3839
network:

app/lib/common/login_helper/main.py

+24-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import requests
22

33
from altfe.interface.root import interRoot
4-
from app.lib.common.login_helper.token import tokenGetter
4+
from app.lib.common.login_helper.token import TokenGetter
55

66

77
@interRoot.bind("loginHelper", "LIB_COMMON")
@@ -15,9 +15,9 @@ def __init__(self):
1515
self.lang = self.INS.i18n.get_bundle("app.common.loginHelper", func=True)
1616
self.requests = requests.Session()
1717
self.requests.mount("https://", CustomAdapter())
18-
self.tokenG = tokenGetter(lang=self.lang, requests=self.requests)
18+
self.token_getter = TokenGetter(lang=self.lang, requests=self.requests)
1919
self.proxy = ""
20-
self.authTokenURL = ""
20+
self.auth_token_url = ""
2121

2222
def check_network(self, URLS=None, silent=False, proxy_="auto"):
2323
"""
@@ -30,10 +30,11 @@ def check_network(self, URLS=None, silent=False, proxy_="auto"):
3030
URLS = (
3131
"https://public-api.secure.pixiv.net",
3232
"https://1.0.0.1/dns-query",
33-
"https://1.1.1.1/dns-query",
34-
"https://doh.dns.sb/dns-query",
35-
"https://cloudflare-dns.com/dns-query",
33+
# "https://1.1.1.1/dns-query",
34+
# "https://doh.dns.sb/dns-query",
35+
# "https://cloudflare-dns.com/dns-query",
3636
) if URLS is None else URLS
37+
3738
proxy = self.STATIC.util.get_system_proxy() if proxy_ == "auto" else proxy_
3839
if silent is False:
3940
self.STATIC.localMsger.msg(self.lang("network.hint_in_check"), header="Login Helper")
@@ -43,36 +44,33 @@ def check_network(self, URLS=None, silent=False, proxy_="auto"):
4344
else:
4445
if input(self.lang("network.hint_detect_proxy") % proxy) == "y":
4546
proxy = input(self.lang("network.press_need_to_type_proxy"))
46-
4747
self.proxy = proxy
48-
self.authTokenURL = URLS[0]
49-
50-
isCanConn = [self._get(url, proxy=self.proxy, silent=silent) for url in URLS]
5148

52-
if isCanConn[0] is True:
49+
self.auth_token_url = URLS[0]
50+
is_conn = [self._get(url, proxy=self.proxy, silent=silent) for url in URLS]
51+
if is_conn[0] is True:
5352
return True
5453

5554
for i in range(len(URLS)):
56-
if isCanConn[i]:
57-
finalIP = self._get_host_ip(hostname=URLS[0], url=URLS[i])
58-
if finalIP is not False:
59-
self.authTokenURL = finalIP
55+
if is_conn[i]:
56+
final_ip = self._get_host_ip(hostname=URLS[0], url=URLS[i])
57+
if final_ip is not False:
58+
self.auth_token_url = final_ip
6059
return True
61-
6260
return False
6361

6462
def login(self):
6563
"""
6664
登陆操作。
67-
:return: bool
65+
:return: tuple(access token, refresh token, user id) || tuple(false, false, false)
6866
"""
6967
kw = (
7068
{"proxies": {"http": self.proxy, "https": self.proxy}}
7169
if self.proxy != ""
7270
else {}
7371
)
7472
try:
75-
return self.tokenG.login(host=self.authTokenURL, newCode=True, kw=kw)
73+
return self.token_getter.login(host=self.auth_token_url, newCode=True, kw=kw)
7674
except Exception as e:
7775
err = str(e)
7876
if "'code': 918" in err:
@@ -81,24 +79,27 @@ def login(self):
8179
self.STATIC.localMsger.red(self.lang("login.fail_code_1508"))
8280
else:
8381
self.STATIC.localMsger.error(e, header=False)
84-
return False
82+
return False, False, False
8583

8684
def refresh(self, refresh_token):
8785
"""
8886
Token 刷新操作。
8987
:param refresh_token: 目前已有的 refresh token
90-
:return: bool
88+
:return: tuple(access token, refresh token, user id) || tuple(false, false, false)
9189
"""
9290
kw = (
9391
{"proxies": {"http": self.proxy, "https": self.proxy}}
9492
if self.proxy != ""
9593
else {}
9694
)
9795
try:
98-
return self.tokenG.refresh(refresh_token=refresh_token, host=self.authTokenURL, **kw)
96+
return self.token_getter.refresh(refresh_token=refresh_token, host=self.auth_token_url, kw=kw)
9997
except Exception as e:
100-
self.STATIC.localMsger.error(e, header=False)
101-
return False
98+
if "Invalid refresh token" in str(e):
99+
self.STATIC.localMsger.red("Common.LoginHelper.refresh: invalid refresh token")
100+
else:
101+
self.STATIC.localMsger.error(e, header=False)
102+
return False, False, False
102103

103104
def _get_host_ip(self, hostname, timeout=5, url="https://1.0.0.1/dns-query"):
104105
"""

app/lib/common/login_helper/token.py

+9-13
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
HASH_SECRET = "28c1fdd170a5204386cb1313c7077b34f83e4aaf4aa829ce78c231e05b0bae2c"
1717

1818

19-
class tokenGetter(object):
19+
class TokenGetter(object):
2020
def __init__(self, lang, requests=requests):
2121
self.lang = lang
2222
self.code = ""
@@ -47,7 +47,7 @@ def login(self, host=AUTH_TOKEN_URL_HOST, kw={}, newCode=False):
4747
:param host: token api 的主机域
4848
:param kw: requests 请求的额外参数
4949
:param newCode: 是否继承使用 code
50-
:return: str: refresh token | except: raise error
50+
:return: tuple(access token, refresh token, user id) || except: raise error
5151
"""
5252
if newCode is False and self.code != "":
5353
code = self.code
@@ -80,19 +80,17 @@ def login(self, host=AUTH_TOKEN_URL_HOST, kw={}, newCode=False):
8080
**kw,
8181
)
8282
rst = response.json()
83-
84-
if "refresh_token" in rst:
85-
return rst["refresh_token"]
86-
else:
87-
raise Exception("Request Error.\nResponse: " + str(rst))
83+
if "access_token" in rst and "refresh_token" in rst:
84+
return rst["access_token"], rst["refresh_token"], rst["user"]["id"]
85+
raise Exception("Request Error.\nResponse: " + str(rst))
8886

8987
def refresh(self, refresh_token, host=AUTH_TOKEN_URL_HOST, kw={}):
9088
"""
9189
刷新 refresh token。
9290
:param refresh_token: 目前可用的 refresh token
9391
:param host: token api 的主机域
9492
:param kw: requests 请求的额外参数
95-
:return: refresh token | except: raise error
93+
:return: tuple(access token, refresh token, user id) || except: raise error
9694
"""
9795
response = self.requests.post(
9896
"%s/auth/token" % host,
@@ -110,11 +108,9 @@ def refresh(self, refresh_token, host=AUTH_TOKEN_URL_HOST, kw={}):
110108
)
111109

112110
rst = response.json()
113-
114-
if "refresh_token" in rst:
115-
return rst["refresh_token"]
116-
else:
117-
raise Exception("Request Error.\nResponse: " + str(rst))
111+
if "access_token" in rst and "refresh_token" in rst:
112+
return rst["access_token"], rst["refresh_token"], rst["user"]["id"]
113+
raise Exception("Request Error.\nResponse: " + str(rst))
118114

119115
@staticmethod
120116
def get_header(headers={}):

0 commit comments

Comments
 (0)