MythTV  master
linuxAction_api.py
Go to the documentation of this file.
1 # -*- coding: UTF-8 -*-
2 
3 # ----------------------
4 # Name: linuxAction_api - XPath and XSLT functions for the www.jupiterbroadcasting.com RSS/HTML items
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__ ="linuxAction_api - XPath and XSLT functions for the www.jupiterbroadcasting.com 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 
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 = ['linuxActionLinkGeneration', 'linuxActionTitleSeEp', 'linuxActioncheckIfDBItem', ]
93  self.s_e_Regex = [
94  # s12e05
95  re.compile('''^.+?[Ss](?P<seasno>[0-9]+)\\e(?P<epno>[0-9]+).*$''', re.UNICODE),
96  # Season 11 Episode 3
97  re.compile('''^.+?Season\\ (?P<seasno>[0-9]+)\\ Episode\\ (?P<epno>[0-9]+).*$''', re.UNICODE),
98  ]
99  self.namespaces = {
100  'atom': "http://www.w3.org/2005/Atom",
101  'atom10': "http://www.w3.org/2005/Atom",
102  'media': "http://search.yahoo.com/mrss/",
103  'itunes':"http://www.itunes.com/dtds/podcast-1.0.dtd",
104  'xhtml': "http://www.w3.org/1999/xhtml",
105  'mythtv': "http://www.mythtv.org/wiki/MythNetvision_Grabber_Script_Format",
106  'feedburner': "http://rssnamespace.org/feedburner/ext/1.0",
107  'fb': "http://www.facebook.com/2008/fbml",
108  }
109  self.mediaIdFilters = [
110  [etree.XPath('//object/@id', namespaces=self.namespaces ), None],
111  ]
112  self.FullScreen = 'http://linuxAction.com/show/popupPlayer?video_id=%s&quality=high&offset=0'
113  self.FullScreenParser = common.parsers['html'].copy()
114  # end __init__()
115 
116 
121 
122  def linuxActionLinkGeneration(self, context, *arg):
123  '''Generate a link for the video.
124  Call example: 'mnvXpath:linuxActionLinkGeneration(string(link))'
125  return the url link
126  '''
127  webURL = arg[0]
128  try:
129  tmpHandle = urllib.request.urlopen(webURL)
130  tmpHTML = str(tmpHandle.read(), 'utf-8')
131  tmpHandle.close()
132  except Exception as errmsg:
133  sys.stderr.write("Error reading url(%s) error(%s)\n" % (webURL, errmsg))
134  return webURL
135 
136  findText = "<embed src="
137  lenText = len(findText)
138  posText = tmpHTML.find(findText)
139  if posText == -1:
140  return webURL
141  tmpHTML = tmpHTML[posText+lenText+1:]
142 
143  tmpLink = tmpHTML[:tmpHTML.find('"')]
144  if tmpLink.find('www.youtube.com') != -1:
145  return '%s&autoplay=1' % tmpLink
146  else:
147  return '%s?autostart=1' % tmpLink
148  # end linuxActionLinkGeneration()
149 
150  def linuxActionTitleSeEp(self, context, *arg):
151  '''Parse the download link and extract an episode number
152  Call example: 'mnvXpath:linuxActionTitleSeEp(title)'
153  return the a massaged title element and an episode element in an array
154  '''
155  title = arg[0]
156  index = title.find('|')
157  if index > 0:
158  title = title[:index].strip()
159  index = title.find('The Linux Action Show')
160  if index > 0:
161  title = title[:index].strip()
162  index = title.find('! Season')
163  if index > 0:
164  title = title[:index-1].strip()
165  title = common.htmlToString('dummy', title)
166 
167  elementArray = []
168  seasonNumber = ''
169  episodeNumber = ''
170  for index in range(len(self.s_e_Regex)):
171  match = self.s_e_Regex[index].match(arg[0])
172  if match:
173  (seasonNumber, episodeNumber) = match.groups()
174  seasonNumber = '%s' % int(seasonNumber)
175  episodeNumber = '%s' % int(episodeNumber)
176  elementArray.append(etree.XML("<title>%s</title>" % ('S%02dE%02d: %s' % (int(seasonNumber), int(episodeNumber), title))))
177  break
178  else:
179  elementArray.append(etree.XML("<title>%s</title>" % title ))
180  if seasonNumber:
181  tmpElement = etree.Element('{http://www.mythtv.org/wiki/MythNetvision_Grabber_Script_Format}season')
182  tmpElement.text = seasonNumber
183  elementArray.append(tmpElement)
184  if episodeNumber:
185  tmpElement = etree.Element('{http://www.mythtv.org/wiki/MythNetvision_Grabber_Script_Format}episode')
186  tmpElement.text = episodeNumber
187  elementArray.append(tmpElement)
188  return elementArray
189  # end linuxActionTitleSeEp()
190 
191  def linuxActioncheckIfDBItem(self, context, *arg):
192  '''Use a unique key value pairing to find out if the 'internetcontentarticles' table already
193  has a matching item. This is done to save accessing the Internet when not required.
194  Call example: 'mnvXpath:linuxActioncheckIfDBItem(title, author)'
195  return True if a match was found
196  return False if a match was not found
197  '''
198  titleElement = self.linuxActionTitleSeEp('dummy', arg[0])[0]
199  return common.checkIfDBItem('dummy', {'feedtitle': 'Technology', 'title': titleElement.text, 'author': arg[1]})
200  # end linuxActioncheckIfDBItem()
201 
202 
207 
208 
213 
214 
nv_python_libs.xsltfunctions.linuxAction_api.OutStreamEncoder.write
def write(self, obj)
Definition: linuxAction_api.py:48
nv_python_libs.xsltfunctions.linuxAction_api.xpathFunctions.functList
functList
Definition: linuxAction_api.py:92
nv_python_libs.xsltfunctions.linuxAction_api.xpathFunctions.FullScreenParser
FullScreenParser
Definition: linuxAction_api.py:113
nv_python_libs.xsltfunctions.linuxAction_api.OutStreamEncoder.out
out
Definition: linuxAction_api.py:42
nv_python_libs.xsltfunctions.linuxAction_api.OutStreamEncoder.__getattr__
def __getattr__(self, attr)
Definition: linuxAction_api.py:57
nv_python_libs.xsltfunctions.linuxAction_api.xpathFunctions.namespaces
namespaces
Definition: linuxAction_api.py:99
nv_python_libs.xsltfunctions.linuxAction_api.xpathFunctions.linuxActionLinkGeneration
def linuxActionLinkGeneration(self, context, *arg)
Start of XPath extension functions.
Definition: linuxAction_api.py:122
nv_python_libs.xsltfunctions.linuxAction_api.xpathFunctions.FullScreen
FullScreen
Definition: linuxAction_api.py:112
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.linuxAction_api.xpathFunctions.linuxActioncheckIfDBItem
def linuxActioncheckIfDBItem(self, context, *arg)
Definition: linuxAction_api.py:191
nv_python_libs.xsltfunctions.linuxAction_api.xpathFunctions.__init__
def __init__(self)
Definition: linuxAction_api.py:91
nv_python_libs.xsltfunctions.linuxAction_api.OutStreamEncoder.encoding
encoding
Definition: linuxAction_api.py:44
nv_python_libs.xsltfunctions.linuxAction_api.OutStreamEncoder
Definition: linuxAction_api.py:39
nv_python_libs.xsltfunctions.linuxAction_api.xpathFunctions.mediaIdFilters
mediaIdFilters
Definition: linuxAction_api.py:109
nv_python_libs.xsltfunctions.linuxAction_api.xpathFunctions
Definition: linuxAction_api.py:88
nv_python_libs.xsltfunctions.linuxAction_api.xpathFunctions.s_e_Regex
s_e_Regex
Definition: linuxAction_api.py:93
nv_python_libs.xsltfunctions.linuxAction_api.OutStreamEncoder.__init__
def __init__(self, outstream, encoding=None)
Definition: linuxAction_api.py:41
nv_python_libs.xsltfunctions.linuxAction_api.xpathFunctions.linuxActionTitleSeEp
def linuxActionTitleSeEp(self, context, *arg)
Definition: linuxAction_api.py:150