The Update Cycle
APScheduler runs inside the Flask process. Every cycle reads the live view counts, compares them with the titles that are already on the videos, and writes only what actually changed.
The whole design is shaped by one fact: a read costs 1 quota unit and can carry fifty videos, while a write costs 50 units and can carry exactly one.
- Read — 1 unit
- Write — 50 units
APScheduler — in the Flask process
One process, one scheduler. Gunicorn runs a single worker on purpose: a second worker would start a second scheduler and double every API call.
Fixed interval, and it ignores the shared budget completely — it keeps running even after the shared lane has stopped for the day.
Recomputed after every run from the units still left and the minutes left until the quota resets, then clamped into that range.
Both lanes run
the same steps
Up to 50 ids
in one call
videos.list — read
1 unit
Every video id in the batch goes into a single request for
snippet,statistics. Fifty ids or one id, the call costs the same 1 unit,
so reading is close to free.
View counts
come back
Compare
The new title is built from the live view count and held against the title already on the video.
No batching
on this side
One request
per video
videos.update — write
50 units each
part=snippet replaces the whole snippet, so the request re-sends
the description, tags and categoryId exactly as
they were read back (categoryId is required and falls back to
"22").
Send only the title and YouTube silently wipes the description, the tags and the category. That is the bug this shape of the code exists to prevent.
New title
goes live
YouTube Data API v3
The title on the video now matches the number of views it has.
The API allows 10,000 units a day; the app budgets 9,000 and keeps the rest as headroom. Spending is counted at the call sites themselves — never estimated — and persisted under the current Pacific date, because that is when YouTube resets the quota.
When the budget runs out the shared lane stops until midnight Pacific. The owner lane does not check it, which is exactly why it is a separate lane.
Reads are cheap — writes cost 50 Fifty videos read for one unit; one video written for fifty.