MythTV  master
nasa_api.py
Go to the documentation of this file.
1 # -*- coding: UTF-8 -*-
2 
3 # ----------------------
4 # Name: nasa_api - XPath and XSLT functions for the NASA RSS/HTML items
5 # Python Script
6 # Author: R.D. Vaughan
7 # Purpose: This python script is intended to perform a variety of utility functions
8 # for the conversion of data to the MNV standard RSS output format.
9 # See this link for the specifications:
10 # http://www.mythtv.org/wiki/MythNetvision_Grabber_Script_Format
11 #
12 # License:Creative Commons GNU GPL v2
13 # (http://creativecommons.org/licenses/GPL/2.0/)
14 #-------------------------------------
15 __title__ ="nasa_api - XPath and XSLT functions for the NASA RSS/HTML"
16 __author__="R.D. Vaughan"
17 __purpose__='''
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
22 '''
23 
24 __version__="v0.1.0"
25 # 0.1.0 Initial development
26 
27 
28 # Specify the class names that have XPath extention functions
29 __xpathClassList__ = ['xpathFunctions', ]
30 
31 # Specify the XSLT extention class names. Each class is a stand lone extention function
32 #__xsltExtentionList__ = ['xsltExtExample', ]
33 __xsltExtentionList__ = []
34 
35 import os, sys, re, time, datetime, shutil, urllib.request, urllib.parse, urllib.error, string
36 from copy import deepcopy
37 import io
38 
39 
40 class OutStreamEncoder(object):
41  """Wraps a stream with an encoder"""
42  def __init__(self, outstream, encoding=None):
43  self.out = outstream
44  if not encoding:
45  self.encoding = sys.getfilesystemencoding()
46  else:
47  self.encoding = encoding
48 
49  def write(self, obj):
50  """Wraps the output stream, encoding Unicode strings with the specified encoding"""
51  if isinstance(obj, str):
52  obj = obj.encode(self.encoding)
53  try:
54  self.out.buffer.write(obj)
55  except OSError:
56  pass
57 
58  def __getattr__(self, attr):
59  """Delegate everything but write to the stream"""
60  return getattr(self.out, attr)
61 
62 if isinstance(sys.stdout, io.TextIOWrapper):
63  sys.stdout = OutStreamEncoder(sys.stdout, 'utf8')
64  sys.stderr = OutStreamEncoder(sys.stderr, 'utf8')
65 
66 try:
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)
71  sys.exit(1)
72 
73 # Check that the lxml library is current enough
74 # From the lxml documents it states: (http://codespeak.net/lxml/installation.html)
75 # "If you want to use XPath, do not use libxml2 2.6.27. We recommend libxml2 2.7.2 or later"
76 # Testing was performed with the Ubuntu 9.10 "python-lxml" version "2.1.5-1ubuntu2" repository package
77 version = ''
78 for digit in etree.LIBXML_VERSION:
79  version+=str(digit)+'.'
80 version = version[:-1]
81 if version < '2.7.2':
82  sys.stderr.write('''
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).
85 ''' % version)
86  sys.exit(1)
87 
88 
89 class xpathFunctions(object):
90  """Functions specific extending XPath
91  """
92  def __init__(self):
93  self.functList = ['nasaTitleEp', ]
94  # Show 1
95  self.regexPattern = re.compile('''Show\\ (?P<seasno>[0-9]+).*$''', re.UNICODE)
96  # end __init__()
97 
98 
103 
104  def nasaTitleEp(self, context, *arg):
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
108  '''
109  stripArray = ['HST SM4:', 'NASA 360:', 'NASA 360', 'NASA EDGE:', 'NASA EDGE', 'NE Live@', 'NE@', 'NASA Mission Update:', "NASA TV's This Week @NASA," ]
110  title = arg[0]
111  for stripText in stripArray:
112  title = title.replace(stripText, '')
113  title = title.strip()
114  episodeNumber = None
115  if title.startswith('Show'):
116  match = self.regexPattern.match(title)
117  if match:
118  episodeNumber = match.groups()
119  episodeNumber = int(episodeNumber[0])
120  title = title[title.find(':')+1:].strip()
121 
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
129  else:
130  etree.SubElement(elementTmp, "title").text = title
131  return elementTmp
132  # end nasaTitleEp()
133 
134 
139 
140 
145 
146 
nv_python_libs.xsltfunctions.nasa_api.OutStreamEncoder.__init__
def __init__(self, outstream, encoding=None)
Definition: nasa_api.py:42
nv_python_libs.xsltfunctions.nasa_api.OutStreamEncoder.write
def write(self, obj)
Definition: nasa_api.py:49
nv_python_libs.xsltfunctions.nasa_api.xpathFunctions.nasaTitleEp
def nasaTitleEp(self, context, *arg)
Start of XPath extension functions.
Definition: nasa_api.py:104
nv_python_libs.xsltfunctions.nasa_api.OutStreamEncoder.encoding
encoding
Definition: nasa_api.py:45
nv_python_libs.xsltfunctions.nasa_api.OutStreamEncoder.out
out
Definition: nasa_api.py:43
nv_python_libs.xsltfunctions.nasa_api.xpathFunctions
Definition: nasa_api.py:89
nv_python_libs.xsltfunctions.nasa_api.OutStreamEncoder
Definition: nasa_api.py:40
nv_python_libs.xsltfunctions.nasa_api.OutStreamEncoder.__getattr__
def __getattr__(self, attr)
Definition: nasa_api.py:58
nv_python_libs.xsltfunctions.nasa_api.xpathFunctions.__init__
def __init__(self)
Definition: nasa_api.py:92
nv_python_libs.xsltfunctions.nasa_api.xpathFunctions.functList
functList
Definition: nasa_api.py:93
nv_python_libs.xsltfunctions.nasa_api.xpathFunctions.regexPattern
regexPattern
Definition: nasa_api.py:95