15 __title__ =
"spitzer_api - XPath and XSLT functions for the www.spitzer.caltech.edu 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 = [
'spitzerLinkGeneration',
'spitzerThumbnailLink',
'spitzerCheckIfDBItem', ]
105 '''Generate a link for the www.spitzer.caltech.edu site.
106 Call example: 'mnvXpath:spitzerLinkGeneration(normalize-space(link), $paraMeter)'
112 tmpHandle = urllib.request.urlopen(webURL)
113 tmpHTML = str(tmpHandle.read(),
'utf-8')
115 except Exception
as errmsg:
116 sys.stderr.write(
"Error reading url(%s) error(%s)\n" % (webURL, errmsg))
120 findText =
"file=mp4:"
121 lenText = len(findText)
122 posText = tmpHTML.find(findText)
125 tmpHTML = tmpHTML[posText+lenText:]
126 tmpLink = tmpHTML[:tmpHTML.find(
'.')]
129 customHTML = common.linkWebPage(
'dummy',
'spitzer')
130 customHTML = customHTML.replace(
'TITLE', urllib.parse.quote(pageTitle))
131 customHTML = customHTML.replace(
'VIDEOCODE', tmpLink)
135 lenText = len(findText)
136 posText = tmpHTML.find(findText)
139 return customHTML.replace(
'IMAGE',
'')
140 tmpHTML = tmpHTML[posText+lenText:]
141 tmpImage = tmpHTML[:tmpHTML.find(
'"')]
142 self.
persistence[
'spitzerThumbnailLink'] =
'http://www.spitzer.caltech.edu%s' % tmpImage
144 return customHTML.replace(
'IMAGE', tmpImage)
148 '''Verify that the thumbnail actually exists. If it does not then use the site image.
149 Call example: 'mnvXpath:spitzerThumbnailLink('dummy')'
150 return the thumbnail url
159 '''Use a unique key value pairing to find out if the 'internetcontentarticles' table already
160 has a matching item. This is done to save accessing the Internet when not required.
161 Call example: 'mnvXpath:spitzerCheckIfDBItem(title, author, description)'
162 return True if a match was found
163 return False if a match was not found
165 return common.checkIfDBItem(
'dummy', {
'feedtitle':
'Space',
'title': arg[0],
'author': arg[1],
'description': arg[2]})