Fix #14: Refactor print_browse/send_to_telegram into single pipeline #22

Merged
shoko merged 3 commits from fix/issue-14-refactor-browse into master 2026-03-25 20:11:07 +01:00
Owner

Summary

Replace duplicate inline formatting in print_browse() and send_to_telegram() with a unified format+render pipeline.

New Functions

  • format_match_event(e) — canonical dict for match events (stateless, pure)
  • format_non_match_event(e) — canonical dict for non-match events
  • render_match_lines(event_dict, i, mode) — renders lines, mode=text|html
  • render_non_match_lines(event_dict, i, mode) — same for non-match events
  • send_chunked(...) — extracted Telegram chunking logic

Pipeline

browse_events()
  -> format_match_event(e) / format_non_match_event(e)
  -> render_match_lines() / render_non_match_lines()
  -> print_browse() / send_to_telegram()

Design principles:

  • format_*() are pure (one event in, dict out)
  • render_*() is mode-aware (text vs HTML escaping)
  • List slicing happens BEFORE formatting (caller owns slice decisions)
  • browse_events() stays simple — no pre-computation

Bug Fix

send_chunked() chunking condition was broken: the original ". " in line check never matched event lines since the period is followed by </b>, not a space. Fixed to just check </a> presence.

Test Coverage

19 new tests (38 total, all passing).

Lines Changed

  • print_browse(): ~80 lines -> ~25 lines
  • send_to_telegram(): ~120 lines -> ~35 lines
  • Net: ~200 lines of duplication removed

Fixes: #14

## Summary Replace duplicate inline formatting in `print_browse()` and `send_to_telegram()` with a unified format+render pipeline. ## New Functions - `format_match_event(e)` — canonical dict for match events (stateless, pure) - `format_non_match_event(e)` — canonical dict for non-match events - `render_match_lines(event_dict, i, mode)` — renders lines, mode=text|html - `render_non_match_lines(event_dict, i, mode)` — same for non-match events - `send_chunked(...)` — extracted Telegram chunking logic ## Pipeline ``` browse_events() -> format_match_event(e) / format_non_match_event(e) -> render_match_lines() / render_non_match_lines() -> print_browse() / send_to_telegram() ``` **Design principles:** - `format_*()` are pure (one event in, dict out) - `render_*()` is mode-aware (text vs HTML escaping) - List slicing happens BEFORE formatting (caller owns slice decisions) - `browse_events()` stays simple — no pre-computation ## Bug Fix `send_chunked()` chunking condition was broken: the original `". " in line` check never matched event lines since the period is followed by `</b>`, not a space. Fixed to just check `</a>` presence. ## Test Coverage 19 new tests (38 total, all passing). ## Lines Changed - `print_browse()`: ~80 lines -> ~25 lines - `send_to_telegram()`: ~120 lines -> ~35 lines - Net: ~200 lines of duplication removed Fixes: #14
shoko added 1 commit 2026-03-25 18:47:11 +01:00
Replace duplicate inline formatting with unified format+render pipeline.

New functions:
- format_match_event(e) — canonical dict for match events
- format_non_match_event(e) — canonical dict for non-match events
- render_match_lines(event_dict, i, mode) — text/HTML renderer
- render_non_match_lines(event_dict, i, mode) — text/HTML renderer
- send_chunked(...) — extracted Telegram chunking logic

Also fixed send_chunked() chunking bug: the original '. ' in line
check never matched event lines (period is followed by '</b>' not space).

Tests: 38 total, all passing.

Fixes: #14
shoko force-pushed fix/issue-14-refactor-browse from d018e87b35 to 3a9f8fb365 2026-03-25 18:51:04 +01:00 Compare
shoko added 1 commit 2026-03-25 19:53:21 +01:00
- fetch_page: replace subprocess.run(curl) with urllib (stdlib, cleaner)
- fetch_all_pages: add matches_max/non_matches_max params for early-exit.
  When both are set, stop fetching once quotas are satisfied.
- browse_events: add sort_by param (None='fast' early-exit, 'volume'=full fetch+sort).
  Early-exit only used when sort_by=None (no client-side sort needed).
- Remove subprocess import (no longer needed after migration)
shoko added 1 commit 2026-03-25 20:08:44 +01:00
New test classes:
- TestIsMatchMarket: 5 tests for is_match_market() classification
- TestGetMlMarket: 5 tests for get_ml_market() and get_ml_volume()
- TestFilterEvents: 5 tests for filter_events() and sort_events()
- TestFetchAllPages: 4 tests for fetch_all_pages() early-exit logic
- TestBrowseEvents: 5 tests for browse_events() sort_by parameter

Total: 24 new tests (62 total, all passing)
shoko merged commit ae50fd14f0 into master 2026-03-25 20:11:07 +01:00
Sign in to join this conversation.