Skip to content

Add Weibo -> Bilibili auto-forwarding tool with README and tests - #1

Open
jiabenguiyin wants to merge 3 commits into
mainfrom
codex/add-automatic-weibo-to-bilibili-forwarding
Open

jiabenguiyin wants to merge 3 commits into
mainfrom
codex/add-automatic-weibo-to-bilibili-forwarding

Conversation

@jiabenguiyin

Copy link
Copy Markdown
Owner

Motivation

  • Provide a small standalone tool to automatically fetch the latest post from specified Weibo users and repost it as a Bilibili dynamic.
  • Avoid external HTTP libraries by using the Python standard library for portability and minimal dependencies.
  • Persist per-UID last-post IDs to avoid duplicate reposts and support dry-run for safe testing.

Description

  • Add forward_weibo_to_bili.py implementing config loading from environment variables, Weibo fetching via m.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).
  • Use urllib for HTTP interactions with a generic helper request_json and extract CSRF token from BILI_COOKIE via get_csrf.
  • Add README.md documenting setup, required environment variables (including BILI_COOKIE and bili_jct), usage, and scheduling examples, and add requirements.txt noting no third-party deps.
  • Add unit tests in tests_forward.py covering UID normalization (normalize_one_uid / normalize_uids) and forward text building (build_forward_text).

Testing

  • Ran unit tests with python -m unittest tests_forward.py which executed NormalizeUidTests and BuildTextTests and completed successfully.
  • Basic manual dry-run tested by setting DRY_RUN=true and running python forward_weibo_to_bili.py to verify fetch/format/print paths without posting to Bilibili, and it behaved as expected.

Codex Task

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread forward_weibo_to_bili.py
post_to_bilibili(content, config.bili_cookie)
print(f"[{uid}] 发布成功。")

state[uid] = mid

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

Comment thread forward_weibo_to_bili.py
Comment on lines +193 to +194
mid = str(latest.get("mid") or latest.get("id"))
if not mid:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Comment thread forward_weibo_to_bili.py

changed = False
for uid in config.weibo_uids:
if process_uid(config, uid, state):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant