Skip to content

Commit

Permalink
Add the option to define a channel time offset in the sources.xml
Browse files Browse the repository at this point in the history
This allows epgimport to correct start- and endtimes of events, as several xmltv sources use local time with +0000 instead of the correct timezone offset. To use this, define the offset like so:
````
<source type="gen_xmltv" nocheck="1" offset="-0200" channels="/etc/epgimport/some.xmltvfile.channels.xml">
 <description>Some XMLTV file</description>
 <url>http://epg.example.org/some,xlmtvfile.gz></url>
</source>
````
in this case because the times in the file are in GMT+2, but the offset is +0000, indicating GMT was used. The offset "-0200" corrects GMT+2 to GMT.
  • Loading branch information
WanWizard authored and Huevos committed Nov 19, 2023
1 parent d301f0b commit 00a8391
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
16 changes: 10 additions & 6 deletions src/EPGImport/EPGConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def isLocalFile(filename):
return '://' not in filename


def getChannels(path, name):
def getChannels(path, name, offset):
global channelCache
if name in channelCache:
return channelCache[name]
Expand All @@ -39,20 +39,21 @@ def getChannels(path, name):
return channelCache[channelfile]
except KeyError:
pass
c = EPGChannel(channelfile)
c = EPGChannel(channelfile, offset=offset)
channelCache[channelfile] = c
return c


class EPGChannel:
def __init__(self, filename, urls=None):
def __init__(self, filename, urls=None, offset=0):
self.mtime = None
self.name = filename
if urls is None:
self.urls = [filename]
else:
self.urls = urls
self.items = None
self.offset = offset

def openStream(self, filename):
fd = open(filename, 'rb')
Expand Down Expand Up @@ -122,7 +123,7 @@ def __repr__(self):


class EPGSource:
def __init__(self, path, elem, category=None):
def __init__(self, path, elem, category=None, offset=0):
self.parser = elem.get('type')
nocheck = elem.get('nocheck')
if nocheck == None:
Expand All @@ -135,10 +136,11 @@ def __init__(self, path, elem, category=None):
self.url = random.choice(self.urls)
self.description = elem.findtext('description')
self.category = category
self.offset = offset
if not self.description:
self.description = self.url
self.format = elem.get('format', 'xml')
self.channels = getChannels(path, elem.get('channels'))
self.channels = getChannels(path, elem.get('channels'), offset)


def enumSourcesFile(sourcefile, filter=None, categories=False):
Expand All @@ -147,7 +149,9 @@ def enumSourcesFile(sourcefile, filter=None, categories=False):
for event, elem in iterparse(open(sourcefile, 'rb'), events=("start", "end")):
if event == 'end':
if elem.tag == 'source':
s = EPGSource(sourcefile, elem, category)
# calculate custom time offset in minutes
offset = int(elem.get('offset', '+0000')) * 3600 // 100
s = EPGSource(sourcefile, elem, category, offset)
elem.clear()
if (filter is None) or (s.description in filter):
yield s
Expand Down
2 changes: 1 addition & 1 deletion src/EPGImport/EPGImport.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def channelDownloadFail(self, failure):
def createIterator(self, filename):
# print("[EPGImport][createIterator], filename", filename)
self.source.channels.update(self.channelFilter, filename)
return getParser(self.source.parser).iterator(self.fd, self.source.channels.items)
return getParser(self.source.parser).iterator(self.fd, self.source.channels.items, self.source.offset)

def readEpgDatFile(self, filename, deleteFile=False):
if not hasattr(self.epgcache, 'load'):
Expand Down
4 changes: 2 additions & 2 deletions src/EPGImport/gen_xmltv.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def new():


class Gen_Xmltv():
def iterator(self, fd, channelsDict):
def iterator(self, fd, channelsDict, offset=0):
try:
xmltv_parser = xmltvconverter.XMLTVConverter(channelsDict, gen_categories, date_format)
xmltv_parser = xmltvconverter.XMLTVConverter(channelsDict, gen_categories, date_format, offset)
for r in xmltv_parser.enumFile(fd):
yield r
except Exception as e:
Expand Down
8 changes: 5 additions & 3 deletions src/EPGImport/xmltvconverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ def enumerateProgrammes(fp):


class XMLTVConverter:
def __init__(self, channels_dict, category_dict, dateformat='%Y%m%d%H%M%S %Z'):
def __init__(self, channels_dict, category_dict, dateformat='%Y%m%d%H%M%S %Z', offset=0):
self.channels = channels_dict
self.categories = category_dict
if dateformat.startswith('%Y%m%d%H%M%S'):
self.dateParser = quickptime
else:
self.dateParser = lambda x: time.strptime(x, dateformat)
self.offset = offset
print("[XMLTVConverter] Using a custom time offset of %d" % offset)

def enumFile(self, fileobj):
print("[XMLTVConverter] Enumerating event information", file=log)
Expand All @@ -87,8 +89,8 @@ def enumFile(self, fileobj):
continue
try:
services = self.channels[channel]
start = get_time_utc(elem.get('start'), self.dateParser)
stop = get_time_utc(elem.get('stop'), self.dateParser)
start = get_time_utc(elem.get('start'), self.dateParser) + self.offset
stop = get_time_utc(elem.get('stop'), self.dateParser) + self.offset
title = get_xml_string(elem, 'title')
# try/except for EPG XML files with program entries containing <sub-title ... />
try:
Expand Down

1 comment on commit 00a8391

@Huevos
Copy link
Member

@Huevos Huevos commented on 00a8391 Nov 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit requested here: #84

Please sign in to comment.