00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 __title__ ="cinemarv_api - XPath and XSLT functions for the CinemaRV.com grabber"
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
00029 __xpathClassList__ = ['xpathFunctions', ]
00030
00031
00032
00033 __xsltExtentionList__ = []
00034
00035 import os, sys, re, time, datetime, shutil, urllib, string
00036 from copy import deepcopy
00037
00038
00039 class OutStreamEncoder(object):
00040 """Wraps a stream with an encoder"""
00041 def __init__(self, outstream, encoding=None):
00042 self.out = outstream
00043 if not encoding:
00044 self.encoding = sys.getfilesystemencoding()
00045 else:
00046 self.encoding = encoding
00047
00048 def write(self, obj):
00049 """Wraps the output stream, encoding Unicode strings with the specified encoding"""
00050 if isinstance(obj, unicode):
00051 try:
00052 self.out.write(obj.encode(self.encoding))
00053 except IOError:
00054 pass
00055 else:
00056 try:
00057 self.out.write(obj)
00058 except IOError:
00059 pass
00060
00061 def __getattr__(self, attr):
00062 """Delegate everything but write to the stream"""
00063 return getattr(self.out, attr)
00064 sys.stdout = OutStreamEncoder(sys.stdout, 'utf8')
00065 sys.stderr = OutStreamEncoder(sys.stderr, 'utf8')
00066
00067 try:
00068 from StringIO import StringIO
00069 from lxml import etree
00070 except Exception, e:
00071 sys.stderr.write(u'\n! Error - Importing the "lxml" and "StringIO" python libraries failed on error(%s)\n' % e)
00072 sys.exit(1)
00073
00074
00075
00076
00077
00078 version = ''
00079 for digit in etree.LIBXML_VERSION:
00080 version+=str(digit)+'.'
00081 version = version[:-1]
00082 if version < '2.7.2':
00083 sys.stderr.write(u'''
00084 ! Error - The installed version of the "lxml" python library "libxml" version is too old.
00085 At least "libxml" version 2.7.2 must be installed. Your version is (%s).
00086 ''' % version)
00087 sys.exit(1)
00088
00089
00090 class xpathFunctions(object):
00091 """Functions specific extending XPath
00092 """
00093 def __init__(self):
00094 self.functList = ['cinemarvLinkGeneration', 'cinemarvIsCustomHTML', 'cinemarvCheckIfDBItem', ]
00095 self.TextTail = etree.XPath("string()")
00096 self.persistence = {}
00097
00098
00099
00100
00101
00102
00103
00104
00105 def cinemarvLinkGeneration(self, context, *args):
00106 '''Generate a link for the CinemaRV.com site. A read of the item's web page is required to
00107 extract the flash video id.
00108 Call example: 'mnvXpath:cinemarvLinkGeneration(string(link))'
00109 return the url link
00110 '''
00111 webURL = args[0]
00112
00113 if self.persistence.has_key('cinemarvLinkGeneration'):
00114 if self.persistence['cinemarvLinkGeneration'] != None:
00115 returnValue = self.persistence['cinemarvLinkGeneration']
00116 self.persistence['cinemarvLinkGeneration'] = None
00117 return returnValue
00118 else:
00119 self.persistence['cinemarvLinkGenerationVideoID'] = etree.XPath('//object[@id="flashObj"]//param[@name="flashVars"]/@value', namespaces=common.namespaces)
00120 self.persistence['cinemarvLinkGenerationParser'] = etree.HTMLParser()
00121
00122 try:
00123 webPageElement = etree.parse(webURL, self.persistence['cinemarvLinkGenerationParser'])
00124 except Exception, errmsg:
00125 sys.stderr.write(u'!Warning: The web page URL(%s) could not be read, error(%s)\n' % (webURL, errmsg))
00126 return webURL
00127 if webPageElement == None:
00128 self.persistence['cinemarvLinkGeneration'] = webURL
00129 return webURL
00130
00131 tmpVideoID = self.persistence['cinemarvLinkGenerationVideoID'](webPageElement)
00132 if not len(tmpVideoID):
00133 self.persistence['cinemarvLinkGeneration'] = webURL
00134 return webURL
00135 index = tmpVideoID[0].find('&')
00136 if index == -1:
00137 self.persistence['cinemarvLinkGeneration'] = webURL
00138 return webURL
00139 videocode = tmpVideoID[0][:index].replace(u'videoId=', u'')
00140 self.persistence['cinemarvLinkGeneration'] = common.linkWebPage(u'dummycontext', 'cinemarv')+videocode
00141 return self.persistence['cinemarvLinkGeneration']
00142
00143
00144 def cinemarvIsCustomHTML(self, context, *args):
00145 '''Check if the link is for a custom HTML
00146 Example call: mnvXpath:cinemarvIsCustomHTML(('dummy'))
00147 return True if the link does not starts with "http://"
00148 return False if the link starts with "http://"
00149 '''
00150 if self.persistence['cinemarvLinkGeneration'] == None:
00151 return False
00152
00153 if self.persistence['cinemarvLinkGeneration'].startswith(u'http://'):
00154 return False
00155 else:
00156 return True
00157
00158
00159 def cinemarvCheckIfDBItem(self, context, *arg):
00160 '''Use a unique key value pairing to find out if the 'internetcontentarticles' table already
00161 has a matching item. This is done to save accessing the Internet when not required.
00162 Call example: 'mnvXpath:cinemarvCheckIfDBItem(.)'
00163 return True if a match was found
00164 return False if a match was not found
00165 '''
00166 return common.checkIfDBItem('dummy', {'feedtitle': 'Movie Trailers', 'title': arg[0].replace('Trailer', u'').strip(), 'author': arg[1], 'description': arg[2]})
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185