TV Shows in conky

Get the latest and the upcomming episodes of your favorite TV Shows in Conky (linux) with my TV Shows API.

TV Shows in conky

First you need to install requests to your python:

python3 -m pip install requests

Then create a python script conky_tv_shows.py:

#!/usr/bin/python3

# Get the next and the latest episodes of your favorite TV shows
# Author: https://bery.fun/
# Documentation: https://docs.bery.dev/api/tv-shows-in-conky/

import sys
import datetime
import requests
import json
from pathlib import Path


SETTINGS = json.load(open(Path(__file__).parent / "conky_tv_shows_settings.json", "r", encoding="utf-8"))
TODAY = datetime.datetime.utcnow().date()
HIGHLIGHT_FROM = TODAY - datetime.timedelta(days=SETTINGS["highlight_prev"])
HIGHLIGHT_TO = TODAY + datetime.timedelta(days=SETTINGS["highlight_next"])


response = requests.post(
    "https://api.bery.dev/tvshows/episodes/",
    json={
        "filter": {
            "id__in": [i["imdb_id"] for i in SETTINGS["shows"]],
            "air_date__gte": (TODAY - datetime.timedelta(days=SETTINGS["air_date_from"])).isoformat(),
            "air_date__lte": (TODAY + datetime.timedelta(days=SETTINGS["air_date_to"])).isoformat()
        },
      "limit": SETTINGS["limit"]
    },
    timeout=5
)
response.raise_for_status()
if response.status_code == 200 and response.content:
    response_data = response.json()
else:
    sys.exit(0)


data = []

if response_data.get('error'):
    error_text = f"{response_data['error']['message']}" if response_data['error'].get('message') else ""
    error_text += f" ({response_data['error']['reason']})" if response_data['error'].get('reason') else ""
    if error_text:
        data.append(f"${{color9}}{error_text}\n")

if response_data.get("items"):
    episodes_by_show = {}
    for ep in response_data["items"]:
        if ep["tv_show"]["imdb_id"] not in episodes_by_show:
            episodes_by_show[ep["tv_show"]["imdb_id"]] = []
        elif len(episodes_by_show[ep["tv_show"]["imdb_id"]]) == 2:
            continue
        episodes_by_show[ep["tv_show"]["imdb_id"]].append(ep)
    show_blocks = []
    for imdb_id, episodes in episodes_by_show.items():
        block = f"${{color8}}{episodes[0]['tv_show'].get('name', 'Unknown: ' + imdb_id)}$color\n"
        for ep in episodes:
            ep_air_date = datetime.date.fromisoformat(ep["air_date"])
            block += "${{color}}{}: {} {}   {}{}\n".format(
                "Last" if ep_air_date < TODAY else "Next",
                ep["episode"],
                ep["name"] if len(ep["name"]) <= 35 else ep["name"][:35].rsplit(' ')[0] + '…',
                "${color9}" if HIGHLIGHT_FROM <= ep_air_date <= HIGHLIGHT_TO else "",
                ep_air_date.strftime(SETTINGS["date_format"])
            )
        show_blocks.append(block)
    if show_blocks:
        data.extend(reversed(show_blocks))

cache_file_path = Path(SETTINGS["cache_file_path"])
cache_file_path.parent.mkdir(exist_ok=True)
with open(cache_file_path, "w", encoding="utf-8") as f:
    f.write("\n".join(data).replace("#", ""))

sys.exit(0)

Create a conky_tv_shows_settings.json file (in the same folder):

(add your favorite TV shows and change the configuration however you want)

{
    "shows": [
        {"imdb_id": "tt5712554", "comment": "The Grand Tour"},
        {"imdb_id": "tt3647998", "comment": "Taboo"},
        {"imdb_id": "tt1898069", "comment": "American Gods"},
        {"imdb_id": "tt2442560", "comment": "Peaky Blinders"},
        {"imdb_id": "tt7221388", "comment": "Cobra Kai"},
        {"imdb_id": "tt4236770", "comment": "Yellowstone"},
        {"imdb_id": "tt8714904", "comment": "Narcos: Mexico"},
        {"imdb_id": "tt5180504", "comment": "The Witcher"},
        {"imdb_id": "tt5607976", "comment": "His Dark Materials"},
        {"imdb_id": "tt9140554", "comment": "Loki"},
        {"imdb_id": "tt7462410", "comment": "The Wheel of Time"},
        {"imdb_id": "tt1190634", "comment": "The Boys"},
        {"imdb_id": "tt11126994", "comment": "Arcane"},
        {"imdb_id": "tt9612516", "comment": "Space Force"},
        {"imdb_id": "tt10541088", "comment": "Clarkson's Farm"},
        {"imdb_id": "tt12785720", "comment": "The Witcher: Blood Origin"},
        {"imdb_id": "tt7631058", "comment": "The Lord of the Rings: The Rings of Power"}
    ],
    "air_date_from": 3,
    "air_date_to": 30,
    "limit": 10,
    "cache_file_path": "/path/to/a/conky/tv_shows/cache/file",
    "highlight_prev": 1,
    "highlight_next": 2,
    "date_format": "%a %d.%m.%Y"
}

And to your crontab -e add a line:

*/30 * * * * python3 /path/to/the/conky_tv_shows.py

And into your .conkyrc put something like:

${color2}TV SHOWS ${hr 1}$color
${execpi 60 cat /path/to/a/conky/tv_shows/cache/file}

And that's all :) Enjoy your favorite TV shows