# TV guide — TV Filmes & Séries

A personal weekly guide to the **movies and series** on Portuguese TV (MEO channel line-up, with
NOS and Vodafone availability), with IMDb ratings, likely series/season premieres and the movies
still available in the 7-day automatic recordings / replay.

It has two parts:

| part | what it does |
|---|---|
| `app/fetch_meo.py` | downloads the schedule once a day (MEO public GridTv API, IMDb public datasets, NOS/Vodafone line-ups) and writes `data/week.json` |
| `web/` | the web app (plain HTML/CSS/JS, no build step) that shows `data/week.json` |
| `app/serve.py` | a small local web server for the two (browsers can't load the data from `file://`) |

Everything is Python standard library: **Python 3.9 or newer** is the only requirement.
The web app is in Portuguese.

## Install

### Windows 10 / 11

1. Install Python 3.9+ from [python.org](https://www.python.org/downloads/) (tick *Add python.exe to PATH*),
   or let the installer do it with `winget`.
2. Unzip `tvguide-1.0.0.zip` and double-click **`install.cmd`**.

This installs for your user only, no administrator rights needed:

- **Files:** `%LOCALAPPDATA%\tvguide`.
- **Scheduled Tasks** (in a *tvguide* folder):
  - "Refresh TV schedule", daily at 05:30;
  - "TV guide web server", at logon.
- **Start menu:** a **TV Guide** shortcut to <http://127.0.0.1:8080/>.

Options go on the command line:

```
powershell -ExecutionPolicy Bypass -File install.ps1 -Port 8090 -Time 06:15 [-Bind 0.0.0.0] [-NoServer]
```

To uninstall, run `%LOCALAPPDATA%\tvguide\uninstall.cmd`. Add `-Purge` to also delete the data.

### macOS

```
tar xzf tvguide-1.0.0.tar.gz && cd tvguide && ./install.sh
```

- **Python:** `python3` from Xcode Command Line Tools (`xcode-select --install`), python.org or Homebrew.
- **Files:** `~/Library/Application Support/tvguide`.
- **Logs:** `~/Library/Logs/tvguide`.
- **Background jobs:** two launchd agents run the daily refresh and the web server.

### Linux

- **Per user** (systemd user units, or cron when systemd is missing):

  ```
  tar xzf tvguide-1.0.0.tar.gz && cd tvguide && ./install.sh
  ```

  - **Files:** `~/.local/share/tvguide`.
  - **Data:** `~/.local/state/tvguide`.
  - **Keep it running after logout:** user services stop when you log out. Run `sudo loginctl enable-linger $USER`.

- **System-wide** (servers, Raspberry Pi):

  ```
  sudo ./install.sh --system                                # built-in server on 127.0.0.1:8080
  sudo ./install.sh --system --webroot /var/www/html/movies # publish through Apache/nginx instead
  ```

  - **Files:** `/opt/tvguide`.
  - **Data:** `/var/lib/tvguide`.
  - **Runs as:** user `tvguide`, with hardened systemd units.
  - **With `--webroot`:** the web app is copied there and the data is written to `<webroot>/data`.

To uninstall, run `~/.local/share/tvguide/uninstall.sh`. For a system install it's `sudo /opt/tvguide/uninstall.sh`. Add `--purge` to also delete the data.

### Installer options

| option | Linux / macOS (`install.sh`) | Windows (`install.ps1`) |
|---|---|---|
| port of the built-in server | `--port 8080` | `-Port 8080` |
| listen address | `--bind 127.0.0.1` | `-Bind 127.0.0.1` |
| daily refresh time | `--time 05:30` | `-Time 05:30` |
| no built-in server | `--no-server` | `-NoServer` |
| don't refresh right away | `--no-fetch-now` | `-NoFetchNow` |

`--bind 0.0.0.0` / `-Bind 0.0.0.0` makes the guide reachable from other devices on your network.
The built-in server is read-only and doesn't list directories, but it has no password.

## First run

The first refresh downloads details for every programme of the past and next week, which takes **10–30 minutes**.
It keeps a cache, so later daily refreshes take about 2 minutes. Until the first refresh finishes, the page shows
*"A carregar…"* or an error.

Progress is logged here:

| install | log |
|---|---|
| Linux, systemd | `journalctl [--user] -u tvguide-fetch` |
| Linux with cron, or macOS | `fetch.log` in the state/log directory |
| Windows | `%LOCALAPPDATA%\tvguide\logs\fetch.log` |

## Running it by hand

```
python3 app/fetch_meo.py --out web/data --cache state/cache.json   # refresh once
python3 app/serve.py --web web --data web/data --port 8080          # serve http://127.0.0.1:8080/
```

`fetch_meo.py --help` lists the other options: days ahead, past days, workers, `--no-imdb`.

## Publishing with your own web server

The web app is static. Copy `web/` to any web root (or use `install.sh --system --webroot DIR`).
Then run `fetch_meo.py --out DIR/data` daily, as a user that can write to `DIR/data`.
Nothing else is needed: no PHP, no database. For Apache, an alias is enough:

```
# more specific alias first
Alias /tv/data/ /var/lib/tvguide/data/
Alias /tv/ /opt/tvguide/web/
<Directory /opt/tvguide/web>
    Require all granted
</Directory>
<Directory /var/lib/tvguide/data>
    Require all granted
    Header set Cache-Control "no-store"
</Directory>
```

## Notes

- **Personal, unofficial tool.** It has no connection to MEO, NOS, Vodafone or IMDb.
  - **Schedules:** come from the public endpoints that the operators' own websites use.
  - **Ratings:** come from IMDb's public datasets, licensed for personal and non-commercial use only.
  - **Load:** keep the default single daily refresh so the services aren't stressed.
- **If an operator changes its API, that part stops working.** The previous data is kept: the fetcher refuses to
  overwrite a good file with an empty or much smaller one.
- **Time zone:** schedules are in Lisbon time. On Windows without the `tzdata` package, the built-in
  Europe/Lisbon rules are used.
- **Your settings:** filters, chosen channels and so on are stored in your browser (localStorage).

Version: see `VERSION`. Source: <https://zedathome.no-ip.org/downloads/tvguide/>
