15 __title__ =
"pbsXSL_api - XPath and XSLT functions for the PBS RSS/HTML"
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 = [
'pbsTitleSeriesEpisodeLink',
'pbsDuration',
'pbsDownloadlink']
95 re.compile(
'''^.+?Season\\ (?P<seasno>[0-9]+)\\:\\ Episode\\ (?P<epno>[0-9]+).*$''', re.UNICODE),
97 re.compile(
'''^.+?Season\\ (?P<seasno>[0-9]+)\\,\\ Episode\\ (?P<epno>[0-9]+).*$''', re.UNICODE),
99 re.compile(
'''^.+?Episode\\ (?P<epno>[0-9]+).*$''', re.UNICODE),
102 'media':
"http://search.yahoo.com/mrss/",
103 'xhtml':
"http://www.w3.org/1999/xhtml",
104 'mythtv':
"http://www.mythtv.org/wiki/MythNetvision_Grabber_Script_Format",
105 'mediaad':
"http://PBS/dtd/mediaad/1.0",
117 '''Generate a link for the PBS site.
118 Call example: 'mnvXpath:pbsTitleSeriesEpisodeLink(normalize-space(./title),normalize-space(./link))/*'
119 return the title, link and season and episode number elements if any were in the title
125 tmpVideoCode = tmpLink.replace(
'http://video.pbs.org/video/',
'')[:-1]
126 self.
persistence[tmpLink] = common.linkWebPage(
'dummy',
'pbs')
135 (seasonNumber, episodeNumber) = match.groups()
138 episodeNumber = match.groups()[0]
142 index = tmpTitle.rfind(
'|')
144 tmpTitle = tmpTitle[index+1:].strip()
145 if seasonNumber
and episodeNumber:
146 tmpTitle =
'S%02dE%02d: %s' % (int(seasonNumber), int(episodeNumber), tmpTitle)
148 index = tmpTitle.find(
':')
150 tmpTitle = tmpTitle[index+1:].strip()
151 tmpTitle =
'Ep%02d: %s' % (int(episodeNumber), tmpTitle)
152 self.
persistence[tmpLink] = common.linkWebPage(
'dummy',
'pbs').replace(
'TITLE', urllib.parse.quote(tmpTitle)).replace(
'VIDEOCODE', tmpVideoCode)
155 elementTmp = etree.XML(
'<xml></xml>')
156 etree.SubElement(elementTmp,
"title").text = tmpTitle
158 etree.SubElement(elementTmp,
"season").text =
"%s" % int(seasonNumber)
160 etree.SubElement(elementTmp,
"episode").text =
"%s" % int(episodeNumber)
161 etree.SubElement(elementTmp,
"link").text = self.
persistence[tmpLink]
166 '''Return the video duration in seconds
167 Call example: 'mnvXpath:pbsDuration(normalize-string(./dd/p[@class="info"]/span[@class="time"]))'
168 return the video duration
176 '''Return a previously created download link
177 Call example: 'mnvXpath:pbsDownloadlink(normalize-space(./link))'