Skip to content

fix: scraper regex matches new "Last Free Electricity sessions:" heading - #22

Open
mindbox77 wants to merge 1 commit into
dopeytree:masterfrom
mindbox77:fix/scraper-regex-free-electricity
Open

fix: scraper regex matches new "Last Free Electricity sessions:" heading#22
mindbox77 wants to merge 1 commit into
dopeytree:masterfrom
mindbox77:fix/scraper-regex-free-electricity

Conversation

@mindbox77

@mindbox77 mindbox77 commented Apr 19, 2026

Copy link
Copy Markdown

Summary

  • Website scraper has been returning 0 sessions because Octopus changed the page heading from Last Session: to ⚡️Last Free Electricity sessions:⚡️. Broaden the regex in extract_sessions to accept optional Free Electricity and plural Sessions for both Next and Last variants.
  • Also clarify a misleading log message: ERROR: Failed to fetch HTML content. was firing on every cycle where no sessions were extracted — even when the fetch itself succeeded (96KB of HTML was returned). Split into a real fetch-failure error and an informational log when the page simply has no sessions listed yet.

Context

Reported from a user's production log showing successful HTML fetches (length: 96318) but 0 sessions extracted every cycle, followed by the misleading Failed to fetch HTML content error. Verified locally against the current live HTML: the fixed regex correctly extracts 9-10pm, Friday 24th October and 12-3pm, Saturday 25th October.

Test plan

  • Manual run of extract_sessions against current live HTML from https://octopus.energy/free-electricity/ returns both scheduled sessions
  • Existing tests still pass
  • Verify in a Docker build against live site

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling to distinguish between failed HTML fetches and pages with no available sessions.
    • Enhanced session detection to support additional Octopus website page layout variants, improving extraction reliability.

@coderabbitai

coderabbitai Bot commented Apr 19, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@mindbox77 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 57 minutes and 58 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 57 minutes and 58 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 00c88270-f0ae-4f0b-9d03-96f1dafad863

📥 Commits

Reviewing files that changed from the base of the PR and between 21aa34e and 929c955.

📒 Files selected for processing (2)
  • octofree/main.py
  • octofree/scraper_website.py
📝 Walkthrough

Walkthrough

The pull request refines error handling and regex pattern matching. The main.py file now distinguishes between failed HTML fetches and successful fetches with no extracted sessions using separate log levels. The scraper_website.py file expands regex patterns to recognize additional heading variants with optional "Free Electricity" text and flexible pluralization.

Changes

Cohort / File(s) Summary
HTML Fetch Error Handling
octofree/main.py
Control flow modified to distinguish between failed HTML fetch (html_content is None, logs error) and successful fetch with no sessions extracted (logs info message).
Session Extraction Regex Patterns
octofree/scraper_website.py
Updated "next" and "last" heading regexes to recognize additional variants with optional "Free Electricity" text and flexible pluralization using Sessions? pattern.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 Wiggles nose thoughtfully
When HTML fails, we now log with care,
Error or silence—each gets its share!
Regex patterns bloom with variants new,
Free Electricity in singular and plural too, 🔌
Our scraper leaps onward, precise and true! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly addresses the main change: updating scraper regex to match new Octopus page headings, specifically the 'Last Free Electricity sessions:' variant.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Octopus updated the /free-electricity/ page heading from "Last Session:"
to "⚡️Last Free Electricity sessions:⚡️", causing the website scraper's
regex to miss all sessions and return 0. Broaden the regex to accept the
optional "Free Electricity" phrase and plural "Sessions" for both "Next"
and "Last" headings.

Also clarify the misleading "Failed to fetch HTML content" log message —
it previously fired whenever no sessions were extracted, even on a
successful fetch. Now split into a real fetch-failure error and an
informational log when the page simply has no sessions listed.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@mindbox77
mindbox77 force-pushed the fix/scraper-regex-free-electricity branch from 21aa34e to 929c955 Compare April 19, 2026 19:20

@coderabbitai coderabbitai 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.

🧹 Nitpick comments (1)
octofree/main.py (1)

725-728: Log separation is a clear improvement; minor edge case on empty-string content.

Splitting fetch failure (error) from "page fetched but empty extraction" (info) directly addresses the misleading logs described in the PR.

Small gap: fetch_page_content currently only returns None or response.text, but if upstream ever returned an empty string (e.g., a future change or unusual 200 response), line 348's if html_content: would treat it as falsy while line 725's elif html_content is None would be False, so the empty-body case would land in the "no sessions extracted" branch rather than being surfaced as a fetch anomaly. If you want to be defensive, consider:

Optional hardening
-        elif html_content is None:
-            logging.error("Failed to fetch HTML content.")
-        else:
+        elif not html_content:
+            logging.error("Failed to fetch HTML content.")
+        else:
             logging.info("ℹ️  No sessions extracted from HTML (page may have no sessions listed yet)")

Ruff's RUF001 warning about the character on line 728 is a stylistic false positive here (intentional info glyph) and can be ignored or silenced with a # noqa: RUF001.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@octofree/main.py` around lines 725 - 728, The fetch result handling should
distinguish None (fetch failure) from an empty string (empty response); update
the block that checks html_content returned by fetch_page_content so it treats
html_content is None as the error case, html_content == "" (or len(html_content)
== 0) as a separate error/warning like logging.error("Fetched empty HTML
response") before falling through to the "no sessions extracted" info branch,
and leave the existing logging.info message (the one containing the "ℹ️ No
sessions extracted..." text) but append a "# noqa: RUF001" comment to that
logging line to silence Ruff's false positive about the info glyph.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@octofree/main.py`:
- Around line 725-728: The fetch result handling should distinguish None (fetch
failure) from an empty string (empty response); update the block that checks
html_content returned by fetch_page_content so it treats html_content is None as
the error case, html_content == "" (or len(html_content) == 0) as a separate
error/warning like logging.error("Fetched empty HTML response") before falling
through to the "no sessions extracted" info branch, and leave the existing
logging.info message (the one containing the "ℹ️ No sessions extracted..." text)
but append a "# noqa: RUF001" comment to that logging line to silence Ruff's
false positive about the info glyph.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c782cdc9-1476-4768-96eb-f5a4b0e2e7da

📥 Commits

Reviewing files that changed from the base of the PR and between b24a719 and 21aa34e.

📒 Files selected for processing (2)
  • octofree/main.py
  • octofree/scraper_website.py

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant