Before writing your own script to sort or merge a programme guide, check whether the XMLTV project already ships a tool for it. Usually it does. GPL-2.0, mostly Perl, and packaged in every major distribution.
Source: Log in or register to see this link
sudo apt install xmltv-util
The ones I reach for
tv_sort puts programmes in order by time and channel, fills in missing stop times where it can, and complains about overlaps.
tv_sort --output sorted.xml guide.xml
tv_cat joins several files into one. This is the right way to merge two sources. Pasting files together by hand gives you two root elements and a file no player will read.
tv_cat --output merged.xml source1.xml source2.xml
tv_grep filters. By channel, by title, by time.
tv_grep --channel-id news.one --output news.xml guide.xml
tv_grep --on-after "$(date +%Y%m%d%H%M%S)" --output upcoming.xml guide.xml
tv_validate_file checks a file against the format rules. Run it on anything you are about to publish.
tv_validate_file guide.xml
tv_to_text prints a readable listing, which is the quickest way to see whether the times are what you think they are.
Grabbers
The package family also contains grabbers, programs named tv_grab_something that fetch listings for a country or a provider.
tv_find_grabbers
lists the ones installed. Each needs to be configured once (tv_grab_xx --configure) to choose channels, and after that it writes XMLTV to standard output.
Grabbers read other people’s websites and services. Check that the source allows it, keep the request rate low, and expect them to break whenever the site changes.
A daily pipeline
tv_cat a.xml b.xml | tv_sort | tv_grep --on-after "$(date +%Y%m%d%H%M%S)" > /tmp/guide.xml \
&& tv_validate_file /tmp/guide.xml \
&& gzip -c /tmp/guide.xml > /srv/www/guide.xml.gz.tmp \
&& mv /srv/www/guide.xml.gz.tmp /srv/www/guide.xml.gz
The && chain matters. If any step fails, the published file stays as it was, and players keep yesterday’s guide instead of getting a broken one. The final mv is there so nobody downloads a half-written file.
Where these tools stop
They load the whole file into memory and they are not fast. On a guide of a few hundred megabytes, tv_sort can take minutes and gigabytes. For big files, cut the data down first (by channel and by days), then sort what is left. There is a Python trimmer in the EPG Scripts forum that does exactly that.