Here’s a step-by-step solution on Garuda Linux to turn your 37 MB EPG XML into a lossless, LLM-friendly, highly-compressed “mega-context” JSON.gz (or JSONL.gz) that you can feed—whole or in chunks—to any model with a ≥1 M token window.
1) Install prerequisites
sudo pacman -Syu python-lxml jq xz
You’ll use
- python3 + lxml for streaming XML→JSON conversion
- jq (optionally) to inspect or chunk JSON
- xz for extra compression (optional)
2) Write a streaming-parser script
Create epg2json.py:
#!/usr/bin/env python3
import gzip, json
from lxml import etree
# Input & output paths
IN_XML = "epg.xml"
OUT_GZ = "epg.json.gz"
# Prepare accumulators
channels = []
programmes = []
# Streaming parse
for event, elem in etree.iterparse(IN_XML, events=("end",)):
if elem.tag == "channel":
channels.append({
"id": elem.get("id"),
"display_names": [dn.text for dn in elem.findall("display-name")]
})
elem.clear()
elif elem.tag == "programme":
prog = {
"channel": elem.get("channel"),
"start": elem.get("start"), # e.g. "20240612T140000Z"
"stop": elem.get("stop")
}
title = elem.find("title")
desc = elem.find("desc")
if title is not None: prog["title"] = title.text
if desc is not None: prog["desc"] = desc.text
programmes.append(prog)
elem.clear()
# Dump a single JSON structure
with gzip.open(OUT_GZ, "wt", encoding="utf-8") as f:
json.dump({
"channels": channels,
"programmes": programmes
}, f, ensure_ascii=False)
print(f"Wrote {OUT_GZ}: channels={len(channels)} programmes={len(programmes)}")
Make it executable:
chmod +x epg2json.py
Run it:
./epg2json.py
# → epg.json.gz (typically ~4 MB)
3) (Optional) Further compression with xz
xz -z --best epg.json.gz
# → epg.json.gz.xz (even smaller)
4) Chunking for very large models
If you still worry about token-count limits, split the programmes array into N chunks. For example, 5 splits:
zcat epg.json.gz \
| jq -c '.channels as $chs | .programmes | split(5; .) | .[] | {channels:$chs, programmes:.}' \
> chunked_epg.jsonl
# This writes 5 JSON lines, each a self-contained context.
You can then gzip or xz the resulting chunked_epg.jsonl.
5) Feeding to the LLM
A) Single-blob approach
- Upload
epg.json.gz (or .xz) to your environment.
- In your system prompt or “context loader,” tell the model:
SYSTEM:
You have the entire EPG data for 1,000 channels today in JSON format (compressed).
I will now provide it. When I say “<<BEGIN EPG>>” you’ll decompress and ingest it as our context.
USER:
<<BEGIN EPG>>
[Base64 or raw of epg.json.gz …]
<<END EPG>>
B) Chunked approach
Cycle through each chunk:
SYSTEM:
We’re building up context. I will send 5 JSON chunks sequentially.
Keep them all in memory.
USER:
<<CHUNK 1 of 5>>
{ "channels": [...], "programmes": [ ...first 20%...] }
<<END CHUNK 1>>
… (repeat for chunks 2–5) …
After the last chunk:
USER:
All chunks sent. You now have the complete EPG context.
Answer my queries about “what’s on” across channels today.
Example of the final “mega-context” JSON schema
Below is a shortened example of what the fully-expanded JSON (before compression) looks like. In reality you’ll have 1,000 channels objects and ~20,000 programmes objects.
{
"channels": [
{ "id": "BBC1", "display_names": ["BBC One", "BBC 1"] },
{ "id": "CNN", "display_names": ["CNN International"] },
{ "id": "HBO", "display_names": ["HBO"] }
// … up to 1000 …
],
"programmes": [
{
"channel": "BBC1",
"start": "20240612T060000Z",
"stop": "20240612T063000Z",
"title": "BBC Breakfast",
"desc": "Morning news, weather and interviews."
},
{
"channel": "CNN",
"start": "20240612T060000Z",
"stop": "20240612T070000Z",
"title": "Early Start",
"desc": "Global news roundup."
}
// … ~20 programmes per channel …
]
}
Compressing that with gzip -9 (or xz -9) gives you a single multi-MB blob that any ≥1 M-token LLM can load wholesale, then answer all your “what’s on now/tomorrow” queries in one shot.
🎉 You’re all set! You now have:
- A streaming Python converter to turn EPG XML → JSON
- A highly-compressed
.json.gz or .json.xz file
- A chunking recipe if you need even finer control
- A prompt recipe to load it into your LLM
Now you can issue natural-language queries like:
• “What movies are on HBO between 8 PM and 11 PM tonight?”
• “Show me all live sports events on ESPN tomorrow.”
• “What cooking shows start before lunch on Food Network?”
…and the model will have the entire day’s EPG at its fingertips. Enjoy! 🚀