00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 __title__ ="chrisPirillo_api - XPath and XSLT functions for the chris.pirillo.com RSS/HTML"
00016 __author__="R.D. Vaughan"
00017 __purpose__='''
00018 This python script is intended to perform a variety of utility functions
00019 for the conversion of data to the MNV standard RSS output format.
00020 See this link for the specifications:
00021 http://www.mythtv.org/wiki/MythNetvision_Grabber_Script_Format
00022 '''
00023
00024 __version__="v0.1.0"
00025
00026
00027
00028 __xpathClassList__ = ['xpathFunctions', ]
00029
00030
00031
00032 __xsltExtentionList__ = []
00033
00034 import os, sys, re, time, datetime, shutil, urllib, string
00035 from copy import deepcopy
00036
00037
00038 class OutStreamEncoder(object):
00039 """Wraps a stream with an encoder"""
00040 def __init__(self, outstream, encoding=None):
00041 self.out = outstream
00042 if not encoding:
00043 self.encoding = sys.getfilesystemencoding()
00044 else:
00045 self.encoding = encoding
00046
00047 def write(self, obj):
00048 """Wraps the output stream, encoding Unicode strings with the specified encoding"""
00049 if isinstance(obj, unicode):
00050 try:
00051 self.out.write(obj.encode(self.encoding))
00052 except IOError:
00053 pass
00054 else:
00055 try:
00056 self.out.write(obj)
00057 except IOError:
00058 pass
00059
00060 def __getattr__(self, attr):
00061 """Delegate everything but write to the stream"""
00062 return getattr(self.out, attr)
00063 sys.stdout = OutStreamEncoder(sys.stdout, 'utf8')
00064 sys.stderr = OutStreamEncoder(sys.stderr, 'utf8')
00065
00066 try:
00067 from StringIO import StringIO
00068 from lxml import etree
00069 except Exception, e:
00070 sys.stderr.write(u'\n! Error - Importing the "lxml" and "StringIO" python libraries failed on error(%s)\n' % e)
00071 sys.exit(1)
00072
00073
00074
00075
00076
00077 version = ''
00078 for digit in etree.LIBXML_VERSION:
00079 version+=str(digit)+'.'
00080 version = version[:-1]
00081 if version < '2.7.2':
00082 sys.stderr.write(u'''
00083 ! Error - The installed version of the "lxml" python library "libxml" version is too old.
00084 At least "libxml" version 2.7.2 must be installed. Your version is (%s).
00085 ''' % version)
00086 sys.exit(1)
00087
00088
00089 class xpathFunctions(object):
00090 """Functions specific extending XPath
00091 """
00092 def __init__(self):
00093 self.functList = ['chrisPirilloLinkGeneration', ]
00094 self.TextTail = etree.XPath("string()")
00095 self.namespaces = {
00096 'content': u"http://purl.org/rss/1.0/modules/content/",
00097 'wfw': u"http://wellformedweb.org/CommentAPI/",
00098 'dc': u"http://purl.org/dc/elements/1.1/",
00099 'atom': u"http://www.w3.org/2005/Atom",
00100 'sy': u"http://purl.org/rss/1.0/modules/syndication/",
00101 'slash': u"http://purl.org/rss/1.0/modules/slash/",
00102 'itunes': u"http://www.itunes.com/dtds/podcast-1.0.dtd",
00103 'media': u"http://search.yahoo.com/mrss/",
00104 'feedburner': u"http://rssnamespace.org/feedburner/ext/1.0",
00105 'atom10': u"http://www.w3.org/2005/Atom",
00106 }
00107 self.youtubeFilter = etree.XPath('.//embed/@src', namespaces=self.namespaces)
00108
00109
00110
00111
00112
00113
00114
00115
00116 def chrisPirilloLinkGeneration(self, context, *arg):
00117 '''Generate a link for the video.
00118 Call example:
00119 'mnvXpath:chrisPirilloLinkGeneration(normalize-space(link), normalize-space(description))'
00120 return the url link
00121 '''
00122 tmpHtml = common.getHtmlData(*arg)
00123 fullScreenLink = self.youtubeFilter(tmpHtml)
00124 if len(fullScreenLink):
00125 link = u'%s%s' % (fullScreenLink[0], u'&autoplay=1')
00126 return link
00127 return arg[0]
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146