MovieAdvisor
Pulls the movie catalogue from the unofficial Cineplex API
A service that pulls the movie catalogue from the unofficial Cineplex API, keeps it in SQLite and answers one question: which movies has this chat not been shown yet? The catalogue is refreshed on a built-in cron schedule; the "already shown" state is kept separately per Telegram chat. It used to be a console tool run once a day that printed new releases to stdout. It is now a REST API (MovieAdvisor.RestApi) called by a stdio MCP server (MovieAdvisor.McpServer), which in turn plugs into an AI bot. 1. Catalogue refresh.
Summary extracted from the README.
# MovieAdvisor
A service that pulls the movie catalogue from the unofficial Cineplex API, keeps it in SQLite and answers one question: which movies has this chat not been shown yet? The catalogue is refreshed on a built-in cron schedule; the "already shown" state is kept separately per Telegram chat.
It used to be a console tool run once a day that printed new releases to stdout. It is now a
REST API (MovieAdvisor.RestApi) called by a stdio MCP server (MovieAdvisor.McpServer), which
in turn plugs into an AI bot.
Architecture
| Interface | Implementation | Purpose |
|---|---|---|
IMovieProvider |
CineplexMovieProvider |
The movie catalogue (every API field, plus ExtraFields for whatever the API adds later) |
ICineplexApiKeyProvider |
CineplexApiKeyProvider |
Supplies and caches Ocp-Apim-Subscription-Key; on a 401 it re-scrapes the current key from the website bundle |
IMovieFilter |
ConfigurableMovieFilter |
IsWorthWatching(Movie) — whether a movie is worth offering at all |
IMovieStore |
SqliteMovieStore |
The catalogue plus the per-chat history of what was shown |
Two independent flows:
- Catalogue refresh.
CatalogRefreshScheduler(cron inside the process) callsCatalogRefreshService: fetch the catalogue, upsert it into SQLite, prune what has gone stale. If the provider is unreachable, the cycle logs the failure and schedules the next run; the API keeps serving the data it already has. - A chat request.
POST /api/chats/{chatId}/new-moviestakes the movies whose tickets are on sale (HasShowtimes, whether or not they are already running) that this chat has not seen, ordered by what is most immediate — already released newest first, then upcoming soonest first — runs them throughIMovieFilter, and — in a single transaction — returns them and marks them as shown. The same movie cannot reach a chat twice, even if two requests arrive at once.
The filter runs per request rather than per refresh, so edits to the Filter section take
effect immediately instead of waiting for the next refresh.
Endpoints
| Method | Path | What it does |
|---|---|---|
POST |
/api/chats/{chatId}/new-movies?limit=&includeFilteredOut= |
Returns the movies this chat has not been shown and marks them as shown. POST, because it changes state |
GET |
/api/chats/{chatId}/shown-movies?days=&limit= |
What the chat was already shown, newest first |
DELETE |
/api/chats/{chatId}/shown-movies |
Forgets the chat history: the whole current catalogue counts as new again |
POST |
/api/catalog/refresh |
Refreshes the catalogue right now |
GET |
/api/catalog/status |
How many movies are stored, how many have tickets on sale, when they were last refreshed |
GET |
/health |
Health probe (the only path that never requires the token) |
chatId is part of the route and comes from the caller. The MCP server takes it from its own
TELEGRAM_CHAT_ID environment variable rather than from a tool argument: an argument is
something the model can fill in with a valid-but-wrong value, and no server-side validation can
catch that.
Ready-made requests live in MovieAdvisor.RestApi/MovieAdvisor.RestApi.http.
new-movies response
{
"chatId": -1001234567890,
"batchId": "d92e5e96b227447ea6a99ad3c56b12c2",
"replayed": false,
"remaining": 35,
"movies": [ { "id": 61830, "name": "...", "releaseDate": "...", "runtimeInMinutes": 108,
"language": "English", "genres": ["Action"], "detailPageUrl": "...", "posterUrl": "..." } ]
}
remaining is how many unshown movies are still waiting, so a caller can offer more.
replayed means this batch has been handed out before — see the replay window below.
State
SQLite, four tables: movies (the catalogue), chat_shown_movies (what was shown to whom),
chat_batches (the replay window) and meta (the last refresh time). The database runs in WAL
mode with a busy_timeout: both the scheduler and concurrent chat requests write to it.
Replay window (Catalog:ReplayWindowSeconds, 3 s by default). An identical repeated request
from the same chat inside the window returns the same batch instead of the next one. This
protects against a retried turn: when the model hits a quota error, the bot re-runs the whole
prompt on a fallback model and the tool is called a second time — without the window, the first
batch would already be marked as shown while the user never saw it. The trade-off is real: a user
who asks for more faster than the window gets the same list back with replayed: true. That is
why the window is short; 0 disables the mechanism entirely.
Catalog:RetentionDays (365) — movies that have not appeared in the catalogue for that long, and
history entries older than that, are deleted after every refresh.
Configuration
Sections of appsettings.json; every key can be overridden by an environment variable
(Cineplex__PageSize=50, Catalog__RefreshCron=...).
| Section | Keys |
|---|---|
Api |
Token — shared bearer token. Empty means no authentication (private networks only) |
Database |
FilePath (a relative path resolves against the application directory), BusyTimeoutSeconds |
Catalog |
RefreshCron, TimeZone, RefreshOnStartup, RetentionDays, DefaultLimit, MaxLimit, ReplayWindowSeconds, DefaultHistoryDays, MaxHistoryLimit |
Cineplex |
BaseUrl, WebsiteUrl, SubscriptionKey, KeyCacheFilePath, Language, PageSize |
Filter |
ExcludedLanguages, ExcludedGenres, ExcludedNameSubstrings, ExcludeEvents, MaxRuntimeMinutes |
Invalid configuration fails at startup (ValidateOnStart) rather than on the first request: an
empty BaseUrl, a broken cron expression, a DefaultLimit above MaxLimit, and so on.
Running
dotnet run --project MovieAdvisor.RestApi # http://localhost:5042
Docker (keep the state on a volume, or the catalogue is empty after every restart until the next refresh):
docker compose up -d --build
curl -X POST http://localhost:8080/api/catalog/refresh
curl -X POST "http://localhost:8080/api/chats/-1001234567890/new-movies?limit=5"
Both calls need -H "Authorization: Bearer <token>" once MOVIEADVISOR_API_TOKEN is set for the
container.
The movieadvisor-state volume is mounted at /data and holds movieadvisor.db and
cineplex-key.json. The cron schedule lives inside the process, so no separate container with a
system cron is needed. To drive the schedule from outside instead, set
Catalog__RefreshOnStartup=false, give Catalog__RefreshCron a deliberately rare expression and
call POST /api/catalog/refresh yourself.
MCP server
MovieAdvisor.McpServer is a local stdio MCP server over this API. It exists so an AI agent in a
chat can ask "which new movies have I not seen yet" without being able to reach another chat's
history.
| Tool | What it does |
|---|---|
list_new_movies(limit?, includeFilteredOut?) |
Unshown movies with tickets on sale (status tells a film that is already running from one on presale), which are marked as shown in the process. remaining is how many are left; replayed: true means this is the same batch as a moment ago (see the replay window) |
list_shown_movies(days?) |
What this chat was already shown |
reset_movie_history() |
Forgets the chat history |
No tool takes a chat id. The server serves exactly one chat and reads its id from the
TELEGRAM_CHAT_ID environment variable, which the bot injects per chat when it spawns the agent.
The reason is specific: a chat id passed as an argument can be filled in by the model with a
valid-but-wrong value — stale from an earlier turn, or a digit off — and unlike, say, a file path,
no server-side validation can tell a wrong chat id from a right one. If the variable is missing,
empty or unparsable, the server refuses to start instead of guessing. Empty matters in
practice: blanking a variable is the only thing an MCP host can do to one it inherited, it cannot
unset it.
Its own settings are MovieAdvisorApi__BaseUrl, MovieAdvisorApi__ApiToken and
MovieAdvisorApi__TimeoutSeconds; see MovieAdvisor.McpServer/README.md for the details and a
host config example.
About the Cineplex API and the self-healing key
The API is unofficial. Ocp-Apim-Subscription-Key is a public key embedded in the cineplex.com
JS bundle. It rotates (confirmed: the key in the bundle changed within a day), so the service
refreshes it on its own and the hardcoded value in the config normally never needs touching.
How it works (CineplexApiKeyProvider):
- The key is taken from the cache (
cineplex-key.json), or fromCineplex:SubscriptionKeywhen there is no cache yet. - If the API answers 401/403,
CineplexMovieProvidercallsRefreshKeyAsync: it downloads the home page, finds the primary Next.js chunks and extracts the key from them. - The fresh key is cached, so later runs skip the scrape.
One subtlety: the bundle carries several keys, one per Cineplex backend. The scraper does not
take the first one it finds but the one sitting closest in the source to the URL of the API we
need (.../theatrical/...) — otherwise it is easy to grab a key belonging to another backend,
which this endpoint may well reject.
Scraping is a fallback, not the main path: while the current key works, the only thing touched over the network is the API itself.
Extension ideas
- IMDB / Rotten Tomatoes: a separate
IMovieEnricher(or a decorator overIMovieProvider) that adds ratings toMovie, with the filter gaining fields likeImdbRating < 7.0m. Every Cineplex field is already passed through as-is, and unknown ones land inMovie.ExtraFields. - Push notifications: the database already holds both the catalogue and the per-chat history, so "post new releases to the chat by yourself" is another consumer of the same store rather than a schema change.
- Filtering by cinema: Cineplex has a showtimes-per-venue endpoint, which fits as one more
step in
CatalogRefreshService.