MythTV  master
chrisPirillo_api.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: UTF-8 -*-
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, string
35 from copy import deepcopy
36 
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, unicode):
50  try:
51  self.out.write(obj.encode(self.encoding))
52  except IOError:
53  pass
54  else:
55  try:
56  self.out.write(obj)
57  except IOError:
58  pass
59 
60  def __getattr__(self, attr):
61  """Delegate everything but write to the stream"""
62  return getattr(self.out, attr)
63 sys.stdout = OutStreamEncoder(sys.stdout, 'utf8')
64 sys.stderr = OutStreamEncoder(sys.stderr, 'utf8')
65 
66 try:
67  from StringIO import StringIO
68  from lxml import etree
69 except Exception, e:
70  sys.stderr.write(u'\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(u'''
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 = ['chrisPirilloLinkGeneration', ]
94  self.TextTail = etree.XPath("string()")
95  self.namespaces = {
96  'content': u"http://purl.org/rss/1.0/modules/content/",
97  'wfw': u"http://wellformedweb.org/CommentAPI/",
98  'dc': u"http://purl.org/dc/elements/1.1/",
99  'atom': u"http://www.w3.org/2005/Atom",
100  'sy': u"http://purl.org/rss/1.0/modules/syndication/",
101  'slash': u"http://purl.org/rss/1.0/modules/slash/",
102  'itunes': u"http://www.itunes.com/dtds/podcast-1.0.dtd",
103  'media': u"http://search.yahoo.com/mrss/",
104  'feedburner': u"http://rssnamespace.org/feedburner/ext/1.0",
105  'atom10': u"http://www.w3.org/2005/Atom",
106  }
107  self.youtubeFilter = etree.XPath('.//embed/@src', namespaces=self.namespaces)
108  # end __init__()
109 
110 
115 
116  def chrisPirilloLinkGeneration(self, context, *arg):
117  '''Generate a link for the video.
118  Call example:
119  'mnvXpath:chrisPirilloLinkGeneration(normalize-space(link), normalize-space(description))'
120  return the url link
121  '''
122  tmpHtml = common.getHtmlData(*arg)
123  fullScreenLink = self.youtubeFilter(tmpHtml)
124  if len(fullScreenLink):
125  link = u'%s%s' % (fullScreenLink[0], u'&amp;autoplay=1')
126  return link
127  return arg[0]
128  # end chrisPirilloLinkGeneration()
129 
130 
135 
136 
141 
142 
def chrisPirilloLinkGeneration(self, context, *arg)
Start of XPath extension functions.