15 __title__ =
"cinemarv_api - XPath and XSLT functions for the CinemaRV.com grabber"
16 __author__=
"R.D. Vaughan"
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
29 __xpathClassList__ = [
'xpathFunctions', ]
33 __xsltExtentionList__ = []
35 import os, sys, re, time, datetime, shutil, urllib.request, urllib.parse, urllib.error, string
36 from copy
import deepcopy
40 """Wraps a stream with an encoder"""
49 """Wraps the output stream, encoding Unicode strings with the specified encoding"""
50 if isinstance(obj, str):
53 self.
out.buffer.write(obj)
58 """Delegate everything but write to the stream"""
59 return getattr(self.
out, attr)
61 if isinstance(sys.stdout, io.TextIOWrapper):
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)
77 for digit
in etree.LIBXML_VERSION:
78 version+=str(digit)+
'.'
79 version = version[:-1]
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).
89 """Functions specific extending XPath
92 self.
functList = [
'cinemarvLinkGeneration',
'cinemarvIsCustomHTML',
'cinemarvCheckIfDBItem', ]
104 '''Generate a link for the CinemaRV.com site. A read of the item's web page is required to
105 extract the flash video id.
106 Call example: 'mnvXpath:cinemarvLinkGeneration(string(link))'
112 if self.
persistence[
'cinemarvLinkGeneration']
is not None:
113 returnValue = self.
persistence[
'cinemarvLinkGeneration']
117 self.
persistence[
'cinemarvLinkGenerationVideoID'] = etree.XPath(
'//object[@id="flashObj"]//param[@name="flashVars"]/@value', namespaces=common.namespaces)
118 self.
persistence[
'cinemarvLinkGenerationParser'] = etree.HTMLParser()
121 webPageElement = etree.parse(webURL, self.
persistence[
'cinemarvLinkGenerationParser'])
122 except Exception
as errmsg:
123 sys.stderr.write(
'!Warning: The web page URL(%s) could not be read, error(%s)\n' % (webURL, errmsg))
125 if webPageElement
is None:
126 self.
persistence[
'cinemarvLinkGeneration'] = webURL
129 tmpVideoID = self.
persistence[
'cinemarvLinkGenerationVideoID'](webPageElement)
130 if not len(tmpVideoID):
131 self.
persistence[
'cinemarvLinkGeneration'] = webURL
133 index = tmpVideoID[0].
find(
'&')
135 self.
persistence[
'cinemarvLinkGeneration'] = webURL
137 videocode = tmpVideoID[0][:index].replace(
'videoId=',
'')
138 self.
persistence[
'cinemarvLinkGeneration'] = common.linkWebPage(
'dummycontext',
'cinemarv')+videocode
143 '''Check if the link is for a custom HTML
144 Example call: mnvXpath:cinemarvIsCustomHTML(('dummy'))
145 return True if the link does not starts with "http://"
146 return False if the link starts with "http://"
148 if self.
persistence[
'cinemarvLinkGeneration']
is None:
151 if self.
persistence[
'cinemarvLinkGeneration'].startswith(
'http://'):
158 '''Use a unique key value pairing to find out if the 'internetcontentarticles' table already
159 has a matching item. This is done to save accessing the Internet when not required.
160 Call example: 'mnvXpath:cinemarvCheckIfDBItem(.)'
161 return True if a match was found
162 return False if a match was not found
164 return common.checkIfDBItem(
'dummy', {
'feedtitle':
'Movie Trailers',
'title': arg[0].replace(
'Trailer',
'').strip(),
'author': arg[1],
'description': arg[2]})