15__title__ =
"mevio_api - XPath and XSLT functions for the www.mevio.com RSS/HTML"
16__author__=
"R.D. Vaughan"
18This python script is intended to perform a variety of utility functions
19for the conversion of data to the MNV standard RSS output format.
20See this link for the specifications:
21http://www.mythtv.org/wiki/MythNetvision_Grabber_Script_Format
31__xpathClassList__ = [
'xpathFunctions', ]
35__xsltExtentionList__ = []
37import os, sys, re, time, datetime, shutil,
urllib.request, urllib.parse, urllib.error, string
38from 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)
63if isinstance(sys.stdout, io.TextIOWrapper):
68 from io
import StringIO
69 from lxml
import etree
71 sys.stderr.write(
'\n! Error - Importing the "lxml" and "StringIO" python libraries failed on error(%s)\n' % e)
76 """Functions specific extending XPath
79 self.
functList = [
'mevioLinkGeneration',
'mevioTitle',
'mevioEpisode',
'mevioCheckIfDBItem', ]
82 re.compile(
'''^.+?Episode\\ (?P<episodeno>[0-9]+).*$''', re.UNICODE),
84 re.compile(
'''^.+?(?P<episodeno>[0-9]+)\\:.*$''', re.UNICODE),
87 'atom10':
"http://www.w3.org/2005/Atom",
88 'media':
"http://search.yahoo.com/mrss/",
89 'itunes':
"http://www.itunes.com/dtds/podcast-1.0.dtd",
90 'xhtml':
"http://www.w3.org/1999/xhtml",
91 'feedburner':
"http://rssnamespace.org/feedburner/ext/1.0",
92 'mythtv':
"http://www.mythtv.org/wiki/MythNetvision_Grabber_Script_Format",
93 'dc':
"http://purl.org/dc/elements/1.1/",
94 'fb':
"http://www.facebook.com/2008/fbml/",
97 [etree.XPath(
".//embed/@flashvars", namespaces=self.
namespaces), re.compile(
'''^.+?MediaId=(?P<videocode>[0-9]+).*$''', re.UNICODE)],
98 [etree.XPath(
".//div[@class='player_wrapper']/a/@href", namespaces=self.
namespaces), re.compile(
'''^.+?\\'(?P<videocode>[0-9]+)\\'\\)\\;.*$''', re.UNICODE)]
109 '''Generate a link for the video.
110 Call example: 'mnvXpath:mevioLinkGeneration(string(link))'
115 tmpHTML = etree.parse(webURL, etree.HTMLParser())
116 except Exception
as errmsg:
117 sys.stderr.write(
"Error reading url(%s) error(%s)\n" % (webURL, errmsg))
126 videocode = match.groups()
127 return 'file://%s/nv_python_libs/configs/HTML/mevio.html?videocode=%s' % (common.baseProcessingDir, videocode[0])
133 '''Parse the title string extract only the title text removing the redundant show name
134 Call example: 'mnvXpath:mevioTitle(./title/text())'
135 return the title text
139 epText =
'Ep %s: ' % epText
142 seperatorStrs = [[
' | ',
'before'], [
': ',
'after'], [
' - ',
'before']]
143 for sepStr
in seperatorStrs:
144 if sepStr[1] ==
'after':
145 index = arg[0].
find(sepStr[0])
147 index = arg[0].rfind(sepStr[0])
149 if sepStr[1] ==
'after':
150 return '%s%s' % (epText, arg[0][index+len(sepStr[0]):].strip())
152 return '%s%s' % (epText, arg[0][:index].strip())
157 return arg[0].strip()
161 '''Parse the title string and extract an episode number
162 Call example: 'mnvXpath:mevioEpisode(./title/text())'
163 return an episode element
169 episodeNumber = match.groups()
171 return etree.XML(
'<episode>%s</episode>' % episodeNumber)
175 '''Use a unique key value pairing to find out if the 'internetcontentarticles' table already
176 has a matching item. This is done to save accessing the Internet when
not required.
177 Call example:
'mnvXpath:mevioCheckIfDBItem(title, description)'
178 return True if a match was found
179 return False if a match was
not found
181 return common.checkIfDBItem(
'dummy', {
'feedtitle':
'Technology',
'title': arg[0],
'description': arg[1]})
def __init__(self, outstream, encoding=None)
def __getattr__(self, attr)
def mevioCheckIfDBItem(self, context, *arg)
def mevioTitle(self, context, arg)
def mevioLinkGeneration(self, context, *arg)
Start of XPath extension functions.
def mevioEpisode(self, context, arg)
static pid_list_t::iterator find(const PIDInfoMap &map, pid_list_t &list, pid_list_t::iterator begin, pid_list_t::iterator end, bool find_open)