15 __title__ =
"mevio_api - XPath and XSLT functions for the www.mevio.com 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
31 __xpathClassList__ = [
'xpathFunctions', ]
35 __xsltExtentionList__ = []
37 import os, sys, re, time, datetime, shutil, urllib.request, urllib.parse, urllib.error, string
38 from copy
import deepcopy
42 """Wraps a stream with an encoder"""
51 """Wraps the output stream, encoding Unicode strings with the specified encoding"""
52 if isinstance(obj, str):
55 self.
out.buffer.write(obj)
60 """Delegate everything but write to the stream"""
61 return getattr(self.
out, attr)
63 if isinstance(sys.stdout, io.TextIOWrapper):
68 from io
import StringIO
69 from lxml
import etree
70 except Exception
as e:
71 sys.stderr.write(
'\n! Error - Importing the "lxml" and "StringIO" python libraries failed on error(%s)\n' % e)
79 for digit
in etree.LIBXML_VERSION:
80 version+=str(digit)+
'.'
81 version = version[:-1]
84 ! Error - The installed version of the "lxml" python library "libxml" version is too old.
85 At least "libxml" version 2.7.2 must be installed. Your version is (%s).
91 """Functions specific extending XPath
94 self.
functList = [
'mevioLinkGeneration',
'mevioTitle',
'mevioEpisode',
'mevioCheckIfDBItem', ]
97 re.compile(
'''^.+?Episode\\ (?P<episodeno>[0-9]+).*$''', re.UNICODE),
99 re.compile(
'''^.+?(?P<episodeno>[0-9]+)\\:.*$''', re.UNICODE),
102 'atom10':
"http://www.w3.org/2005/Atom",
103 'media':
"http://search.yahoo.com/mrss/",
104 'itunes':
"http://www.itunes.com/dtds/podcast-1.0.dtd",
105 'xhtml':
"http://www.w3.org/1999/xhtml",
106 'feedburner':
"http://rssnamespace.org/feedburner/ext/1.0",
107 'mythtv':
"http://www.mythtv.org/wiki/MythNetvision_Grabber_Script_Format",
108 'dc':
"http://purl.org/dc/elements/1.1/",
109 'fb':
"http://www.facebook.com/2008/fbml/",
112 [etree.XPath(
".//embed/@flashvars", namespaces=self.
namespaces), re.compile(
'''^.+?MediaId=(?P<videocode>[0-9]+).*$''', re.UNICODE)],
113 [etree.XPath(
".//div[@class='player_wrapper']/a/@href", namespaces=self.
namespaces), re.compile(
'''^.+?\\'(?P<videocode>[0-9]+)\\'\\)\\;.*$''', re.UNICODE)]
124 '''Generate a link for the video.
125 Call example: 'mnvXpath:mevioLinkGeneration(string(link))'
130 tmpHTML = etree.parse(webURL, etree.HTMLParser())
131 except Exception
as errmsg:
132 sys.stderr.write(
"Error reading url(%s) error(%s)\n" % (webURL, errmsg))
141 videocode = match.groups()
142 return 'file://%s/nv_python_libs/configs/HTML/mevio.html?videocode=%s' % (common.baseProcessingDir, videocode[0])
148 '''Parse the title string extract only the title text removing the redundant show name
149 Call example: 'mnvXpath:mevioTitle(./title/text())'
150 return the title text
154 epText =
'Ep %s: ' % epText
157 seperatorStrs = [[
' | ',
'before'], [
': ',
'after'], [
' - ',
'before']]
158 for sepStr
in seperatorStrs:
159 if sepStr[1] ==
'after':
160 index = arg[0].
find(sepStr[0])
162 index = arg[0].rfind(sepStr[0])
164 if sepStr[1] ==
'after':
165 return '%s%s' % (epText, arg[0][index+len(sepStr[0]):].strip())
167 return '%s%s' % (epText, arg[0][:index].strip())
172 return arg[0].strip()
176 '''Parse the title string and extract an episode number
177 Call example: 'mnvXpath:mevioEpisode(./title/text())'
178 return an episode element
184 episodeNumber = match.groups()
186 return etree.XML(
'<episode>%s</episode>' % episodeNumber)
190 '''Use a unique key value pairing to find out if the 'internetcontentarticles' table already
191 has a matching item. This is done to save accessing the Internet when not required.
192 Call example: 'mnvXpath:mevioCheckIfDBItem(title, description)'
193 return True if a match was found
194 return False if a match was not found
196 return common.checkIfDBItem(
'dummy', {
'feedtitle':
'Technology',
'title': arg[0],
'description': arg[1]})