Pick the oldest eligible post still pending on at least one platform. ``platform_filters`` maps a platform name to a predicate deciding whether that platform wants a given post at all (e.g. foojay only takes the weekly Friday post). A post a platform does not want is treated as already
(
posts: list[Post],
state: State,
platforms: list[str],
today: dt.date,
floor: dt.date,
min_age_days: int,
platform_filters: dict[str, Callable[[Post], bool]] | None = None,
)
| 223 | |
| 224 | |
| 225 | def select_candidate( |
| 226 | posts: list[Post], |
| 227 | state: State, |
| 228 | platforms: list[str], |
| 229 | today: dt.date, |
| 230 | floor: dt.date, |
| 231 | min_age_days: int, |
| 232 | platform_filters: dict[str, Callable[[Post], bool]] | None = None, |
| 233 | ) -> Post | None: |
| 234 | """Pick the oldest eligible post still pending on at least one platform. |
| 235 | |
| 236 | ``platform_filters`` maps a platform name to a predicate deciding whether |
| 237 | that platform wants a given post at all (e.g. foojay only takes the weekly |
| 238 | Friday post). A post a platform does not want is treated as already |
| 239 | satisfied for that platform, so it can never block the rotation. |
| 240 | """ |
| 241 | cutoff = today - dt.timedelta(days=min_age_days) |
| 242 | |
| 243 | def pending(post: Post, platform: str) -> bool: |
| 244 | wanted = platform_filters.get(platform) if platform_filters else None |
| 245 | if wanted is not None and not wanted(post): |
| 246 | return False |
| 247 | return not state.is_syndicated(post.slug, platform) |
| 248 | |
| 249 | for post in posts: |
| 250 | if post.date <= floor: |
| 251 | continue |
| 252 | if post.date > cutoff: |
| 253 | continue |
| 254 | if not any(pending(post, p) for p in platforms): |
| 255 | continue |
| 256 | return post |
| 257 | return None |
| 258 | |
| 259 | |
| 260 | _RELATIVE_LINK_RE = re.compile(r"(\]\()(/[^)\s]+)(\))") |