MythTV  master
bliptvXSL_api.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: UTF-8 -*-
3 # ----------------------
4 # Name: bliptvXSL_api - XPath and XSLT functions for the Blip.tv RSS/HTML itmes
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__ ="bliptvXSL_api - XPath and XSLT functions for the Blip.tv RSS/HTML"
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.1"
25 # 0.1.0 Initial development
26 # 0.1.1 Fixed a bug when an autoplay link cannot be created
27 # Added MP4 as an acceptable downloadable video file type
28 
29 
30 # Specify the class names that have XPath extention functions
31 __xpathClassList__ = ['xpathFunctions', ]
32 
33 # Specify the XSLT extention class names. Each class is a stand lone extention function
34 #__xsltExtentionList__ = ['xsltExtExample', ]
35 __xsltExtentionList__ = []
36 
37 import os, sys, re, time, datetime, shutil, urllib, string
38 from copy import deepcopy
39 
40 
41 class OutStreamEncoder(object):
42  """Wraps a stream with an encoder"""
43  def __init__(self, outstream, encoding=None):
44  self.out = outstream
45  if not encoding:
46  self.encoding = sys.getfilesystemencoding()
47  else:
48  self.encoding = encoding
49 
50  def write(self, obj):
51  """Wraps the output stream, encoding Unicode strings with the specified encoding"""
52  if isinstance(obj, unicode):
53  try:
54  self.out.write(obj.encode(self.encoding))
55  except IOError:
56  pass
57  else:
58  try:
59  self.out.write(obj)
60  except IOError:
61  pass
62 
63  def __getattr__(self, attr):
64  """Delegate everything but write to the stream"""
65  return getattr(self.out, attr)
66 sys.stdout = OutStreamEncoder(sys.stdout, 'utf8')
67 sys.stderr = OutStreamEncoder(sys.stderr, 'utf8')
68 
69 try:
70  from StringIO import StringIO
71  from lxml import etree
72 except Exception, e:
73  sys.stderr.write(u'\n! Error - Importing the "lxml" and "StringIO" python libraries failed on error(%s)\n' % e)
74  sys.exit(1)
75 
76 # Check that the lxml library is current enough
77 # From the lxml documents it states: (http://codespeak.net/lxml/installation.html)
78 # "If you want to use XPath, do not use libxml2 2.6.27. We recommend libxml2 2.7.2 or later"
79 # Testing was performed with the Ubuntu 9.10 "python-lxml" version "2.1.5-1ubuntu2" repository package
80 version = ''
81 for digit in etree.LIBXML_VERSION:
82  version+=str(digit)+'.'
83 version = version[:-1]
84 if version < '2.7.2':
85  sys.stderr.write(u'''
86 ! Error - The installed version of the "lxml" python library "libxml" version is too old.
87  At least "libxml" version 2.7.2 must be installed. Your version is (%s).
88 ''' % version)
89  sys.exit(1)
90 
91 
92 class xpathFunctions(object):
93  """Functions specific extending XPath
94  """
95  def __init__(self):
96  self.functList = ['bliptvFlvLinkGeneration', 'bliptvDownloadLinkGeneration', 'bliptvEpisode', 'bliptvIsCustomHTML', ]
97  self.episodeRegex = [
98  re.compile(u'''TERRA\\ (?P<episodeno>[0-9]+).*$''', re.UNICODE),
99  ]
100  self.namespaces = {
101  'xsi': u"http://www.w3.org/2001/XMLSchema-instance",
102  'media': u"http://search.yahoo.com/mrss/",
103  'xhtml': u"http://www.w3.org/1999/xhtml",
104  'atm': u"http://www.w3.org/2005/Atom",
105  'mythtv': "http://www.mythtv.org/wiki/MythNetvision_Grabber_Script_Format",
106  'itunes':"http://www.itunes.com/dtds/podcast-1.0.dtd",
107  'creativeCommons': "http://backend.userland.com/creativeCommonsRssModule",
108  'geo': "http://www.w3.org/2003/01/geo/wgs84_pos#",
109  'blip': "http://blip.tv/dtd/blip/1.0",
110  'wfw': "http://wellformedweb.org/CommentAPI/",
111  'amp': "http://www.adobe.com/amp/1.0",
112  'dcterms': "http://purl.org/dc/terms",
113  'gm': "http://www.google.com/schemas/gm/1.1",
114  'mediaad': "http://blip.tv/dtd/mediaad/1.0",
115  }
116  self.flvFilter = etree.XPath(".//media:content[@type='video/x-flv']", namespaces=self.namespaces)
117  self.m4vFilter = etree.XPath(".//media:content[@type='video/mp4' or @type='video/quicktime' or @type='video/x-m4v']", namespaces=self.namespaces)
118  self.durationFilter = etree.XPath(".//blip:runtime/text()", namespaces=self.namespaces)
119  self.linkFilter = etree.XPath("./link/text()", namespaces=self.namespaces)
120  self.languageFilter = etree.XPath("../language/text()", namespaces=self.namespaces)
121  # end __init__()
122 
123 
128 
129  def bliptvFlvLinkGeneration(self, context, arg):
130  '''Generate a link for the Blip.tv site.
131  Call example: 'mnvXpath:bliptvFlvLinkGeneration(.)'
132  return the url link
133  '''
134  flvFile = self.flvFilter(arg[0])
135  if len(flvFile):
136  flvFileLink = flvFile[0].attrib['url']
137  return u'%s%s' % (common.linkWebPage('dummy', 'bliptv'), flvFileLink.replace(u'.flv', u'').replace(u'http://blip.tv/file/get/', u''))
138  else:
139  return self.linkFilter(arg[0])[0]
140  # end bliptvXSLLinkGeneration()
141 
142  def bliptvDownloadLinkGeneration(self, context, arg):
143  '''Generate a download link for the Blip.tv site.
144  Call example: 'mnvXpath:bliptvDownloadLinkGeneration(.)'
145  return an array of one download link element
146  '''
147  downloadLink = etree.XML(u'<link></link>')
148  flvFile = self.flvFilter(arg[0])
149  m4vFile = self.m4vFilter(arg[0])
150  if len(m4vFile):
151  downloadLink.attrib['url'] = m4vFile[0].attrib['url']
152  if m4vFile[0].attrib.get('width'):
153  downloadLink.attrib['width'] = m4vFile[0].attrib['width']
154  if m4vFile[0].attrib.get('height'):
155  downloadLink.attrib['height'] = m4vFile[0].attrib['height']
156  if m4vFile[0].attrib.get('fileSize'):
157  downloadLink.attrib['length'] = m4vFile[0].attrib['fileSize']
158  if len(self.durationFilter(arg[0])):
159  downloadLink.attrib['duration'] = self.durationFilter(arg[0])[0]
160  downloadLink.attrib['lang'] = self.languageFilter(arg[0])[0]
161  return [downloadLink]
162  elif len(flvFile):
163  downloadLink.attrib['url'] = flvFile[0].attrib['url']
164  if flvFile[0].attrib.get('width'):
165  downloadLink.attrib['width'] = flvFile[0].attrib['width']
166  if flvFile[0].attrib.get('height'):
167  downloadLink.attrib['height'] = flvFile[0].attrib['height']
168  if flvFile[0].attrib.get('fileSize'):
169  downloadLink.attrib['length'] = flvFile[0].attrib['fileSize']
170  if len(self.durationFilter(arg[0])):
171  downloadLink.attrib['duration'] = self.durationFilter(arg[0])[0]
172  downloadLink.attrib['lang'] = self.languageFilter(arg[0])[0]
173  return [downloadLink]
174  else:
175  downloadLink.attrib['url'] = self.linkFilter(arg[0])[0]
176  if len(self.durationFilter(arg[0])):
177  downloadLink.attrib['duration'] = self.durationFilter(arg[0])[0]
178  downloadLink.attrib['lang'] = self.languageFilter(arg[0])[0]
179  return [downloadLink]
180  # end bliptvDownloadLinkGeneration()
181 
182  def bliptvEpisode(self, context, arg):
183  '''Parse the title string and extract an episode number
184  Call example: 'mnvXpath:bliptvEpisode(./title/text())'
185  return the url link
186  '''
187  episodeNumber = u''
188  for index in range(len(self.episodeRegex)):
189  match = self.episodeRegex[index].match(arg[0])
190  if match:
191  episodeNumber = match.groups()
192  break
193  return etree.XML(u'<episode>%s</episode>' % episodeNumber)
194  # end bliptvEpisode()
195 
196  def bliptvIsCustomHTML(self, context, arg):
197  '''Parse the item element and deternmine if there is a flv file
198  Call example: 'mnvXpath:bliptvIsCustomHTML(.)'
199  return True is there is a '.flv' file
200  return False if there is no .flv' file
201  '''
202  if len(self.flvFilter(arg[0])):
203  return True
204  return False
205  # end bliptvIsCustomHTML()
206 
207 
212 
213 
218 
219 
def bliptvFlvLinkGeneration(self, context, arg)
Start of XPath extension functions.