Jannchie commited on
Commit
2104fdb
·
1 Parent(s): a3caf4c

feat(sync): read Danbooru credentials from the environment

Browse files

DANBOORU_API_KEY / DANBOORU_LOGIN, with --api_key / --login still winning. The
weekly scheduled refresh needs credentials without putting them on a command
line, where they would be visible in the task definition, the process list and
shell history.

Also rejects a half-pair up front. Supplying one built a request with
`api_key=None` in the query string and got a 401 several seconds into the run,
which reads as a network problem rather than a configuration one.

Claude-Session: https://claude.ai/code/session_01Y7zhT65juXoYRt6EcDyL76

Files changed (1) hide show
  1. scripts/update_danbooru.py +7 -2
scripts/update_danbooru.py CHANGED
@@ -1,4 +1,5 @@
1
  import argparse
 
2
  import queue
3
  import threading
4
  import time
@@ -35,8 +36,12 @@ parser.add_argument(
35
  )
36
  args = parser.parse_args()
37
 
38
- API_KEY: str | None = args.api_key
39
- LOGIN: str | None = args.login
 
 
 
 
40
 
41
  BASE_API_URL = "https://danbooru.donmai.us/posts.json?page=a{}&limit=200{}"
42
  LATEST_ID_URL = "https://danbooru.donmai.us/posts.json?limit=1{}"
 
1
  import argparse
2
+ import os
3
  import queue
4
  import threading
5
  import time
 
36
  )
37
  args = parser.parse_args()
38
 
39
+ # 命令行优先,环境变量兜底。定时任务不该把 API key 写进命令行 —— 那会进任务定义、
40
+ # 进进程列表、进 shell 历史。两者都没有时匿名访问(限流更严,但能跑)。
41
+ API_KEY: str | None = args.api_key or os.environ.get("DANBOORU_API_KEY")
42
+ LOGIN: str | None = args.login or os.environ.get("DANBOORU_LOGIN")
43
+ if bool(API_KEY) != bool(LOGIN):
44
+ raise SystemExit("api_key 和 login 必须同时提供:只给一个会拼出 401 的请求参数")
45
 
46
  BASE_API_URL = "https://danbooru.donmai.us/posts.json?page=a{}&limit=200{}"
47
  LATEST_ID_URL = "https://danbooru.donmai.us/posts.json?limit=1{}"