MythTV  master
chrisPirillo_api.py
Go to the documentation of this file.
1 # -*- coding: UTF-8 -*-
2 
3 # ----------------------
4 # Name: chrisPirillo_api - XPath and XSLT functions for the chris.pirillo.com 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__ ="chrisPirillo_api - XPath and XSLT functions for the chris.pirillo.com 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 # Specify the class names that have XPath extention functions
28 __xpathClassList__ = ['xpathFunctions', ]
29 
30 # Specify the XSLT extention class names. Each class is a stand lone extention function
31 #__xsltExtentionList__ = ['xsltExtExample', ]
32 __xsltExtentionList__ = []
33 
34 import os, sys, re, time, datetime, shutil, urllib.request, urllib.parse, urllib.error, string
35 from copy import deepcopy
36 import io
37 
38 class OutStreamEncoder(object):
39  """Wraps a stream with an encoder"""
40  def __init__(self, outstream, encoding=None):
41  self.out = outstream
42  if not encoding:
43  self.encoding = sys.getfilesystemencoding()
44  else:
45  self.encoding = encoding
46 
47  def write(self, obj):
48  """Wraps the output stream, encoding Unicode strings with the specified encoding"""
49  if isinstance(obj, str):
50  obj = obj.encode(self.encoding)
51  try:
52  self.out.buffer.write(obj)
53  except OSError:
54  pass
55 
56  def __getattr__(self, attr):
57  """Delegate everything but write to the stream"""
58  return getattr(self.out, attr)
59 
60 if isinstance(sys.stdout, io.TextIOWrapper):
61  sys.stdout = OutStreamEncoder(sys.stdout, 'utf8')
62  sys.stderr = OutStreamEncoder(sys.stderr, 'utf8')
63 
64 try:
65  from io import StringIO
66  from lxml import etree
67 except Exception as e:
68  sys.stderr.write('\n! Error - Importing the "lxml" and "StringIO" python libraries failed on error(%s)\n' % e)
69  sys.exit(1)
70 
71 # Check that the lxml library is current enough
72 # From the lxml documents it states: (http://codespeak.net/lxml/installation.html)
73 # "If you want to use XPath, do not use libxml2 2.6.27. We recommend libxml2 2.7.2 or later"
74 # Testing was performed with the Ubuntu 9.10 "python-lxml" version "2.1.5-1ubuntu2" repository package
75 version = ''
76 for digit in etree.LIBXML_VERSION:
77  version+=str(digit)+'.'
78 version = version[:-1]
79 if version < '2.7.2':
80  sys.stderr.write('''
81 ! Error - The installed version of the "lxml" python library "libxml" version is too old.
82  At least "libxml" version 2.7.2 must be installed. Your version is (%s).
83 ''' % version)
84  sys.exit(1)
85 
86 
87 class xpathFunctions(object):
88  """Functions specific extending XPath
89  """
90  def __init__(self):
91  self.functList = ['chrisPirilloLinkGeneration', ]
92  self.TextTail = etree.XPath("string()")
93  self.namespaces = {
94  'content': "http://purl.org/rss/1.0/modules/content/",
95  'wfw': "http://wellformedweb.org/CommentAPI/",
96  'dc': "http://purl.org/dc/elements/1.1/",
97  'atom': "http://www.w3.org/2005/Atom",
98  'sy': "http://purl.org/rss/1.0/modules/syndication/",
99  'slash': "http://purl.org/rss/1.0/modules/slash/",
100  'itunes': "http://www.itunes.com/dtds/podcast-1.0.dtd",
101  'media': "http://search.yahoo.com/mrss/",
102  'feedburner': "http://rssnamespace.org/feedburner/ext/1.0",
103  'atom10': "http://www.w3.org/2005/Atom",
104  }
105  self.youtubeFilter = etree.XPath('.//embed/@src', namespaces=self.namespaces)
106  # end __init__()
107 
108 
113 
114  def chrisPirilloLinkGeneration(self, context, *arg):
115  '''Generate a link for the video.
116  Call example:
117  'mnvXpath:chrisPirilloLinkGeneration(normalize-space(link), normalize-space(description))'
118  return the url link
119  '''
120  tmpHtml = common.getHtmlData(*arg)
121  fullScreenLink = self.youtubeFilter(tmpHtml)
122  if len(fullScreenLink):
123  link = '%s%s' % (fullScreenLink[0], '&amp;autoplay=1')
124  return link
125  return arg[0]
126  # end chrisPirilloLinkGeneration()
127 
128 
133 
134 
139 
140 
nv_python_libs.xsltfunctions.chrisPirillo_api.xpathFunctions.youtubeFilter
youtubeFilter
Definition: chrisPirillo_api.py:105
nv_python_libs.xsltfunctions.chrisPirillo_api.OutStreamEncoder.encoding
encoding
Definition: chrisPirillo_api.py:43
nv_python_libs.xsltfunctions.chrisPirillo_api.xpathFunctions.chrisPirilloLinkGeneration
def chrisPirilloLinkGeneration(self, context, *arg)
Start of XPath extension functions.
Definition: chrisPirillo_api.py:114
nv_python_libs.xsltfunctions.chrisPirillo_api.OutStreamEncoder.out
out
Definition: chrisPirillo_api.py:41
nv_python_libs.xsltfunctions.chrisPirillo_api.xpathFunctions.TextTail
TextTail
Definition: chrisPirillo_api.py:92
nv_python_libs.xsltfunctions.chrisPirillo_api.OutStreamEncoder.__getattr__
def __getattr__(self, attr)
Definition: chrisPirillo_api.py:56
nv_python_libs.xsltfunctions.chrisPirillo_api.xpathFunctions.namespaces
namespaces
Definition: chrisPirillo_api.py:93
nv_python_libs.xsltfunctions.chrisPirillo_api.OutStreamEncoder.__init__
def __init__(self, outstream, encoding=None)
Definition: chrisPirillo_api.py:40
nv_python_libs.xsltfunctions.chrisPirillo_api.OutStreamEncoder
Definition: chrisPirillo_api.py:38
nv_python_libs.xsltfunctions.chrisPirillo_api.xpathFunctions.functList
functList
Definition: chrisPirillo_api.py:91
nv_python_libs.xsltfunctions.chrisPirillo_api.xpathFunctions.__init__
def __init__(self)
Definition: chrisPirillo_api.py:90
nv_python_libs.xsltfunctions.chrisPirillo_api.xpathFunctions
Definition: chrisPirillo_api.py:87
nv_python_libs.xsltfunctions.chrisPirillo_api.OutStreamEncoder.write
def write(self, obj)
Definition: chrisPirillo_api.py:47