MythTV  master
traileraddicts_api.py
Go to the documentation of this file.
1 # -*- coding: UTF-8 -*-
2 
3 # ----------------------
4 # Name: traileraddicts_api - XPath and XSLT functions for the TrailerAddicts.com grabber
5 # Python Script
6 # Author: R.D. Vaughan
7 # Purpose: This python script is intended to perform a variety of utility functions
8 # for the conversion of data to the MNV standard RSS output format.
9 # See this link for the specifications:
10 # http://www.mythtv.org/wiki/MythNetvision_Grabber_Script_Format
11 #
12 # License:Creative Commons GNU GPL v2
13 # (http://creativecommons.org/licenses/GPL/2.0/)
14 #-------------------------------------
15 __title__ ="traileraddicts_api - XPath and XSLT functions for the TrailerAddicts.com grabber"
16 __author__="R.D. Vaughan"
17 __purpose__='''
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
22 '''
23 
24 __version__="v0.1.0"
25 # 0.1.0 Initial development
26 
27 
28 # Specify the class names that have XPath extention functions
29 __xpathClassList__ = ['xpathFunctions', ]
30 
31 # Specify the XSLT extention class names. Each class is a stand lone extention function
32 #__xsltExtentionList__ = ['xsltExtExample', ]
33 __xsltExtentionList__ = []
34 
35 import os, sys, re, time, datetime, shutil, urllib.request, urllib.parse, urllib.error, string
36 from copy import deepcopy
37 import io
38 
39 class OutStreamEncoder(object):
40  """Wraps a stream with an encoder"""
41  def __init__(self, outstream, encoding=None):
42  self.out = outstream
43  if not encoding:
44  self.encoding = sys.getfilesystemencoding()
45  else:
46  self.encoding = encoding
47 
48  def write(self, obj):
49  """Wraps the output stream, encoding Unicode strings with the specified encoding"""
50  if isinstance(obj, str):
51  obj = obj.encode(self.encoding)
52  try:
53  self.out.buffer.write(obj)
54  except OSError:
55  pass
56 
57  def __getattr__(self, attr):
58  """Delegate everything but write to the stream"""
59  return getattr(self.out, attr)
60 
61 if isinstance(sys.stdout, io.TextIOWrapper):
62  sys.stdout = OutStreamEncoder(sys.stdout, 'utf8')
63  sys.stderr = OutStreamEncoder(sys.stderr, 'utf8')
64 
65 try:
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)
70  sys.exit(1)
71 
72 # Check that the lxml library is current enough
73 # From the lxml documents it states: (http://codespeak.net/lxml/installation.html)
74 # "If you want to use XPath, do not use libxml2 2.6.27. We recommend libxml2 2.7.2 or later"
75 # Testing was performed with the Ubuntu 9.10 "python-lxml" version "2.1.5-1ubuntu2" repository package
76 version = ''
77 for digit in etree.LIBXML_VERSION:
78  version+=str(digit)+'.'
79 version = version[:-1]
80 if version < '2.7.2':
81  sys.stderr.write('''
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).
84 ''' % version)
85  sys.exit(1)
86 
87 
88 class xpathFunctions(object):
89  """Functions specific extending XPath
90  """
91  def __init__(self):
92  self.functList = ['traileraddictsLinkGenerationMovie', 'traileraddictsLinkGenerationClip', 'traileraddictsCheckIfDBItem']
93  self.TextTail = etree.XPath("string()")
94  self.persistence = {}
95  # end __init__()
96 
97 
102 
103  def traileraddictsLinkGenerationMovie(self, context, *args):
104  '''Generate a link for the TrailerAddicts.com site.
105  Call example: 'mnvXpath:traileraddictsLinkGenerationMovie(position(), link)'
106  return the url link
107  '''
108  webURL = args[1].strip()
109 
110  # If this is for the download element then just return what was found for the "link" element
111  if 'traileraddictsLinkGenerationMovie' in self.persistence:
112  if args[0] == self.persistence['traileraddictsLinkGenerationMovie']['position']:
113  return self.persistence['traileraddictsLinkGenerationMovie']['link']
114  else:
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)
120 
121  self.persistence['traileraddictsLinkGenerationMovie']['position'] = args[0]
122 
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]
125 
126  return self.persistence['traileraddictsLinkGenerationMovie']['link']
127  # end traileraddictsLinkGenerationMovie()
128 
129 
130  def traileraddictsLinkGenerationClip(self, context, *args):
131  '''Generate a link for the TrailerAddicts.com site.
132  Call example: 'mnvXpath:traileraddictsLinkGenerationClip(position(), link)'
133  return the url link
134  '''
135  webURL = args[1].strip()
136  # If this is for the download element then just return what was found for the "link" element
137  if 'traileraddictsLinkGenerationClip' in self.persistence:
138  if args[0] == self.persistence['traileraddictsLinkGenerationClip']['position']:
139  return self.persistence['traileraddictsLinkGenerationClip']['link']
140  else:
141  self.persistence['traileraddictsLinkGenerationClip'] = {}
142  self.persistence['traileraddictsLinkGenerationClip']['embedded'] = etree.XPath('//embed[@allowfullscreen="true"]/@src', namespaces=common.namespaces)
143 
144  self.persistence['traileraddictsLinkGenerationClip']['position'] = args[0]
145 
146  tmpHTML = etree.parse(webURL, etree.HTMLParser())
147  self.persistence['traileraddictsLinkGenerationClip']['link'] = self.persistence['traileraddictsLinkGenerationClip']['embedded'](tmpHTML)[0]
148  return self.persistence['traileraddictsLinkGenerationClip']['link']
149  # end traileraddictsLinkGenerationClip()
150 
151  def traileraddictsCheckIfDBItem(self, context, *arg):
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
157  '''
158  return common.checkIfDBItem('dummy', {'feedtitle': 'Movie Trailers', 'title': arg[0], 'author': arg[1], 'description': arg[2]})
159  # end traileraddictsCheckIfDBItem()
160 
161 
166 
167 
172 
173 
174 
nv_python_libs.xsltfunctions.traileraddicts_api.OutStreamEncoder.encoding
encoding
Definition: traileraddicts_api.py:44
nv_python_libs.xsltfunctions.traileraddicts_api.xpathFunctions.__init__
def __init__(self)
Definition: traileraddicts_api.py:91
nv_python_libs.xsltfunctions.traileraddicts_api.OutStreamEncoder.write
def write(self, obj)
Definition: traileraddicts_api.py:48
nv_python_libs.xsltfunctions.traileraddicts_api.xpathFunctions.TextTail
TextTail
Definition: traileraddicts_api.py:93
nv_python_libs.xsltfunctions.traileraddicts_api.xpathFunctions.traileraddictsLinkGenerationMovie
def traileraddictsLinkGenerationMovie(self, context, *args)
Start of XPath extension functions.
Definition: traileraddicts_api.py:103
nv_python_libs.xsltfunctions.traileraddicts_api.OutStreamEncoder.__getattr__
def __getattr__(self, attr)
Definition: traileraddicts_api.py:57
nv_python_libs.xsltfunctions.traileraddicts_api.OutStreamEncoder
Definition: traileraddicts_api.py:39
MythFile::copy
MBASE_PUBLIC long long copy(QFile &dst, QFile &src, uint block_size=0)
Copies src file to dst file.
Definition: mythmiscutil.cpp:263
nv_python_libs.xsltfunctions.traileraddicts_api.OutStreamEncoder.out
out
Definition: traileraddicts_api.py:42
nv_python_libs.xsltfunctions.traileraddicts_api.xpathFunctions
Definition: traileraddicts_api.py:88
nv_python_libs.xsltfunctions.traileraddicts_api.xpathFunctions.traileraddictsCheckIfDBItem
def traileraddictsCheckIfDBItem(self, context, *arg)
Definition: traileraddicts_api.py:151
nv_python_libs.xsltfunctions.traileraddicts_api.xpathFunctions.persistence
persistence
Definition: traileraddicts_api.py:94
nv_python_libs.xsltfunctions.traileraddicts_api.OutStreamEncoder.__init__
def __init__(self, outstream, encoding=None)
Definition: traileraddicts_api.py:41
nv_python_libs.xsltfunctions.traileraddicts_api.xpathFunctions.functList
functList
Definition: traileraddicts_api.py:92
nv_python_libs.xsltfunctions.traileraddicts_api.xpathFunctions.traileraddictsLinkGenerationClip
def traileraddictsLinkGenerationClip(self, context, *args)
Definition: traileraddicts_api.py:130