Add Weibo -> Bilibili auto-forwarding tool with README and tests - #1
jiabenguiyin wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8c295b3307
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| post_to_bilibili(content, config.bili_cookie) | ||
| print(f"[{uid}] 发布成功。") | ||
|
|
||
| state[uid] = mid |
There was a problem hiding this comment.
Prevent dry-run from mutating forwarding state
The dry-run path still updates state[uid], and main() later persists that state, so a DRY_RUN=true execution marks posts as already forwarded even though nothing was published. In practice, if operators do a dry-run check and then run normally, the latest post is skipped and never sent to Bilibili.
Useful? React with 👍 / 👎.
| mid = str(latest.get("mid") or latest.get("id")) | ||
| if not mid: |
There was a problem hiding this comment.
Validate mid/id before casting to string
Casting latest.get("mid") or latest.get("id") to str before validation makes missing IDs become the literal string 'None', which is truthy, so the following guard never raises. If Weibo returns a malformed or non-standard card without mid/id, the script will proceed with an invalid dedup key and corrupt state tracking.
Useful? React with 👍 / 👎.
|
|
||
| changed = False | ||
| for uid in config.weibo_uids: | ||
| if process_uid(config, uid, state): |
There was a problem hiding this comment.
Persist state even when a later UID processing fails
process_uid exceptions are not handled inside the UID loop, so if one UID is posted successfully and a later UID fails (e.g., transient network/API error), main() exits before save_state runs. That loses the already-successful UID's mid, causing duplicate repost attempts on the next run.
Useful? React with 👍 / 👎.
Motivation
Description
forward_weibo_to_bili.pyimplementing config loading from environment variables, Weibo fetching viam.weibo.cn(fetch_latest_weibo), content extraction (html_to_text/build_forward_text), Bilibili posting (post_to_bilibili), and JSON state persistence (read_state/save_state).urllibfor HTTP interactions with a generic helperrequest_jsonand extract CSRF token fromBILI_COOKIEviaget_csrf.README.mddocumenting setup, required environment variables (includingBILI_COOKIEandbili_jct), usage, and scheduling examples, and addrequirements.txtnoting no third-party deps.tests_forward.pycovering UID normalization (normalize_one_uid/normalize_uids) and forward text building (build_forward_text).Testing
python -m unittest tests_forward.pywhich executedNormalizeUidTestsandBuildTextTestsand completed successfully.DRY_RUN=trueand runningpython forward_weibo_to_bili.pyto verify fetch/format/print paths without posting to Bilibili, and it behaved as expected.Codex Task