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__='''
18This python script is intended to perform a variety of utility functions
19for the conversion of data to the MNV standard RSS output format.
20See this link for the specifications:
21http://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
35import os, sys, re, time, datetime, shutil, urllib.request, urllib.parse, urllib.error, string
36from copy import deepcopy
37import io
38
39
40class 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
62if isinstance(sys.stdout, io.TextIOWrapper):
63 sys.stdout = OutStreamEncoder(sys.stdout, 'utf8')
64 sys.stderr = OutStreamEncoder(sys.stderr, 'utf8')
65
66try:
67 from io import StringIO
68 from lxml import etree
69except 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
74class xpathFunctions(object):
75 """Functions specific extending XPath
76 """
77 def __init__(self):
78 self.functList = ['nasaTitleEp', ]
79 # Show 1
80 self.regexPattern = re.compile('''Show\\ (?P<seasno>[0-9]+).*$''', re.UNICODE)
81 # end __init__()
82
83
88
89 def nasaTitleEp(self, context, *arg):
90 '''Parse the guid element and extract an episode number
91 Call example: 'mnvXpath:nasaTitleEp(string(title))'
92 return the a massaged title element and an episode element
93 '''
94 stripArray = ['HST SM4:', 'NASA 360:', 'NASA 360', 'NASA EDGE:', 'NASA EDGE', 'NE Live@', 'NE@', 'NASA Mission Update:', "NASA TV's This Week @NASA," ]
95 title = arg[0]
96 for stripText in stripArray:
97 title = title.replace(stripText, '')
98 title = title.strip()
99 episodeNumber = None
100 if title.startswith('Show'):
101 match = self.regexPattern.match(title)
102 if match:
103 episodeNumber = match.groups()
104 episodeNumber = int(episodeNumber[0])
105 title = title[title.find(':')+1:].strip()
106
107 mythtvNamespace = "http://www.mythtv.org/wiki/MythNetvision_Grabber_Script_Format"
108 mythtv = "{%s}" % mythtvNamespace
109 NSMAP = {'mythtv' : mythtvNamespace}
110 elementTmp = etree.Element(mythtv + "mythtv", nsmap=NSMAP)
111 if not episodeNumber is None:
112 etree.SubElement(elementTmp, "title").text = "EP%02d: %s" % (episodeNumber, title)
113 etree.SubElement(elementTmp, mythtv + "episode").text = "%s" % episodeNumber
114 else:
115 etree.SubElement(elementTmp, "title").text = title
116 return elementTmp
117 # end nasaTitleEp()
118
119
124
125
130
131
def __init__(self, outstream, encoding=None)
Definition: nasa_api.py:42
def nasaTitleEp(self, context, *arg)
Start of XPath extension functions.
Definition: nasa_api.py:89