azlm8t / tv_grab_az_sdjson_sqlite

XMLTV grabber for Schedules Direct JSON service
GNU General Public License v2.0
9 stars 1 forks source link
mythtv tvheadend xmltv

tv_grab_az_sdjson_sqlite / tv_meta_az_sd.py

XMLTV grabber for Schedules Direct JSON service, optimized for large numbers of channels, with local fast caching of xmltv output, and IMDB support. Also includes a Tvheadend metadata program for populating fanart for Kodi.

Place the tv_grab_az_sdjson_sqlite and tv_meta_az_sd.py in the same location as existing grabber files (probably /usr/bin).

Overview

The tv_grab_az_sdjson_sqlite program grabs schedule information from SchedulesDirect and creates an xmltv file for use by PVRs (such as Tvheadend and MythTV).

The tv_meta_az_sd.py integrates with "tvhmeta" in Tvheaded 4.3+ to provide additional fanart for Kodi 18+. It needs to be put in the same directory as Tvheadend's "tvhmeta" program. The "fanart" checkbox in the Configuration->Recording->(expert) then needs to be ticked. The Tvheadend server with then call tv_meta_az_sd.py at the approriate time to populate fanart.

You need a SchedulesDirect account for these programs to work. Information from SchedulesDirect is stored in a local SQLite database. Generated information can also be cached locally to improve performance.

This program adds a number of extra features for updating the description with extra information. This is useful because many GUIs do not readily display details about programmes such as season and episode information or cast information.

Information about artwork is also downloaded and this can also be included in the description as URL tags that are displayed by some clients.

See the options --update-description-with-all and --update-description-with-artwork for extra information.

Many of the options can be prefixed with "no-" in order to disable them, such as "--update-all --no-update-description-with-premiere --no-update-description-with-live"

Abbreviated samples of descriptions are given below for a movie and a tv programme episode.

====

Rambo: First Blood (1982).

(1982). (R). ☆☆☆. Vietnam veteran...

Advisory: Adult Language, Adult Situations, Brief Nudity, Graphic Violence.

Cast: Sylvester Stallone, Richard Crenna...

Categories: Action, Feature Film, Movie.

====

How I Met Your Mother - S03E16 - Sandcastles in the Sand.

S3/9 E16/20. (2008-04-21). (TV-PG). Robin rekindles...

Cast: Josh Radnor, Jason Segel...

Categories: Episode, Series, Show, Sitcom.

====

Support for Multiple Lineups

Some countries require multiple SD channel lineups to populate all the channels that can be received by one tuner.

To support this, the configuration file can be manually edited to add multiple "lineup=" lines.

For example if your configuration file contained "lineup=XXX-1234-DEFAULT" then you could alter it to be:

lineup=XXX-1234-DEFAULT
lineup=XXX-4567-DEFAULT
lineup=XXX-890A-DEFAULT

Then the three lineups are fetched and all the channels and programmes from all three lineups are output in the same xmltv output file.

Alternatively the UI can be used with the --configure option to add all lineups to the configuration file.

Local Caching of Formatted Data

This is an experimental feature. That means the options may change or be removed in future releases and the generated output has not been rigorously tested.

Local caching of formatted programme data can be enabled. This will cache xmltv output to a local server such as to Redis. The sqlite database (containing details of downloaded data) is separate from the xmltv-output formatted caching done with redis.

This significantly reduces the time taken for subsequent runs when fetching multiple days of listings. To enable this you will need the Perl module "CHI" and "CHI::Driver::Redis" and a working Redis server running locally.

Caching can also allow the xmltv file to only contain changes, rather than a complete listing via --cache-ignore-unchanged-programmes. So, by default, SD can have several week's of data and an xmltv file will say "news is on at 8pm on 1st April", and on the following day's run it will say again "news is on at 8pm on 1st April", and so on for several weeks. With --cache-ignore-unchanged-programmes, the xmltv file will only contain the item once. This avoids the PVR needing to process the same information repeatedly every day.

The cache can be used via the extra arguments: "--cache-driver=Redis"

Other Perl CHI drivers can be used, but are not rigorously tested such as: "--cache-driver=File"

The first time the programme is run, the formatted output is cached, so the run takes longer than normal as this information is saved. On the second and subsequent runs, the cache is checked, and if the formatted output is already cached, then it is retrieved.

With multiple days and hundreds of channels, this can significantly reduce overhead.

To check that redis caching is occurring, you can use "redis-cli --stat" to monitor keys/memory usage of the redis server. The server must have enough space to hold the cached data (maxmemory setting in the redis server's redis.conf). Since the server only uses as much memory as it needs, it can be useful to set it to a high value such as "maxmemory 1gb", and then monitor how much memory is actually used over the course of a week.

Ensure that the redis.conf does not have a "maxmemory-policy" of "noeviction". For example, set it to "maxmemory-policy allkeys-lru".

If you need to erase all cached entries in Redis then you can use "redis-cli flushdb".

File caching is an alternative to redis and can be used if you have a fast filesystem. Since file system does not natively support purging, it is recommended to also add the purge option. This is selected via "--cache-driver=File --cache-purge-expired".

IMDB Support

Experimental IMDB support is included.

This feature adds details to the generated xmltv from IMDB such as ratings for shows, URL information, etc. This is useful since SchedulesDirect does not provide ratings for shows, only for movies, and many of the URLs it provides for movies are outdated.

An example of the additional IMDB fields in the generated file:

<url >https://www.imdb.com/title/tt2350522</url>
<url >https://www.imdb.com/title/tt1219024</url>
<episode-num system = "imdb.com" >episode/tt2350522</episode-num>
<episode-num system = "imdb.com" >series/tt1219024</episode-num>
<star-rating system = "imdb" ><value >9/10</value></star-rating>

Note that many lookups will fail primarily because tv guides are full of local shows, or have programmes that are old where there are discrepencies on when the show was made or how it should be spelled (such as "Quincy", "Quincy, M.E.", and "Quincy M.E."). Although we could handle more special cases, that would unfortunately slow down the majority of lookups that work.

It is recommended that the redis cache server is enabled to use IMDB since it will allow caching of mapping information, see "Local Caching" options such as "--cache-driver=Redis".

The IMDB support needs mysql server running since sqlite was too slow for the large volumes of data that needs to be frequently imported.

As user root:

mysql
create database imdb character set utf8mb4;
create user 'imdb'@'%' identified by 'imdb';
grant all on imdb.* to 'imdb'@'%';
create user 'imdb'@'localhost' identified by 'imdb';
grant all on imdb.* to 'imdb'@'localhost';
grant file on *.* to 'imdb'@'localhost';
flush privileges;

The grabber needs to be run with the extra argument --use-imdb, with additional configuration available if you choose to use a different username, password, database, etc. (see below). A common additional argument is "--prefer-imdb-rating" to prefer the IMDB ratings over the SchedulesDirect ratings.

The IMDB datasets can be downloaded from https://www.imdb.com/interfaces/ You need the title.basics.tsv.gz, title.episode.tsv.gz, and title.ratings.tsv.gz (see --imdb-download for option to automatically download the files if we have write access to the mysql secure directory).

These files should be uncompressed and placed in to your mysql "secure" directory. This is because modern mysql locks down bulk loading of data. mysql -p -e "SHOW VARIABLES WHERE Variable_Name = 'secure_file_priv'"

For me, this gives /var/db/mysql_secure or /var/lib/mysql-files/ depending on the server.

The files will be automatically loaded if the grabber has access to this directory. If necessary then the directory can be specified with --imdb-dir=xxx, the default is /var/db/mysql_secure.

The files will need to be periodically downloaded and they will be automatically reloaded in to the database. See --imdb-download option.

Extra Options

Extra Options for Updating Description

These options alter the programme's description. This can be useful since some frontends do not display programme year, age rating, season, episode, star rating, or other information in some views.

Extra Supported Tags and Features

The program has a few features that are enabled without user configuration options:

Extra Packages Required

A list of extra (potentially non-standard) packages you may need to install is below:

For local (advanced) caching you also need:

The other modules used are typically already installed as part of xmltv.

Different distributions may have different dependencies but this will install dependencies on some distributions:

sudo apt install xmltv libdatetime-format-sqlite-perl libipc-run3-perl libjson-xs-perl liblwp-protocol-https-perl liburi-escape-xs-perl libchi-perl libchi-driver-redis-perl

Some distributions might ship with fewer dependencies pre-installed as part of the base system. If you have an error stating "you may need to install the ABC::DEF module" then you can normally install it using sudo apt install libabc-def-perl (note the installed package name is all lowercase and has lib prefix and -perl suffix). If this does not work, you can normally find the package name using apt search libabc-def-perl or apt search ABC::DEF.

Miscellaneous Information

In Kodi, to view the star rating you may have to change the font to "Arial Based" in Settings/Interface Settings/Skin/Fonts, otherwise it may appear as square blocks instead of stars.

The airdate may be incorrect for your country since it tends to be the first worldwide airdate.

Total number of seasons is known to be sometimes incorrect when a new series is released.

Programmes marked as "new/premiere" are based on information provided to Schedules Direct by the networks. So, a movie may be "new" because it's the first time a network has purchased the rights to show the movie. This means a movie from 1970 may be marked as "new" on one network but could be a "repeat" on a different network. Similarly, some networks mark programmes as "new" based on the timeslot. So a programme could be "new" at 9pm and the same episode could also be "new" the following day at 2am.

How Do You (Personally) Run This Program?

I like to have long listings on channels I watch, and shorter listings on all the other channels such as shopping.

For mythtv/mythweb, I like to have artwork injected in the description.

I used to use File caching backend since I have a reasonably fast filesystem backed by SSD and it seems a bit faster than Redis on my system. However, I now use Redis since it performs purging faster than File.

So I have a script that is run via crontab. It is similar to:

#! /bin/sh

# Interesting channels have this text in their name (separated by pipe symbol)
CHREGEX="BBC|Movie|Film|Sony"

COMMONARGS="--config-file sd.conf --merge-split=5 --artwork-max-width=720 --update-description-with-all --update-description-with-artwork --update-previously-shown-with-year --cache-driver=Redis --cache-ignore-unchanged-programmes --benchmark --cache-ignore-unchanged-programmes --no-channel-output --use-imdb --prefer-imdb-rating --imdb-download=month"
tv_grab_az_sdjson_sqlite --output out.xml --days 10 --channel-short-days=3 --channel-short-days-exclude-regex="$CHREGEX" $COMMONARGS
mythfilldatabase --file --xmlfile out.xml --sourceid 1

For tvheadend, I don't use artwork in the descriptions since its grid doesn't handle them very well:

tv_grab_az_sdjson_sqlite --output tvh.xml $COMMON_ARGS --no-update-description-with-artwork --cache-namespace-extra=tvh
nc -w 5 -U /home/hts/.hts/tvheadend/epggrab/xmltv.sock < tvh.xml

Most people don't need this level of complexity and can use the script from within the PVR directly, perhaps adding appropriate "extra arguments" such as "--artwork-max-width=720 --cache-driver=Redis".