Wednesday 17 April 2019

Easy way to get EPG into Tvheadend Server

One of the headaches of TVheadend is providing an easy method of keeping the EPG updated, I maintain a large tvheadend server for a large secondary school giving them the ability to record and play live terrestrial tv into classrooms.

This "how-to" is noddy or noob format it assumes you know nothing about anything. 

I used to use the built in epg but found xmltv.co.uk to provide excellent epg data without subscription and is free to use here in the UK.

First off disable all other methods of EPG grabber modules in tvheadend, and enable External XMLTV grabber, make sure you install xmltv package on your tvheadend server and that .xmltv directory appears in tvheadend directory



ls -al /home/hts/.xmltv 

and that the unix socket works and is available

ls /home/hts/.hts/tvheadend/epggrab/xmltv.sock
/home/hts/.hts/tvheadend/epggrab/xmltv.sock  (good it exists!!!)

there are many how-to's available on installing the xmltv package and are beyond the scope of this "how to", in most cases just installing the xmltv package will be enough

ie/ apt-get install xmltv 

should suffice 


then simply create this bash script shown below (I've called it get-epg.sh) and edit it to get the appropriate xml file you require, the type of xml file is in the hashed comments/description
I selected 6715 which is 3 days and all data, simply change it to what you require by changing the number on the end of the wget statement

ie/ nano get-epg.sh



#!/bin/bash
# Feeds available
# Select which one you require and attach it to
# end of wget command
#6721   All 24 Hour All data
#6145   All 24 Hour Entertainment
#6128   All 24 Hour Movies All data
#6187   All 24 Hour Sports All data
#6729   All 48 Hour All data
#6715   All 72 Hour All data
#6793   Limited 5 Days Limited data
#1897   Limited 7 Days  Limited data
clear
echo Removing old xmltv file
rm -rf /home/hts/xmltv.xml
echo Get new xmltv file
wget --output-document /home/hts/xmltv.xml http://www.xmltv.co.uk/feed/1897
echo Touch new file with date downloaded
touch /home/hts/xmltv.xml
echo Copy new file to locations
cp /home/hts/xmltv.xml /home/hts/.xmltv/xmltv.xml
echo Update Completed

you may have to chown (change ownership) the new script to the hts user in my case

chown hts:video get-epg.sh

then su to the hts user (become the hts user) by entering

su hts

then create a new cron job

crontab -e

then copy/paste the line below, it will execute it at 6am every day and in my case get 3 days worth of
epg at a time

0 6 * * *  hts /home/hts/get-epg.sh

followed by Ctrl o (that's o for orange if using nano) to write out the new crontab 


it's pretty self explanatory, remove old file get the new xml file and rename it on save as xmltv.xml and put it in the /home/hts directory, touch it to change the date downloaded (more of a belts and braces touch to make sure the date is updated so you can easily check if the file is current) 

Then copy it to the .xmltv directory, you could of course just copy it to the xmltv directory directly or create a symlink either would work, but it's such a small file 30MB tops that it takes less than a second to copy it. 

it's a very simple bash script and no doubt it can be improved upon, but I like simple you can easily follow it.

voila a simple and automated way to get UK EPG data into your tvheadend server, restart your tvheadend server and you should see your EPG data populate and have 3 days worth.

There is no point in getting the epg data more than once every 24 hours because it's not updated any sooner than once every 24 hours in each day.

xmltv.co.uk sometimes change the feed names so you need to check those first and amend the script accordingly, I've also noticed sometimes the website goes offline with no explanation


so make sure you can actually browse the website before debugging the script

Latest xmltv feeds