15 __title__ =
"skyAtNight_api - XPath and XSLT functions for the BBC Sky at Night magazine RSS/HTML"
16 __author__=
"R.D. Vaughan"
18 This python script is intended to perform a variety of utility functions
19 for the conversion of data to the MNV standard RSS output format.
20 See this link for the specifications:
21 http://www.mythtv.org/wiki/MythNetvision_Grabber_Script_Format
29 __xpathClassList__ = [
'xpathFunctions', ]
33 __xsltExtentionList__ = []
35 import os, sys, re, time, datetime, shutil, urllib.request, urllib.parse, urllib.error, string
36 from copy
import deepcopy
40 """Wraps a stream with an encoder"""
49 """Wraps the output stream, encoding Unicode strings with the specified encoding"""
50 if isinstance(obj, str):
53 self.
out.buffer.write(obj)
58 """Delegate everything but write to the stream"""
59 return getattr(self.
out, attr)
61 if isinstance(sys.stdout, io.TextIOWrapper):
66 from io
import StringIO
67 from lxml
import etree
68 except Exception
as e:
69 sys.stderr.write(
'\n! Error - Importing the "lxml" and "StringIO" python libraries failed on error(%s)\n' % e)
77 for digit
in etree.LIBXML_VERSION:
78 version+=str(digit)+
'.'
79 version = version[:-1]
82 ! Error - The installed version of the "lxml" python library "libxml" version is too old.
83 At least "libxml" version 2.7.2 must be installed. Your version is (%s).
89 """Functions specific extending XPath
102 '''Parse the guid element and extract an episode number
103 Call example: 'mnvXpath:skyAtNightTitleEp(string(guid))'
104 return the a massaged title element and an episode element in an array
106 searchText =
'BBC_SAN'
108 episodeNumber = title.replace(searchText,
'').strip()
110 episodeNumber = int(episodeNumber)
114 mythtvNamespace =
"http://www.mythtv.org/wiki/MythNetvision_Grabber_Script_Format"
115 mythtv =
"{%s}" % mythtvNamespace
116 NSMAP = {
'mythtv' : mythtvNamespace}
117 elementTmp = etree.Element(mythtv +
"mythtv", nsmap=NSMAP)
118 if not episodeNumber
is None:
119 etree.SubElement(elementTmp,
"title").text =
"EP%02d" % episodeNumber
120 etree.SubElement(elementTmp, mythtv +
"episode").text =
"%s" % episodeNumber
122 etree.SubElement(elementTmp,
"title").text = title