MythTV  master
hubbleCast_api.py
Go to the documentation of this file.
1 # -*- coding: UTF-8 -*-
2 
3 # ----------------------
4 # Name: hubbleCast_api - XPath and XSLT functions for the HubbleCast 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__ ="hubbleCast_api - XPath and XSLT functions for the HubbleCast 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 class OutStreamEncoder(object):
40  """Wraps a stream with an encoder"""
41  def __init__(self, outstream, encoding=None):
42  self.out = outstream
43  if not encoding:
44  self.encoding = sys.getfilesystemencoding()
45  else:
46  self.encoding = encoding
47 
48  def write(self, obj):
49  """Wraps the output stream, encoding Unicode strings with the specified encoding"""
50  if isinstance(obj, str):
51  obj = obj.encode(self.encoding)
52  try:
53  self.out.buffer.write(obj)
54  except OSError:
55  pass
56 
57  def __getattr__(self, attr):
58  """Delegate everything but write to the stream"""
59  return getattr(self.out, attr)
60 
61 if isinstance(sys.stdout, io.TextIOWrapper):
62  sys.stdout = OutStreamEncoder(sys.stdout, 'utf8')
63  sys.stderr = OutStreamEncoder(sys.stderr, 'utf8')
64 
65 try:
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)
70  sys.exit(1)
71 
72 # Check that the lxml library is current enough
73 # From the lxml documents it states: (http://codespeak.net/lxml/installation.html)
74 # "If you want to use XPath, do not use libxml2 2.6.27. We recommend libxml2 2.7.2 or later"
75 # Testing was performed with the Ubuntu 9.10 "python-lxml" version "2.1.5-1ubuntu2" repository package
76 version = ''
77 for digit in etree.LIBXML_VERSION:
78  version+=str(digit)+'.'
79 version = version[:-1]
80 if version < '2.7.2':
81  sys.stderr.write('''
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).
84 ''' % version)
85  sys.exit(1)
86 
87 
88 class xpathFunctions(object):
89  """Functions specific extending XPath
90  """
91  def __init__(self):
92  self.functList = ['hubbleCastLinkGeneration', ]
93  self.namespaces = {
94  'atom': "http://www.w3.org/2005/Atom",
95  'atom10': "http://www.w3.org/2005/Atom",
96  'media': "http://search.yahoo.com/mrss/",
97  'itunes':"http://www.itunes.com/dtds/podcast-1.0.dtd",
98  'xhtml': "http://www.w3.org/1999/xhtml",
99  'mythtv': "http://www.mythtv.org/wiki/MythNetvision_Grabber_Script_Format",
100  'feedburner': "http://rssnamespace.org/feedburner/ext/1.0",
101  'amp': "http://www.adobe.com/amp/1.0",
102  }
103  # end __init__()
104 
105 
110 
111  def hubbleCastLinkGeneration(self, context, *arg):
112  '''Generate a link for the video.
113  Call example: 'mnvXpath:hubbleCastLinkGeneration(string(guid))'
114  return the url link
115  '''
116  guidURL = arg[0]
117  index = guidURL.find('.m4v')
118  if index != -1:
119  index2 = guidURL.rfind('/')
120  return '%s%s' % (common.linkWebPage('dummy', 'hubble'), guidURL[index2+1:index])
121  elif guidURL[len(guidURL)-1] == '/':
122  index2 = guidURL[:-1].rfind('/')
123  return '%s%s' % (common.linkWebPage('dummy', 'hubble'), guidURL[index2+1:-1])
124  else:
125  return guidURL
126  # end hubbleCastLinkGeneration()
127 
128 
133 
134 
139 
140 
nv_python_libs.xsltfunctions.hubbleCast_api.xpathFunctions.namespaces
namespaces
Definition: hubbleCast_api.py:93
nv_python_libs.xsltfunctions.hubbleCast_api.OutStreamEncoder.write
def write(self, obj)
Definition: hubbleCast_api.py:48
nv_python_libs.xsltfunctions.hubbleCast_api.xpathFunctions
Definition: hubbleCast_api.py:88
nv_python_libs.xsltfunctions.hubbleCast_api.xpathFunctions.hubbleCastLinkGeneration
def hubbleCastLinkGeneration(self, context, *arg)
Start of XPath extension functions.
Definition: hubbleCast_api.py:111
nv_python_libs.xsltfunctions.hubbleCast_api.OutStreamEncoder.__init__
def __init__(self, outstream, encoding=None)
Definition: hubbleCast_api.py:41
nv_python_libs.xsltfunctions.hubbleCast_api.OutStreamEncoder.out
out
Definition: hubbleCast_api.py:42
nv_python_libs.xsltfunctions.hubbleCast_api.OutStreamEncoder.__getattr__
def __getattr__(self, attr)
Definition: hubbleCast_api.py:57
nv_python_libs.xsltfunctions.hubbleCast_api.xpathFunctions.__init__
def __init__(self)
Definition: hubbleCast_api.py:91
nv_python_libs.xsltfunctions.hubbleCast_api.xpathFunctions.functList
functList
Definition: hubbleCast_api.py:92
nv_python_libs.xsltfunctions.hubbleCast_api.OutStreamEncoder.encoding
encoding
Definition: hubbleCast_api.py:44
nv_python_libs.xsltfunctions.hubbleCast_api.OutStreamEncoder
Definition: hubbleCast_api.py:39