15 __title__ =
"traileraddicts_api - XPath and XSLT functions for the TrailerAddicts.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 = [
'traileraddictsLinkGenerationMovie',
'traileraddictsLinkGenerationClip',
'traileraddictsCheckIfDBItem']
104 '''Generate a link for the TrailerAddicts.com site.
105 Call example: 'mnvXpath:traileraddictsLinkGenerationMovie(position(), link)'
108 webURL = args[1].strip()
111 if 'traileraddictsLinkGenerationMovie' in self.
persistence:
112 if args[0] == self.
persistence[
'traileraddictsLinkGenerationMovie'][
'position']:
113 return self.
persistence[
'traileraddictsLinkGenerationMovie'][
'link']
115 self.
persistence[
'traileraddictsLinkGenerationMovie'] = {}
116 self.
persistence[
'traileraddictsLinkGenerationMovie'][
'embedRSS'] = etree.parse(
'http://www.traileraddict.com/embedrss', common.parsers[
'xml'].
copy())
117 self.
persistence[
'traileraddictsLinkGenerationMovie'][
'matchlink'] = etree.XPath(
'//link[string()=$link]/..', namespaces=common.namespaces)
118 self.
persistence[
'traileraddictsLinkGenerationMovie'][
'description'] = etree.XPath(
'normalize-space(description)', namespaces=common.namespaces)
119 self.
persistence[
'traileraddictsLinkGenerationMovie'][
'embedded'] = etree.XPath(
'//embed/@src', namespaces=common.namespaces)
121 self.
persistence[
'traileraddictsLinkGenerationMovie'][
'position'] = args[0]
123 matchLink = self.
persistence[
'traileraddictsLinkGenerationMovie'][
'matchlink'](self.
persistence[
'traileraddictsLinkGenerationMovie'][
'embedRSS'], link=webURL)[0]
124 self.
persistence[
'traileraddictsLinkGenerationMovie'][
'link'] = self.
persistence[
'traileraddictsLinkGenerationMovie'][
'embedded'](common.getHtmlData(
'dummy',(self.
persistence[
'traileraddictsLinkGenerationMovie'][
'description'](matchLink))))[0]
126 return self.
persistence[
'traileraddictsLinkGenerationMovie'][
'link']
131 '''Generate a link for the TrailerAddicts.com site.
132 Call example: 'mnvXpath:traileraddictsLinkGenerationClip(position(), link)'
135 webURL = args[1].strip()
137 if 'traileraddictsLinkGenerationClip' in self.
persistence:
138 if args[0] == self.
persistence[
'traileraddictsLinkGenerationClip'][
'position']:
139 return self.
persistence[
'traileraddictsLinkGenerationClip'][
'link']
141 self.
persistence[
'traileraddictsLinkGenerationClip'] = {}
142 self.
persistence[
'traileraddictsLinkGenerationClip'][
'embedded'] = etree.XPath(
'//embed[@allowfullscreen="true"]/@src', namespaces=common.namespaces)
144 self.
persistence[
'traileraddictsLinkGenerationClip'][
'position'] = args[0]
146 tmpHTML = etree.parse(webURL, etree.HTMLParser())
147 self.
persistence[
'traileraddictsLinkGenerationClip'][
'link'] = self.
persistence[
'traileraddictsLinkGenerationClip'][
'embedded'](tmpHTML)[0]
148 return self.
persistence[
'traileraddictsLinkGenerationClip'][
'link']
152 '''Use a unique key value pairing to find out if the 'internetcontentarticles' table already
153 has a matching item. This is done to save accessing the Internet when not required.
154 Call example: 'mnvXpath:traileraddictsCheckIfDBItem(.)'
155 return True if a match was found
156 return False if a match was not found
158 return common.checkIfDBItem(
'dummy', {
'feedtitle':
'Movie Trailers',
'title': arg[0],
'author': arg[1],
'description': arg[2]})