15 __title__ =
"nasa_api - XPath and XSLT functions for the NASA 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
41 """Wraps a stream with an encoder"""
50 """Wraps the output stream, encoding Unicode strings with the specified encoding"""
51 if isinstance(obj, str):
54 self.
out.buffer.write(obj)
59 """Delegate everything but write to the stream"""
60 return getattr(self.
out, attr)
62 if isinstance(sys.stdout, io.TextIOWrapper):
67 from io
import StringIO
68 from lxml
import etree
69 except Exception
as e:
70 sys.stderr.write(
'\n! Error - Importing the "lxml" and "StringIO" python libraries failed on error(%s)\n' % e)
78 for digit
in etree.LIBXML_VERSION:
79 version+=str(digit)+
'.'
80 version = version[:-1]
83 ! Error - The installed version of the "lxml" python library "libxml" version is too old.
84 At least "libxml" version 2.7.2 must be installed. Your version is (%s).
90 """Functions specific extending XPath
95 self.
regexPattern = re.compile(
'''Show\\ (?P<seasno>[0-9]+).*$''', re.UNICODE)
105 '''Parse the guid element and extract an episode number
106 Call example: 'mnvXpath:nasaTitleEp(string(title))'
107 return the a massaged title element and an episode element
109 stripArray = [
'HST SM4:',
'NASA 360:',
'NASA 360',
'NASA EDGE:',
'NASA EDGE',
'NE Live@',
'NE@',
'NASA Mission Update:',
"NASA TV's This Week @NASA," ]
111 for stripText
in stripArray:
112 title = title.replace(stripText,
'')
113 title = title.strip()
115 if title.startswith(
'Show'):
118 episodeNumber = match.groups()
119 episodeNumber = int(episodeNumber[0])
120 title = title[title.find(
':')+1:].strip()
122 mythtvNamespace =
"http://www.mythtv.org/wiki/MythNetvision_Grabber_Script_Format"
123 mythtv =
"{%s}" % mythtvNamespace
124 NSMAP = {
'mythtv' : mythtvNamespace}
125 elementTmp = etree.Element(mythtv +
"mythtv", nsmap=NSMAP)
126 if not episodeNumber
is None:
127 etree.SubElement(elementTmp,
"title").text =
"EP%02d: %s" % (episodeNumber, title)
128 etree.SubElement(elementTmp, mythtv +
"episode").text =
"%s" % episodeNumber
130 etree.SubElement(elementTmp,
"title").text = title